亚洲必赢手机入口Dotnet Core Windows ServiceDotnet Core Windows Service

by admin on 2018年10月5日

于dotnet 中生出topshelf 可以很便宜的写windows
服务又安装为是坏有益于之,命令行 运行.exe install 就一直把exe
程序安装成windows 服务。当然

当dotnet 中出topshelf 可以死方便之写windows
服务而安装为是十分便宜的,命令行 运行.exe install 就直拿exe
程序安装成windows 服务。当然

代码也要召开相应的改动,具体的可参考例子。

代码也只要做相应的改动,具体的可以参考例子。

当dotnet core 2.0 中 我们啊起一个良有益之dll 来试用 

每当dotnet core 2.0 中 我们呢来一个怪便宜的dll 来试用 

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

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

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

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

便民多单服务我们先定义一个接口

有利多只服务我们事先定义一个接口

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

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

切实的兑现呢
我举个例证,在例子中我们试用了只Timer,定时的得某些任务,这样
我们即便足以而且写一些只service 只要继续 IBaseService 就实施,也于方面设置

实际的实现呢
我推个例子,在例子中我们试用了个Timer,定时的成功某些任务,这样
我们即便可而且写一些单service 只要继续 IBaseService 就行,也比方面设置

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

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 = new System.Timers.Timer(10000);
_timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

_timer.Interval = 2000;

_timer.Interval = 2000;

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

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

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

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

Thread.Sleep(5 * 60 * 1000);

_timer.Enabled = true;

_timer.Enabled = true;

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

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

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

 

 

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

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

IConfigurationRoot Configuration;
// ILoggerFactory LoggerFactory;

IConfigurationRoot Configuration;
// ILoggerFactory LoggerFactory;

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

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

Configuration = builder.Build();

Configuration = builder.Build();

var services = new ServiceCollection();

var services = new ServiceCollection();

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

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

services.AddTransient<ILoggerFactory, LoggerFactory>();

services.AddTransient<ILoggerFactory, LoggerFactory>();

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

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

var serviceProvider = services.BuildServiceProvider();

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

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.OnStop(service =>
{
Console.WriteLine(“Service {0} stopped”, name);
service.Stop();
});

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

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

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

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图