日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > windows >内容正文

windows

Dotnet Core Windows Service

發(fā)布時間:2023/12/4 windows 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Dotnet Core Windows Service 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在dotnet 中有topshelf 可以很方便的寫windows 服務(wù)并且安裝也是很方便的,命令行 運行.exe install 就直接把exe 程序安裝成windows 服務(wù)。當(dāng)然代碼也要做相應(yīng)的修改,具體的可以參照例子。

在dotnet core 2.0 中 我們也有一個很方便的dll 來使用?

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

通過Nuget來安裝 :?Install-Package PeterKottas.DotNetCore.WindowsService

方便多個服務(wù)我們先定義一個接口

public interface IBaseService
{
void Start();
void Stop();
}

具體的實現(xiàn)呢 我舉個例子,在例子中我們試用了個Timer,定時的完成某些任務(wù),這樣 我們就可以同時寫好幾個service 只要繼續(xù)?IBaseService 就行,也比較方面安裝

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<HttpResponseMessage> 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<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");
});
}
}

原文地址:http://www.cnblogs.com/jfliuyun/p/8242945.html


.NET社區(qū)新聞,深度好文,歡迎訪問公眾號文章匯總 http://www.csharpkit.com

總結(jié)

以上是生活随笔為你收集整理的Dotnet Core Windows Service的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。