All Projects → dasMulli → Dotnet Win32 Service

dasMulli / Dotnet Win32 Service

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

Projects that are alternatives of or similar to Dotnet Win32 Service

Spectre.console
A .NET library that makes it easier to create beautiful console applications.
Stars: ✭ 4,226 (+894.35%)
Mutual labels:  dotnet-standard, dotnet-core
Fluentresults
A generalised Result object implementation for .NET/C#
Stars: ✭ 266 (-37.41%)
Mutual labels:  dotnet-standard, dotnet-core
Mongo2go
Mongo2Go - MongoDB for integration tests (.NET Core)
Stars: ✭ 240 (-43.53%)
Mutual labels:  dotnet-standard, dotnet-core
Linqtotwitter
LINQ Provider for the Twitter API (C# Twitter Library)
Stars: ✭ 401 (-5.65%)
Mutual labels:  dotnet-standard, dotnet-core
Blog Core
Modular blog using Blazor with clean domain-driven design patterns
Stars: ✭ 345 (-18.82%)
Mutual labels:  dotnet-standard, dotnet-core
Entityframework Extensions
Entity Framework Bulk Operations | Improve Entity Framework performance with Bulk SaveChanges, Insert, update, delete and merge for SQL Server, SQL Azure, SQL Compact, MySQL and SQLite.
Stars: ✭ 215 (-49.41%)
Mutual labels:  dotnet-standard, dotnet-core
PokemonBattleEngine
A C# library that can emulate Pokémon battles.
Stars: ✭ 92 (-78.35%)
Mutual labels:  dotnet-core, dotnet-standard
Command Line Api
Command line parsing, invocation, and rendering of terminal output.
Stars: ✭ 2,418 (+468.94%)
Mutual labels:  dotnet-standard, dotnet-core
Websocket Client
🔧 .NET/C# websocket client library
Stars: ✭ 297 (-30.12%)
Mutual labels:  dotnet-standard, dotnet-core
Tusdotnet
.NET server implementation of the Tus protocol for resumable file uploads. Read more at https://tus.io
Stars: ✭ 298 (-29.88%)
Mutual labels:  dotnet-standard, dotnet-core
Storage
💿 Storage abstractions with implementations for .NET/.NET Standard
Stars: ✭ 380 (-10.59%)
Mutual labels:  dotnet-standard, dotnet-core
Awesome Cms Core
Awesome CMS Core is an open source CMS built using ASP.Net Core & ReactJS with module seperation concern in mind and provide lastest trend of technology like .Net Core, React, Webpack, SASS, Background Job, Message Queue.
Stars: ✭ 352 (-17.18%)
Mutual labels:  dotnet-standard, dotnet-core
Corehook
A library that simplifies intercepting application function calls using managed code and the .NET Core runtime
Stars: ✭ 191 (-55.06%)
Mutual labels:  dotnet-standard, dotnet-core
Sharpyaml
SharpYaml is a .NET library for YAML compatible with CoreCLR
Stars: ✭ 217 (-48.94%)
Mutual labels:  dotnet-standard, dotnet-core
Rafty
Implementation of RAFT consensus in .NET core
Stars: ✭ 182 (-57.18%)
Mutual labels:  dotnet-standard, dotnet-core
Sharpsnmplib
Sharp SNMP Library- Open Source SNMP for .NET and Mono
Stars: ✭ 247 (-41.88%)
Mutual labels:  dotnet-standard, dotnet-core
Appmetrics
App Metrics is an open-source and cross-platform .NET library used to record and report metrics within an application.
Stars: ✭ 1,986 (+367.29%)
Mutual labels:  dotnet-standard, dotnet-core
Dotnet Etcd
A C# .NET (dotnet) GRPC client for etcd v3 +
Stars: ✭ 157 (-63.06%)
Mutual labels:  dotnet-standard, dotnet-core
Mockqueryable
Moking Entity Framework Core operations such ToListAsync, FirstOrDefaultAsync etc
Stars: ✭ 281 (-33.88%)
Mutual labels:  dotnet-standard, dotnet-core
Config
⚙ Config.Net - the easiest configuration framework for .NET developers
Stars: ✭ 349 (-17.88%)
Mutual labels:  dotnet-standard, dotnet-core

.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")

The service may show up as disabled for some time until all tools accessing the Windows services APIs have been closed. See this Stack Overflow 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 to Services or Task Manager and start your service.

sc will install your service as SYSTEM user which has way to many access rights to run things like web apps. See its 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).

Pause & Continue support

To create a service that supports being paused and later continued or stopped, implement IPausableWin32Service which extends IWin32Service by Pause() and Continue() methods you can use to implement your pause & continue logic.

Limitations

  • No custom exceptions / error codes. Everything will throw a Win32Exception if something goes wrong (Its 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 commands such as power event and system shutdown
    • However, consumers can now use IWin32ServiceStateMachine to implement custom behavior. Copy SimpleServiceStateMachine as a starting point to implement extended services.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].