来自 www.2257.com 2019-09-10 15:01 的文章
当前位置: 新萄京娱乐手机版 > www.2257.com > 正文

Dotnet Core Windows Service

现实的贯彻呢 作者举个例证,在例子中大家试用了个Timer,定时的姣好有个别任务,那样 大家就足以同不日常间写一些个service 只要继续 IBaseService 就行,也比较方面设置

_timer.AutoReset = true;
_timer.Enabled = false;
logger = loggerFactory.CreateLogger<SyncService>();
}

_timer.Enabled = true;

config.SetName("SAASService");
config.SetDescription("SAAS Service For All Saas Client");
config.SetDisplayName("SAAS Service");
});
}
}

_timer = new System.Timers.Timer(10000);
_timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

public void Start()
{
_timer.Start();
_timer.Enabled = true;
}
public void Stop()
{
_timer.Stop();
_timer.Enabled = false;
}
}

services.AddTransient<IBaseService, SyncService>();
services.AddTransient<IBaseService, CreateDBService>();
services.AddTransient<IBaseService, OnLineOrderService>();

_timer.Interval = 2000;

services.AddTransient<ILoggerFactory, LoggerFactory>();

通过Nuget来安装 Using nuget: Install-Package PeterKottas.DotNetCore.WindowsService

}
private async Task<HttpResponseMessage> SyncData()
{
string url = configModel.DatabaseIncrementUrl;
var httpClient = new HttpClient();
return await httpClient.GetAsync(url);
}

Thread.Sleep(5 * 60 * 1000);

方便多少个服务大家先定义一个接口

Configuration = builder.Build();

services.AddOptions();
services.Configure<ConfigModel>(Configuration.GetSection("ConfigSetting"));
services.AddSingleton<IConfiguration>(Configuration);

var serviceProvider = services.BuildServiceProvider();

private void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine(string.Format("SyncService:{0:yyyy-MM-dd HH:mm:sss}", DateTime.Now));
_timer.Enabled = false;

var services = new ServiceCollection();

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));

var builder = new ConfigurationBuilder()
.SetBasePath(Path.Combine(AppContext.BaseDirectory))
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables();

 

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();
});

class Program
{
static void Main(string[] args)
{

代码也要做相应的退换,具体的可以参见例子。

在dotnet core 2.0 中 大家也可以有三个很方便的dll 来试用 

在dotnet 中有topshelf 能够很有益的写windows 服务並且安装也是很便利的,命令行 运转.exe install 就直接把exe 程序安装成windows 服务。当然

serviceConfig.OnError(e =>
{
Console.WriteLine("Service {0} errored with exception : {1}", name, e.Message);
});
});

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

public class SyncService : IBaseService
{
private readonly System.Timers.Timer _timer;
private readonly ILogger logger;
public SyncService( ILoggerFactory loggerFactory)
{

IConfigurationRoot Configuration;
// ILoggerFactory LoggerFactory;

serviceConfig.OnStop(service =>
{
Console.WriteLine("Service {0} stopped", name);
service.Stop();
});

本文由新萄京娱乐手机版发布于www.2257.com,转载请注明出处:Dotnet Core Windows Service

关键词: