Base solution for your next web application
Open Closed

DependsOn with ctor injection #5441


User avatar
0
ivanosw1 created

Hi, I've two plugin modules.

The first one is composed by two dll. One shared with the interface, and one other with the concrete implementation.

REngine -> REngineModule -> REngine : IEngine RShared -> RSharedModule -> IEngine

The second module need to register some stuffs to REngine on PostInitialize. I've used the DependsOn attribute but PModule is loaded earlier then RSharedModule and I've the exception: PModule is waiting for the following dependencies: IEngine

[DependsOn(typeof(RSharedModule))]
    public class PModule : AbpModule
    {
        private readonly IEngine _engine;

        public PModule(IEngine engine)
        {
            _engine = engine;
        }

       
        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(typeof(PModule).GetAssembly());
        }

        public override void PostInitialize()
        {
          _engine.Register(new PicSource);
  
        }
    }

If I use IocManager.Resolve<IEngine>() all works.

So, the question is: How can I tell to abp to instantiate one module before others?

Thank you.


4 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    How can I tell to abp to instantiate one module before others?

    By specifying the DependsOn attribute, which you already do.

    Instantiating is different from initialising. This is the flow:

    • All modules get instantiated.
    • All modules get pre-initialized.
    • All modules get initialized.
    • All modules get post-initialized.

    In each step, the specification of DependsOn is respected. However, dependencies are usually registered during initialization (step 3).

    Therefore, you cannot constructor-inject dependencies. You should resolve dependencies from other modules during post-initialization (step 4).

  • User Avatar
    0
    ivanosw1 created

    Ok, the case you say works for me but I've found the ctro approach in your zero code in Test project ,Migrator project, Web core. Are all these special cases?

    Test module:

    [DependsOn(
            typeof(FrameworkApplicationModule),
            typeof(FrameworkEntityFrameworkCoreModule),
            typeof(AbpTestBaseModule))]
        public class FrameworkTestModule : AbpModule
        {
            public FrameworkTestModule(FrameworkEntityFrameworkCoreModule abpZeroTemplateEntityFrameworkCoreModule)
            {
                abpZeroTemplateEntityFrameworkCoreModule.SkipDbContextRegistration = true;
            }
    

    WebCore

    public class FrameworkWebCoreModule : AbpModule
        {
            private readonly IHostingEnvironment _env;
            private readonly IConfigurationRoot _appConfiguration;
    
            public FrameworkWebCoreModule(IHostingEnvironment env)
            {
                _env = env;
                _appConfiguration = env.GetAppConfiguration();
            }
    

    Migrator module:

    [DependsOn(typeof(FrameworkEntityFrameworkCoreModule))]
        public class FrameworkMigratorModule : AbpModule
        {
            private readonly IConfigurationRoot _appConfiguration;
    
            public FrameworkMigratorModule(FrameworkEntityFrameworkCoreModule abpZeroTemplateEntityFrameworkCoreModule)
            {
                abpZeroTemplateEntityFrameworkCoreModule.SkipDbSeed = true;
    
                _appConfiguration = AppConfigurations.Get(
                    typeof(FrameworkMigratorModule).GetAssembly().GetDirectoryPathOrNull()
                );
            }
    
  • User Avatar
    0
    aaron created
    Support Team

    Yes. Observe that they are injecting modules, or IHostingEnvironment (which is registered by ASP.NET Core).

  • User Avatar
    0
    ivanosw1 created

    Ok, this make sense. Thank you @aaron.