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

自定义配置文件

第一增多一个.json 文件,譬喻 setting.json 文件内容如下,记得把文件设置为“复制到输出目录”

{
"ConfigSetting": {
"XXXName": "Name",
"XXXValue": "Value"
}
}

 

下来定义多个经受Model 

public class SettingModel
{
public string XXXName { get; set; }

public string XXXValue { get; set; }
}

在 Startup.cs 的格局中 public void ConfigureServices(IServiceCollection services) 增添上边包车型地铁挂号事件

 services.Configure<SettingModel>(Configuration.GetSection("ConfigSetting"));

 

接下来在任务Controller 可能是劳务中就能够不荒谬使用,使用方法见上边玉栗褐部分:

public class SyncService : IBaseService
{

private readonly System.Timers.Timer _timer;
private readonly ILogger logger;
private readonly SettingModel settingModel;
public SyncService(IOptions<SettingModel> settingOptons, ILoggerFactory loggerFactory)
{
this.settingModel = settingOptons.Value;
_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);

_www.2257.com,timer.Enabled = true;

}

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

本文由新萄京娱乐手机版发布于www.2257.com,转载请注明出处:自定义配置文件

关键词: