NopCommerce 4.2 Plugin - How to resolve dependencies and get settings in Startup

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
4 years ago
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
authService
is 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.
4 years ago
I am also confused on the order of dependency injection.  I have been using NopCommerce but I did notice that There is not a ContainerManager in the 4.20 version and the Initialize is also missing in 4.20.  I should make the assumption that this is handled in the Startup of an Application.  Does the 4.20 framework require building a customized DependencyRegistar to handle initializing the DI?
4 years ago
RobbyeRob wrote:
I am also confused on the order of dependency injection.  I have been using NopCommerce but I did notice that There is not a ContainerManager in the 4.20 version and the Initialize is also missing in 4.20.  I should make the assumption that this is handled in the Startup of an Application.  Does the 4.20 framework require building a customized DependencyRegistar to handle initializing the DI?


The Plugin tutorial I was following detailed how to create a custom DependencyRegistrar - example in my code above. I believe nopCommerce uses Reflection to include any IDependencyRegistrar implementations in the DI container.

The problem I have is the timing of when this happens, as I need to call my auth service in the Startup class in order to put the JWT auth in to the pipeline, but the DI container hasn't been initialsed at that point so I can't resolve my auth service.

I can't believe this problem hasn't already been solved, or maybe I'm doing something fundamentally wrong.
4 years ago
So initially what I did to get the DependencyRegister to load was to do exactly what the Web Site did.  The Web Site Project has a Startup.cs and Program.cs that contains everything you need to load the class.   My case is a little different in the fact that I created a console application to load a WEB API class library and then I used the Startup class of the console app to set an IConfiguration and IHostingEnvironment.  These are loaded in the ConfigurationServices collection through the public IServiceProvider ConfigureServices (IServiceCollection services) method signature.   Return services.ConfigureApplicationServices(configuration, hostingenvironment).  This uses the Static Extension for the ServiceCollectionExtensions class in the Nop.Web.Framework.Infrastructure.Extensions namespace to load all of the Nop Commerce services.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.