Giter Site home page Giter Site logo

tanhe / dotnetcore.windowsservice Goto Github PK

View Code? Open in Web Editor NEW

This project forked from peterkottas/dotnetcore.windowsservice

0.0 2.0 0.0 36 KB

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

License: MIT License

C# 100.00%

dotnetcore.windowsservice's Introduction

DotNetCore.WindowsService

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

Installation

Using nuget: Install-Package PeterKottas.DotNetCore.WindowsService

Usage

  1. Create .NETCore console app with a project.json simmilar to this:

    {
    	"version": "1.0.0-*",
    	"buildOptions": {
    		"emitEntryPoint": true
    	},
    	"frameworks": {
    		"netcoreapp1.1": {
    			"dependencies": {
    				"Microsoft.NETCore.App": {
    					"version": "1.1.0"//Optionally add "type": "platform" if you don't want self contained app
    				}
    			},
    			"imports": "dnxcore50"
    		}
    	},
    	"runtimes": { //Optionally add runtimes that you want to support
    		"win81-x64": {}
    	}
    }
  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 simmilar 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) =>
    		{
    			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.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 username:YOUR_USERNAME, password:YOUR_PASSWORD and action:install which installs it for the given account.

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

  13. Run the service with displayName:YOUR_DISPLAY_NAME and it setup Display name for the service.

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

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

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.

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.