Skip to content
项目

PeterKottas/DotNetCore.WindowsService

在 GitHub 查看

关于

一个简单的库,可将 .NET Core 应用托管为 Windows 服务。为微服务架构提供完美解决方案。

开发工具

这些数字如何统计

实测星标增长

24小时
0
7天
0
15天
0
1个月
正在收集历史

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README.md

原文

DotNetCore.WindowsService

Simple library that allows one to host dot net core application as windows services. Perfect solution to power micro-services architecture.

Important note

This library was created to enable one to host CONSOLE dot net core applications. If you want to host a WEBSITE as a service, you're better of following https://docs.microsoft.com/en-us/aspnet/core/hosting/windows-service

Installation

Using NuGet:

Install-Package PeterKottas.DotNetCore.WindowsService

Quick start

Easiest way to start is using a brand new template. Just do:

dotnet new -i PeterKottas.DotNetCore.WindowsService.Templates::*

This will add one template at the moment.

Follow up with this

mkdir NameOfYourProject
cd NameOfYourProject
dotnet new [ mcrsvc-min | mcrsvc-std ]

This will create a sample project for you. Next chapter explains its features in more details especially points 6 onwards if you used the template.

Community, feel encouraged to add more templates if you find something missing/usefull. I'll be more than happy to add these. Just copy the project in https://github.com/PeterKottas/DotNetCore.WindowsService/tree/master/Source/Templates/PeterKottas.DotNetCore.WindowsService.MinimalTemplate and follow instructions in https://github.com/dotnet/templating if you need more specific behvaiour.

Usage

  1. Create .NETCore console app.

  2. Create your first service, something like this:

    public class ExampleService : IMicroService
    {
        public void Start()
        {
            Console.WriteLine("I started");
        }
        
        public void Stop()
        {
            Console.WriteLine("I stopped");
        }
    }
    
  3. You can also inherit MicroService base class and take advantage of built in timers:

    public class ExampleService : MicroService, IMicroService
    {
        public void Start()
        {
            this.StartBase();
            Timers.Start("Poller", 1000, () =>
            {
                Console.WriteLine("Polling at {0}\n", DateTime.Now.ToString("o"));
            },
            (e) =>
            {
                Console.WriteLine("Exception while polling: {0}\n", e.ToString());
            });
            Console.WriteLine("I started");
        }
        
        public void Stop()
        {
            this.StopBase();
            Console.WriteLine("I stopped");
        }
    }
    
  4. Api for services (and yeah, it's similar to Topshelf, thanks for inspiration, I just couldn't wait for you guys to implement this):

    ServiceRunner<ExampleService>.Run(config =>
    {
        var name = config.GetDefaultName();
        config.Service(serviceConfig =>
            {
                serviceConfig.ServiceFactory((extraArguments, microServiceController) =>
            {
                return new ExampleService();
            });
            serviceConfig.OnStart((service, extraArguments) =>
            {
                Console.WriteLine("Service {0} started", name);
                service.Start();
            });
    
            serviceConfig.OnStop(service =>
            {
                Console.WriteLine("Service {0} stopped", name);
                service.Stop();
            });
            
            serviceConfig.OnInstall(service =>
            {
                Console.WriteLine("Service {0} installed", name);
            });
            
            serviceConfig.OnUnInstall(service =>
            {
                Console.WriteLine("Service {0} uninstalled", name);
            });
            
            serviceConfig.OnPause(service =>
            {
                Console.WriteLine("Service {0} paused", name);
            });
            
            serviceConfig.OnContinue(service =>
            {
                Console.WriteLine("Service {0} continued", name);
            });
            
            serviceConfig.OnShutdown(service =>
            {
                Console.WriteLine("Service {0} shutdown", name);
            });
    
            serviceConfig.OnError(e =>
            {
                Console.WriteLine("Service {0} errored with exception : {1}", name, e.Message);
            });
        });
    });
    
  5. Optionally set the name of the service like this:

    ServiceRunner<ExampleService>.Run(config =>
    {
        config.SetName("MyTestService");
    });
    
  6. Run the service without arguments and it runs like console app.

  7. Run the service with action:install and it will install the service.

  8. Run the service with action:uninstall and it will uninstall the service.

  9. Run the service with action:start and it will start the service.

  10. Run the service with action:stop and it will stop the service.

  11. Run the service with action:pause and it will pause the service.

  12. Run the service with action:continue and it will continue the service.

  13. Run the service with username:YOUR_USERNAME, password:YOUR_PASSWORD and action:install which installs it for the given account.

  14. Run the service with built-in-account:(NetworkService|LocalService|LocalSystem) and action:install which installs it for the given built in account. Defaults to LocalSystem.

  15. Run the service with description:YOUR_DESCRIPTION and it setup description for the service.

  16. Run the service with display-name:YOUR_DISPLAY_NAME and it setup Display name for the service.

  17. Run the service with name:YOUR_NAME and it setup name for the service.

  18. Run the service with start-immediately:(true|false) to start service immediately after install. Defaults to true.

  19. You can find the complete example in PeterKottas.DotNetCore.Example project.

  20. Install the service using powershell: dotnet.exe $serviceDllPath action:install

Created and sponsored by

  • GuestBell - Customer centric online POS for Hotels and short terms stays.

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

License

MIT

Credit

Huge thanks goes to @dasMulli the guy behind a useful lib which is one of the dependecies for this library.

探索相关项目

加载中…