Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Dotnet Core Windows Service

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail the example analysis about Dotnet Core Windows Service. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

In dotnet, there is topshelf that can easily write and install windows services. Run the command line. Exe install will directly install the exe program as a windows service. Of course, the code should also be modified accordingly, which can be referred to as an example.

We also have a convenient dll to use in dotnet core 2.0.

Https://github.com/PeterKottas/DotNetCore.WindowsService

Install via Nuget: Install-Package PeterKottas.DotNetCore.WindowsService

To facilitate multiple services, let's define an interface first.

Public interface IBaseService

{

Void Start ()

Void Stop ()

}

Let me give you an example of the specific implementation. in this example, we try a Timer to complete certain tasks on a regular basis, so that we can write several service at the same time as long as we continue to IBaseService and compare aspects of 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 ()

}

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 SyncData ()

{

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 (Configuration.GetSection ("ConfigSetting"))

Services.AddSingleton (Configuration)

Services.AddTransient ()

Services.AddTransient ()

Services.AddTransient ()

Services.AddTransient ()

Var serviceProvider = services.BuildServiceProvider ()

ServiceRunner.Run (config = >

{

Var name = config.GetDefaultName ()

Config.Service (serviceConfig = >

{

ServiceConfig.ServiceFactory ((extraArguments, controller) = >

{

Return new ServiceFactory (serviceProvider.GetService (), 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")

});

}

}

This is the end of the sample analysis on Dotnet Core Windows Service. I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report