Chris Satchell
Chris Satchell

Reputation: 749

Configuration .NET Core

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#.

Upvotes: 1

Views: 487

Answers (1)

Evk
Evk

Reputation: 101633

It's better to not perform any static initialization (like static RunAsync in your case) when you use dependency injection container. Instead, register JobManager itself (preferrably through interface) in container (assuming you pass IOptions though constructor as in your example):

services.AddSingleton<JobManager>();

Then, later, for example in Configure method in your Startup class, resolve it and start:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {
    var jm = app.ApplicationServices.GetService<JobManager>();
    jm.StartAsync();
    // etc
}

If you will need to use JobManager in any controller - just pass it though constructor - it will be resolved automatically.

Upvotes: 3

Related Questions