In ASP.NET Core ConfigureServices Startup method you can do things such as
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidateIssuerSigningKey = true)) }; }); I want to do something like this on startup to configure my own dependency.
services.AddTest(options => new TestOptions { TestNumber = 8 }); This is my extension method that chains onto IServiceCollection...
public static IServiceCollection AddTest(this IServiceCollection services, Action<TestOptions> options) { services.AddScoped<ITest, Test>(); if (options != null) { services.Configure(options); } return services; } and my TestOptions is just a simple model...
public class TestOptions { public int? TestNumber { get; set; } } Then, finally I pass those options through to the constructor of the class I want to use them...
public Test (IConfiguration configuration, IOptions<TestOptions> options) { LocalTestNumber = options.Value.TestNumber; } However, when I hit the breakpoint on LocalTestNumber = options.Value.TestNumber the TestNumber is null and I'm not sure why, anyone have any insight on this?
I followed this tutorial to do this... https://codeburst.io/options-pattern-in-net-core-a50285aeb18d