So I've been following this guide, but all examples are either accessing the options in the same method in which they were declared or in a controller. For the life of me, I can't figure out how to get it to work with any random class.
Here's my current code:
appsettings.json
{
"Settings": {
"GameServerVersion": 14
},
....
}
Settings.cs
public class Settings
{
public int GameServerVersion { get; set; }
}
Startup.cs
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; }
// 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"));
JobManager.StartAsync();
}
JobManager.cs
public class JobManager
{
private static IScheduler scheduler;
public static async void StartAsync()
{
// Quartz.NET jobs
// Setup scheduler, register jobs, etc.
// Trying to retrieve ```Settings.GameServerVersion``` to pass it to one of the jobs
}
}
Now the guide suggests I should be using the following (applied to my example):
private readonly Settings settings;
public JobManager(IOptions<Settings> settingsAccessor)
{
settings = settingsAccessor.Value;
}
And this is where I start to get lost. I understand I could just use the following, but I don't know what or how to pass the relevant parameter to the constructor.
JobManager jm = new JobManager(); // cannot use an empty constructor obviously
jm.StartAsync(); // no longer a static method
Any help or guidance would be much appreciated, as I'm still very new to C#.
services.AddSingletonor similar) and resolve it from there instead of manually constructing.