Hi. I'm developing a plugin which includes a REST API. In my startup class I want to call my auth service to attach the JWT pipeline:
public class ApiPluginStartup : INopStartup
{
private readonly IApiAuthService authService;
public ApiPluginStartup(IApiAuthService authService)
{
this.authService = authService;
}
/// <summary>
/// Add and configure any of the middleware
/// </summary>
/// <param name="services">Collection of service descriptors</param>
/// <param name="configuration">Configuration of the application</param>
public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
authService.Configure(services);
}
/// <summary>
/// Configure the using of added middleware
/// </summary>
/// <param name="application">Builder for configuring a n application's request pipeline</param>
public void Configure(IApplicationBuilder application)
{
}
/// <summary>
/// Gets order of this startup configuration implementation
/// </summary>
public int Order => new AuthenticationStartup().Order + 1;
}
The problem I have is that at this point, my DependencyRegistrar has not been called so the DI does not resolve and
authServiceis null. What's the correct way to get that dependency this early in the application?
Secondly - how can I read the settings from the database to initialise the auth service? At the moment I have a hard coded secret and token timeout :
public class DependencyRegistrar : IDependencyRegistrar
{
/// <summary>
/// Register services and interfaces
/// </summary>
/// <param name="builder">Container builder</param>
/// <param name="typeFinder">Type finder</param>
/// <param name="config">Config</param>
public void Register(ContainerBuilder builder, ITypeFinder typeFinder, NopConfig config)
{
//register custom services
builder.RegisterType<ApiAuthService>().As<IApiAuthService>()
.WithParameter("jwtSecret", "IveGotTheKeyIveGotTheSecret")
.WithParameter("jwtExpiryHours", 100)
.InstancePerLifetimeScope();
}
/// <summary>
/// Order of this dependency registrar implementation
/// </summary>
public int Order => 2;
}
The secret and expiry hours are in my database settings for the shop but I have the same problem as in the first question - at the point this gets called, the DI has not been set up yet so I can't resolve a settings object to access the settings.
Apologies for the noob questions, any help appreciated.