Giter Site home page Giter Site logo

cckk66 / dotnet-win32-service Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dasmulli/dotnet-win32-service

0.0 1.0 0.0 999 KB

Helper classes to set up and run as windows services directly on .net core. A ServiceBase alternative.

License: MIT License

C# 100.00%

dotnet-win32-service's Introduction

.NET Standard based Windows Service support for .NET

This repo contains a library for running a .NET Core application as windows service, without the need for a wrapper assembly or the full (desktop) .NET Framework. It is built using P/Invoke calls into native windows assemblies.

Usage scenarios include:

  • Running on Windows Nano Server (no full framework but can run windows services)
  • Shipping a modern service application using the latest .NET core version to systems where you cannot upgrade to new versions of .NET, but you want to use new framework features.
  • Build truly portable applications that can for example run as service on windows and as daemon on linux, just using runtime checks / switches

How to use the example application

Prerequisites:

  • .NET Core SDK 2.0.3 or higher (.csproj based tooling)
  • Windows machine
  • Elevated command prompt: Run cmd as administrator.
> cd samples\TestService
> dotnet restore
> dotnet run --register-service --urls http://*:5080
...
Successfully registered and started service "Demo .NET Core Service" ("Demo ASP.NET Core Service running on .NET Core")

Open http://localhost:5080 in a browser. You should see Hello world.

The "Services" administrative tool should show the service: running service running service

> dotnet run --unregister-service
...
Successfully unregistered service "Demo .NET Core Service" ("Demo ASP.NET Core Service running on .NET Core")

Note that the service may show up as disabled for some time until all tools accessing the windows services apis have been closed. See this Stackoverflow question.

API

Add a NuGet package reference to DasMulli.Win32.ServiceUtils.

Write a windows service using:

using DasMulli.Win32.ServiceUtils;

class Program
{
    public static void Main(string[] args)
    {
        var myService = new MyService();
        var serviceHost = new Win32ServiceHost(myService);
        serviceHost.Run();
    }
}

class MyService : IWin32Service
{
    public string ServiceName => "Test Service";

    public void Start(string[] startupArguments, ServiceStoppedCallback serviceStoppedCallback)
    {
        // Start coolness and return
    }

    public void Stop()
    {
        // shut it down again
    }
}

You can then register your service via sc.exe (run cmd / powershell as administrator!):

sc.exe create MyService DisplayName= "My Service" binpath= "C:\Program Files\dotnet\dotnet.exe C:\path\to\MyService.dll --run-as-service"

Now go the services console / task manager and start your service.

Not that sc will install your service as SYSTEM user which has way to many access rights to run things like web apps. See it's reference for more options.

If you want to get rid of it again, use:

sc.exe delete MyService

You can also create a service that registers itself like the example provided by taking a look at the sample source.

Also take a look at the ASP.NET Core MVC sample, which has additional logic to set the correct working directory. When running it in development and not from the published output, be sure to pass --preserve-working-directory to it when registering so that it will run from the project directory (e.g. run dotnet run --register-service --preserve-working-directory from and administrative command prompt).

Limitations

  • No custom exceptions / error codes. Everything will throw a Win32Exception if something goes wrong (It's message should be interpretable on windows).
  • All exceptions thrown by the service implementation will cause the service host to report exit code -1 / 0xffffffff to the service control manager.
  • Currently, no direct support for services supporting pause and continue commands as well as other commands (power event, system shutdown)
    • However, consumers can now use IWin32ServiceStateMachine to implement custom behavior. Copy SimpleServiceStateMachine as a starting point to implement extended services.

dotnet-win32-service's People

Contributors

dasmulli avatar trydis avatar rydergillen-compacsort avatar ravualhemio avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.