成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

DotnetCoreWindowsService的示例分析

這篇文章將為大家詳細講解有關Dotnet Core Windows Service的示例分析,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領域值得信任、有價值的長期合作伙伴,公司提供的服務項目有:空間域名、網(wǎng)絡空間、營銷軟件、網(wǎng)站建設、石林網(wǎng)站維護、網(wǎng)站推廣。

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

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

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

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

方便多個服務我們先定義一個接口

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

具體的實現(xiàn)呢 我舉個例子,在例子中我們試用了個Timer,定時的完成某些任務,這樣 我們就可以同時寫好幾個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");
});
}
}

關于Dotnet Core Windows Service的示例分析就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

新聞標題:DotnetCoreWindowsService的示例分析
本文網(wǎng)址:http://jinyejixie.com/article6/jpdsog.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供微信小程序、ChatGPT企業(yè)網(wǎng)站制作、小程序開發(fā)、企業(yè)建站建站公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務器托管
上高县| 井研县| 奎屯市| 昂仁县| 泸水县| 雷山县| 翁源县| 花莲市| 兴安盟| 蒙城县| 衢州市| 正镶白旗| 正阳县| 治县。| 崇阳县| 蒙城县| 马公市| 红安县| 唐山市| 盈江县| 衡阳县| 襄垣县| 社旗县| 镇安县| 杭锦后旗| 吴旗县| 泰兴市| 济南市| 天全县| 太白县| 岫岩| 依兰县| 临汾市| 苏尼特右旗| 罗平县| 措美县| 正宁县| 石泉县| 永川市| 诏安县| 绥中县|