{"id":22538452,"url":"https://github.com/shuttle/shuttle.core.workerservice","last_synced_at":"2025-08-04T04:31:59.188Z","repository":{"id":43771666,"uuid":"461118765","full_name":"Shuttle/Shuttle.Core.WorkerService","owner":"Shuttle","description":"Turns a console application into a Worker Service that can be used as a Windows Service / Systemd Unit.","archived":false,"fork":false,"pushed_at":"2022-08-31T16:06:36.000Z","size":29,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-23T13:55:20.536Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Shuttle.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-02-19T07:27:57.000Z","updated_at":"2024-04-23T13:55:20.537Z","dependencies_parsed_at":"2023-01-16T18:15:41.647Z","dependency_job_id":null,"html_url":"https://github.com/Shuttle/Shuttle.Core.WorkerService","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shuttle%2FShuttle.Core.WorkerService","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shuttle%2FShuttle.Core.WorkerService/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shuttle%2FShuttle.Core.WorkerService/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shuttle%2FShuttle.Core.WorkerService/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Shuttle","download_url":"https://codeload.github.com/Shuttle/Shuttle.Core.WorkerService/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228593488,"owners_count":17942329,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-07T11:12:08.855Z","updated_at":"2024-12-07T11:12:09.384Z","avatar_url":"https://github.com/Shuttle.png","language":"C#","readme":"# Shuttle.Core.WorkerService\n\n```\nPM\u003e Install-Package Shuttle.Core.WorkerService\n```\n\nA simple wrapper built around the `Microsoft.Extensions.Hosting` that allows you to host your Net 5.0+ console application (Net 5.0+).  On Windows you can host the resulting console application as a Windows Service.  On Linux you would use Systemd to host the service.\n\n## Implementation\n\nA typical implementation would be as follows:\n\n``` c#\nusing System;\nusing System.Threading;\nusing Shuttle.Core.WorkerService;\n\nnamespace Shuttle.Core.ServiceHost.Server\n{\n    internal class Program\n    {\n        private static void Main()\n        {\n            ServiceHost.Run\u003cTestHost\u003e();\n        }\n\n        public class TestHost : IServiceHost\n        {\n            private readonly Thread _thread;\n            private volatile bool _active;\n\n            public TestHost()\n            {\n                _thread = new Thread(Worker);\n            }\n\n            public void Start()\n            {\n                _active = true;\n                _thread.Start();\n            }\n\n            public void Stop()\n            {\n                _active = false;\n                _thread.Join(5000);\n            }\n\n            public bool Active =\u003e _active;\n\n            private void Worker()\n            {\n                while (_active)\n                {\n                    Console.WriteLine($\"[working] : {DateTime.Now:O}\");\n                    ThreadSleep.While(1000, this);\n                }\n            }\n        }\n    }\n}\n```\n\nImplement the `IServiceHost` interface if you need both `Start()` and `Stop()` methods; else `IServiceHostStart` for `Start()` and `IServiceHostStop` for `Stop()` although there would be little value in having only a `Stop()`.  If you do not need a `Stop()` method or you prefer using `IDisposable` to handle the destruction then you would go with only the `IServiceHostStart` interface.\n\n## Running the host\n\nThe following methods are available to get this going on the `ServiceHost` class:\n\n``` c#\npublic static void Run\u003cT\u003e() where T : IServiceHostStart, new()\npublic static void Run(IServiceHostStart service)\n```\n\nFor .Net 4.6+ the following are also available:\n\n``` c#\npublic static void Run\u003cT\u003e(Action\u003cIServiceConfiguration\u003e configure) where T : IServiceHostStart, new()\npublic static void Run(IServiceHostStart service, Action\u003cIServiceConfiguration\u003e configure)\n```\n\n## Windows\n\nInstalling on `Windows` requires using the [sc utility](https://docs.microsoft.com/en-us/windows/win32/services/controlling-a-service-using-sc):\n\n``` cmd\nsc create {service-name} binPath={path-to-exe}\n```\n\n## Linux\n\nUsing `Systemd` on `Linux` would require a `{SserviceName}.service` file in the `/etc/systemd/system/` folder:\n\n``` sh\n[Unit]\nDescription=Service created using Shuttle.Core.WorkerService\n\n[Service]\nExecStart=/srv/somewhere/ServiceName\n# journalctl identifier\nSyslogIdentifier=HelloWorld\n\nUser=username\n\nEnvironment=DOTNET_ROOT={dotnet-path}\n\n[Install]\nWantedBy=multi-user.target\n```\n\nThen reload the `Systemd` configuration:\n\n``` sh\nsudo systemctl daemon-reload\nsudo systemctl start {ServiceName}\n```\n\nYou can then view the status using:\n\n``` sh\nsudo systemctl status {ServiceName}\n```\n\nThe log can be viewed as follows:\n\n``` sh\nsudo journalctl -u HelloWorld\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshuttle%2Fshuttle.core.workerservice","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshuttle%2Fshuttle.core.workerservice","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshuttle%2Fshuttle.core.workerservice/lists"}