{"id":13432037,"url":"https://github.com/dasMulli/dotnet-win32-service","last_synced_at":"2025-03-16T23:30:29.027Z","repository":{"id":60773929,"uuid":"70846293","full_name":"dasMulli/dotnet-win32-service","owner":"dasMulli","description":"Helper classes to set up and run as windows services directly on .net core. A ServiceBase alternative.","archived":false,"fork":false,"pushed_at":"2019-11-06T21:38:56.000Z","size":1055,"stargazers_count":451,"open_issues_count":9,"forks_count":57,"subscribers_count":30,"default_branch":"master","last_synced_at":"2025-03-09T18:12:54.567Z","etag":null,"topics":["dotnet-core","dotnet-standard","windows-service"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dasMulli.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":"2016-10-13T20:39:44.000Z","updated_at":"2025-02-21T01:46:37.000Z","dependencies_parsed_at":"2022-10-04T15:29:28.084Z","dependency_job_id":null,"html_url":"https://github.com/dasMulli/dotnet-win32-service","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dasMulli%2Fdotnet-win32-service","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dasMulli%2Fdotnet-win32-service/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dasMulli%2Fdotnet-win32-service/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dasMulli%2Fdotnet-win32-service/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dasMulli","download_url":"https://codeload.github.com/dasMulli/dotnet-win32-service/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243841205,"owners_count":20356458,"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":["dotnet-core","dotnet-standard","windows-service"],"created_at":"2024-07-31T02:01:08.079Z","updated_at":"2025-03-16T23:30:29.021Z","avatar_url":"https://github.com/dasMulli.png","language":"C#","funding_links":[],"categories":["Frameworks, Libraries and Tools","C#","C\\#","Windows Service","框架, 库和工具"],"sub_categories":["Windows Service","Windows服务"],"readme":"# .NET Standard-based Windows Service support for .NET\n\nThis repo contains a library for running a .NET Core application as Windows service without\nthe need for a wrapper assembly or the full (desktop) .NET Framework.\nIt is built using P/Invoke calls into native Windows assemblies.\n\nUsage scenarios include:\n* Running on Windows Nano Server (no full framework but can run Windows services)\n* Shipping a modern service application using the latest .NET Core version to systems\n  where you cannot upgrade to new versions of .NET, but you want to use new framework features.\n* Build truly portable applications that can for example run as service on Windows and as daemon on Linux,\n  just using runtime checks / switches\n\n## How to use the example application\nPrerequisites:\n* .NET Core SDK 2.0.3 or higher (`.csproj` based tooling)\n* Windows machine\n* **Elevated command prompt**: Run cmd as administrator.\n```cmd\n\u003e cd samples\\TestService\n\u003e dotnet restore\n\u003e dotnet run --register-service --urls http://*:5080\n...\nSuccessfully registered and started service \"Demo .NET Core Service\" (\"Demo ASP.NET Core Service running on .NET Core\")\n```\nOpen `http://localhost:5080` in a browser. You should see `Hello world`.\n\nThe \"Services\" administrative tool should show the service:\n![running service](./img/running-service.png)\n![running service](./img/running-service-taskmgr.png)\n\n```cmd\n\u003e dotnet run --unregister-service\n...\nSuccessfully unregistered service \"Demo .NET Core Service\" (\"Demo ASP.NET Core Service running on .NET Core\")\n```\nThe service may show up as `disabled` for some time until all tools accessing the Windows services APIs have been closed.\nSee this [Stack Overflow question](http://stackoverflow.com/questions/20561990/how-to-solve-the-specified-service-has-been-marked-for-deletion-error).\n\n## API\n\nAdd a NuGet package reference to `DasMulli.Win32.ServiceUtils`.\n\nWrite a Windows service using:\n\n```c#\nusing DasMulli.Win32.ServiceUtils;\n\nclass Program\n{\n    public static void Main(string[] args)\n    {\n        var myService = new MyService();\n        var serviceHost = new Win32ServiceHost(myService);\n        serviceHost.Run();\n    }\n}\n\nclass MyService : IWin32Service\n{\n    public string ServiceName =\u003e \"Test Service\";\n\n    public void Start(string[] startupArguments, ServiceStoppedCallback serviceStoppedCallback)\n    {\n        // Start coolness and return\n    }\n\n    public void Stop()\n    {\n        // shut it down again\n    }\n}\n```\n\nYou can then register your service via sc.exe (run cmd / powershell as administrator!):\n\n`sc.exe create MyService DisplayName= \"My Service\" binpath= \"C:\\Program Files\\dotnet\\dotnet.exe C:\\path\\to\\MyService.dll --run-as-service\"`\n\nNow go to Services or Task Manager and start your service.\n\n`sc` will install your service as `SYSTEM` user which has way to many access rights to run things like web apps.\nSee [its reference](https://technet.microsoft.com/en-us/library/cc990289(v=ws.11).aspx) for more options.\n\nIf you want to get rid of it again, use:\n\n`sc.exe delete MyService`\n\nYou can also create a service that registers itself like the example provided by\ntaking a look at [the sample source](./samples/TestService/Program.cs).\n\nAlso take a look at the [ASP.NET Core MVC sample](./samples/MvcTestService) which has additional logic to set the correct working directory.\nWhen running it in development and not from the published output, be sure to pass `--preserve-working-directory` to it when registering\nso that it will run from the project directory (e.g. run `dotnet run --register-service --preserve-working-directory` from and administrative\ncommand prompt).\n\n## Pause \u0026 Continue support\n\nTo create a service that supports being paused and later continued or stopped, implement `IPausableWin32Service` which extends `IWin32Service` by `Pause()` and `Continue()` methods\nyou can use to implement your pause \u0026 continue logic.\n\n## Limitations\n\n* No custom exceptions / error codes. Everything will throw a `Win32Exception` if something goes wrong (Its message should be\n  interpretable on Windows).\n* All exceptions thrown by the service implementation will cause the service host\n  to report exit code -1 / 0xffffffff to the service control manager.\n* Currently, no direct support for services supporting commands such as power event and system shutdown\n  * However, consumers can now use `IWin32ServiceStateMachine` to implement custom behavior.\n    Copy `SimpleServiceStateMachine` as a starting point to implement extended services.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FdasMulli%2Fdotnet-win32-service","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FdasMulli%2Fdotnet-win32-service","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FdasMulli%2Fdotnet-win32-service/lists"}