Base solution for your next web application
Open Closed

IConnectionStringResolver with ABP 4.1.0 #6301


User avatar
0
schneana created

Dear Support

Sorry, I am sure I am overlooking something but in 4.0.2 I used in the ApbModule:

public override void PreInitialize() { if (!SkipDbContextRegistration) { Configuration.ReplaceService<IConnectionStringResolver, myConnectionStringResolver>(); ...

but this is not working anymore (I assume the ReplaceService notation changed in AbpModule). Can you please point me to the correcty way to use an own IConnectionStringResolver (ASP.NET MVC CORE + JQUERY) ?

Kind regards Andreas

<br> <br>


6 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    I have not reproduced your question, please share more details and code.

  • User Avatar
    0
    schneana created

    Dear Maliming

    Please as follow, the code is in the cwadmin.EntityFrameworkCore project, EntityFrameworkCore folder:

    [DependsOn(
        typeof(AbpZeroCoreEntityFrameworkCoreModule),
        typeof(cwadminCoreModule),
        typeof(AbpZeroCoreIdentityServerEntityFrameworkCoreModule)
        )]
    public class cwadminEntityFrameworkCoreModule : AbpModule
    {
        /* Used it tests to skip dbcontext registration, in order to use in-memory database of EF Core */
        public bool SkipDbContextRegistration { get; set; }
    
        public bool SkipDbSeed { get; set; }
    
        public override void PreInitialize()
        {
            if (!SkipDbContextRegistration)
            {
                Configuration.ReplaceService&lt;IConnectionStringResolver, myConnectionStringResolver&gt;();
    
                Configuration.Modules.AbpEfCore().AddDbContext&lt;cwadminDbContext&gt;(options =>
                {
                    if (options.ExistingConnection != null)
                    {
                        cwadminDbContextConfigurer.Configure(options.DbContextOptions, options.ExistingConnection);
                    }
                    else
                    {
                        cwadminDbContextConfigurer.Configure(options.DbContextOptions, options.ConnectionString);
                    }
                });
    
                Configuration.Modules.AbpEfCore().AddDbContext&lt;cwTopologySwitchDbContext&gt;(options =>
                {
                    if (options.ExistingConnection != null)
                    {
                        cwTopologySwitchDbContextConfigurer.Configure(options.DbContextOptions, options.ExistingConnection);
                    }
                    else
                    {
                        cwTopologySwitchDbContextConfigurer.Configure(options.DbContextOptions, options.ConnectionString);
                    }
                });
    
                Configuration.Modules.AbpEfCore().AddDbContext&lt;cwCauDbContext&gt;(options =>
                {
                    if (options.ExistingConnection != null)
                    {
                        cwCauDbContextConfigurer.Configure(options.DbContextOptions, options.ExistingConnection);
                    }
                    else
                    {
                        cwCauDbContextConfigurer.Configure(options.DbContextOptions, options.ConnectionString);
                    }
                });
    
    
                Configuration.Modules.AbpEfCore().AddDbContext&lt;cwClientDbContext&gt;(options =>
                {
                    if (options.ExistingConnection != null)
                    {
                        cwClientDbContextConfigurer.Configure(options.DbContextOptions, options.ExistingConnection);
                    }
                    else
                    {
                        cwClientDbContextConfigurer.Configure(options.DbContextOptions, options.ConnectionString);
                    }
                });
            }
    
            // Uncomment below line to write change logs for the entities below:
            Configuration.EntityHistory.Selectors.Add("cwadminEntities", EntityHistoryHelper.TrackedTypes);
            Configuration.CustomConfigProviders.Add(new EntityHistoryConfigProvider(Configuration));
        }
    
        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(typeof(cwadminEntityFrameworkCoreModule).GetAssembly());
        }
    
        public override void PostInitialize()
        {
            var configurationAccessor = IocManager.Resolve&lt;IAppConfigurationAccessor&gt;();
    
            using (var scope = IocManager.CreateScope())
            {
                if (!SkipDbSeed && scope.Resolve&lt;DatabaseCheckHelper&gt;().Exist(configurationAccessor.Configuration["ConnectionStrings:Default"]))
                {
                    SeedHelper.SeedHostDb(IocManager);
                }
            }
        }
    }
    

    It follows the recomendation for multiple databases found in https://stackoverflow.com/questions/49243891/how-to-use-multiples-databases-in-abp-core-zero/49572121#49572121

    Kind regards Andreas

  • User Avatar
    0
    maliming created
    Support Team

    Try Resolve IConnectionStringResolver under the PostInitialize method to see if it is correct?

    var connectionStringResolver = IocManager.Resolve<IConnectionStringResolver>();

    public override void PostInitialize()
    {
        var configurationAccessor = IocManager.Resolve<IAppConfigurationAccessor>();
        var connectionStringResolver = IocManager.Resolve<IConnectionStringResolver>();
        // ...
    }
    
  • User Avatar
    0
    schneana created

    Dear Maliming

    Thank you for your feedback. Sorry, maybe my question was missleading. I would like to understand how I should register my own implementation if IConnectionStringResolver because Configuration.ReplaceService<IConnectionStringResolver, myConnectionStringResolver>(); does not work anymore with APB 4.1.0.

    Kind regards Andreas

  • User Avatar
    0
    maliming created
    Support Team

    hi schneana.

    abp v4.1 did not modify the features you mentioned. https://github.com/aspnetboilerplate/aspnetboilerplate/releases/tag/v4.1.0

  • User Avatar
    0
    schneana created

    Dear Maliming

    Many thanks for your feedback and for the link. You are right, investigating from the link I could trace down the issue. As usual the issue was infront of the keyboard, in this case I had a reference to Abp.Configuration.Startup. AbpStartupConfigurationExtensions was therefore not available togeter with the extesion on .ReplaceService<,>.

    Again many thanks for your swift response.

    Kind regard Andreas