C# Quartz.NET IOptionsSnapshot

C# Quartz.NET IOptionsSnapshot,c#,dependency-injection,quartz.net,C#,Dependency Injection,Quartz.net,目标是能够更改appsettings.json中的设置,并让使用此值的作业自动反映更改并使用新值,但我就是无法让它工作。以下是我的最新尝试: appsettings.json "Settings": { "GameServerVersion": 15 }, 设置.cs public class Settings { public int GameServerVersion { get; set; } } Startup.cs public class Startup {

目标是能够更改
appsettings.json中的设置,并让使用此值的作业自动反映更改并使用新值,但我就是无法让它工作。以下是我的最新尝试:

appsettings.json

"Settings": {
    "GameServerVersion": 15
},
设置.cs

public class Settings
{
    public int GameServerVersion { get; set; }
}
Startup.cs

public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables();
        this.Configuration = builder.Build();
    }

    public IConfigurationRoot Configuration { get; }

    private IJobManager JobManager { get; set; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddMvc();
        services.AddCors();
        services.AddOptions();

        services.Configure<Settings>(this.Configuration.GetSection("Settings"));
        services.AddSingleton<ServersJob>();
        services.AddSingleton<JobFactory>();
        services.AddSingleton<IJobManager, JobManager>();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IApplicationLifetime applicationLifetime, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {

        app.UseCors(builder => builder.AllowAnyOrigin());
        app.UseLogger();
        app.UseMvc();

        this.JobManager = app.ApplicationServices.GetService<IJobManager>();
        this.JobManager.StartAsync();
    }
Serversjob.cs

public readonly Settings settings;

public ServersJob(IOptionsSnapshot<Settings> options)
{
    settings = options.Value;
}

public async Task Execute(IJobExecutionContext context)
{
    int gameServerVersion = settings.GameServerVersion;
    return;
}
公共只读设置;
公用服务器作业(IOptionsSnapshot选项)
{
设置=选项。值;
}
公共异步任务执行(IJobExecutionContext上下文)
{
int gameServerVersion=settings.gameServerVersion;
返回;
}

这很好,但作业没有反映所做的任何更改。我是否错过了明显的(可能的)答案?提前感谢。

我是否理解作业没有改变价值是正确的?为什么从
服务器job执行(IJobExecutionContext)
是异步的?作业已经在他自己的线程/任务中运行,不需要异步它。这是正确的,值不会更改。我使用一个控制器进行了测试,正如在web上的许多示例中所宣传的那样,它工作得很好。只是作业无法使用更新的值。任务是
async
,因为它包含对
async
函数的调用。这有区别吗?好的,但是您的
Execution()
方法没有显示任何异步调用。我猜Quartz不能处理您想要的异步任务,我看不出有任何必要。作业已在其自己的线程/任务中执行。只需从方法定义中删除
异步任务
。我想这应该能解决你的问题。如果没有,请尝试调试并观察它是否真的执行了更改。@Rabban我没有提供
Execute()
函数的完整代码,完整版本包含HTML请求并运行良好。您是否删除了
async Task
并在没有它的情况下尝试了它?我是否理解作业没有更改值是正确的?为什么从
服务器job执行(IJobExecutionContext)
是异步的?作业已经在他自己的线程/任务中运行,不需要异步它。这是正确的,值不会更改。我使用一个控制器进行了测试,正如在web上的许多示例中所宣传的那样,它工作得很好。只是作业无法使用更新的值。任务是
async
,因为它包含对
async
函数的调用。这有区别吗?好的,但是您的
Execution()
方法没有显示任何异步调用。我猜Quartz不能处理您想要的异步任务,我看不出有任何必要。作业已在其自己的线程/任务中执行。只需从方法定义中删除
异步任务
。我想这应该能解决你的问题。如果没有,请尝试调试并观察它是否真的执行了更改。@Rabban我没有提供
Execute()
函数的完整代码,完整版本包含HTML请求并运行良好。您是否删除了
async Task
并在没有它的情况下进行了尝试?
public class JobFactory : IJobFactory
{
    private readonly IServiceProvider Container;

    public JobFactory(IServiceProvider container)
    {
        this.Container = container;
    }

    public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
    {
        return this.Container.GetService(bundle.JobDetail.JobType) as IJob;
    }

    public void ReturnJob(IJob job)
    {
        var disposable = job as IDisposable;
        if (disposable != null)
        {
            disposable.Dispose();
        }
    }
}
public readonly Settings settings;

public ServersJob(IOptionsSnapshot<Settings> options)
{
    settings = options.Value;
}

public async Task Execute(IJobExecutionContext context)
{
    int gameServerVersion = settings.GameServerVersion;
    return;
}