In dotnet, topshelf can easily write Windows services and installation is also very convenient, the command line run. EXE install directly installs the EXE program as a Windows service. Of course
The code should also make corresponding changes, the specific can be referred to examples.
In Dotnet Core 2.0 We also have a handy dll to try out
Https://github.com/PeterKottas/DotNetCore.WindowsService
Use NuGet to install the using Nuget:install-package PeterKottas.DotNetCore.WindowsService
Convenient for multiple services we define an interface first
public interface Ibaseservice
{
void Start ();
void Stop ();
}
Specific implementation I give an example, in the example we tried a timer, timed to complete certain tasks, so that we can write a few service at the same time as long as continue to ibaseservice on the line, but also the comparison of the installation
public class Syncservice:ibaseservice
{
Private ReadOnly System.Timers.Timer _timer;
Private ReadOnly ILogger logger;
Public SyncService (Iloggerfactory loggerfactory)
{
_timer = new System.Timers.Timer (10000);
_timer. Elapsed + = new Elapsedeventhandler (ontimedevent);
_timer. Interval = 2000;
_timer. AutoReset = true;
_timer. Enabled = false;
Logger = loggerfactory.createlogger<syncservice> ();
}
private void Ontimedevent (object source, Elapsedeventargs e)
{
Console.WriteLine (String. Format ("Syncservice:{0:yyyy-mm-dd HH:mm:sss}", DateTime.Now));
_timer. Enabled = false;
Try
{
do some job;
}
catch (Exception ex)
{
Logger. LogError ("SyncService Error {0}:", ex. Message);
}
Console.WriteLine (String. Format ("Syncservice:{0:yyyy-mm-dd HH:mm:sss}", DateTime.Now));
Thread.Sleep (5 * 60 * 1000);
_timer. Enabled = true;
}
Private async task{
string url = configmodel.databaseincrementurl;
var httpClient = new HttpClient ();
Return await Httpclient.getasync (URL);
}
public void Start ()
{
_timer. Start ();
_timer. Enabled = true;
}
public void Stop ()
{
_timer. Stop ();
_timer. Enabled = false;
}
}
Class Program
{
static void Main (string[] args)
{
Iconfigurationroot Configuration;
Iloggerfactory loggerfactory;
var builder = new Configurationbuilder ()
. Setbasepath (Path.Combine (appcontext.basedirectory))
. Addjsonfile ("Appsettings.json")
. Addenvironmentvariables ();
Configuration = Builder. Build ();
var services = new Servicecollection ();
Services. AddOptions ();
Services. Configure<configmodel> (Configuration.getsection ("configsetting"));
Services. Addsingleton<iconfiguration> (Configuration);
Services. Addtransient<iloggerfactory, loggerfactory> ();
Services. Addtransient<ibaseservice, syncservice> ();
Services. Addtransient<ibaseservice, createdbservice> ();
Services. Addtransient<ibaseservice, onlineorderservice> ();
var serviceprovider = Services. Buildserviceprovider ();
Servicerunner<servicefactory>. Run (config =
{
var name = config. Getdefaultname ();
Config. Service (ServiceConfig =
{
Serviceconfig.servicefactory (extraarguments, Controller) =
{
return new Servicefactory (serviceprovider.getservice<ienumerable<ibaseservice>> (), Controller);
});
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);
});
});
Config. SetName ("Saasservice");
Config. SetDescription ("SaaS Service for All SaaS Client");
Config. Setdisplayname ("SAAS Service");
});
}
}
Dotnet Core Windows Service