Open Closed

using MongoDB in microservice-service inside microservice solution which uses efcore #1269


User avatar
0
Neozzz created

We created a microservice solution using the following command: abp new ZW -t microservice-pro -u angular

Then we added a microservice-service to the solution using: abp new CompanyService -t microservice-service-pro

Now I want to use MongoDB on the newly added µservice-service and keep using the ef core for the main solution for identity and other related features.

I've already tried to add mongodb into CompanyService µservice-service. I am getting issues with

Please let me know if this is possible, and if so is there any best practice?

Thank you

  • ABP Framework version: v4.3
  • UI type: Angular
  • DB provider: EF Core & MongoDB

39 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi Neozzz

    You can completely disable transactions if your database doesn't support it.

    https://support.abp.io/QA/Questions/2176#answer-a0b36b39-5bdd-3259-a49e-3a005ca3118c

    https://github.com/abpframework/abp/pull/10658

  • User Avatar
    0
    Neozzz created

    hi maliming,

    this was my question: https://support.abp.io/QA/Questions/1269/using-MongoDB-in-microservice-service-inside-microservice-solution-which-uses-efcore#answer-3c384c74-c57f-d8c8-f71a-3a005a05e232

  • User Avatar
    0
    Neozzz created

    we have a microservice-pro solution that uses efcore. i need to add a microservice service inside this solution. but this has to use mongo for storing its entities but at the same time has to make use of the efcore for saas, admin etc.

    i have tried something, but while migrating we've to divide the concerns to both mongo and efcore. guess this is where am having the issue.

    please lemme know how to proceed.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Can you try to apply the changes of this PR to your mongodb?

    https://github.com/abpframework/abp/pull/10658/files

  • User Avatar
    0
    Neozzz created
    [DependsOn(
            typeof(PJNameSharedHostingMicroservicesModule),
            typeof(MySvcApplicationModule),
            typeof(MySvcHttpApiModule),
            typeof(MySvcMongoDbModule)
            )]
        public class MySvcHttpApiHostModule : AbpModule
        {
            public override void ConfigureServices(ServiceConfigurationContext context)
            {           
                context.Services.AddSingleton(typeof(UnitOfWorkManager));
                Configure<AbpUnitOfWorkDefaultOptions>(options =>
                {
                    options.TransactionBehavior = UnitOfWorkTransactionBehavior.Disabled;
                });
                JwtBearerConfigurationHelper.Configure(context, "MySvc");
                SwaggerConfigurationHelper.Configure(context, "MySvc API");
            }
    
            public override void OnApplicationInitialization(ApplicationInitializationContext context)
            {
                var app = context.GetApplicationBuilder();
                var env = context.GetEnvironment();
    
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
    
                app.UseCorrelationId();
                app.UseAbpRequestLocalization();
                app.UseStaticFiles();
                app.UseRouting();
                app.UseAuthentication();
                app.UseAbpClaimsMap();
                app.UseMultiTenancy();
                app.UseAuthorization();
                app.UseSwagger();
                app.UseSwaggerUI(options =>
                {
                    options.SwaggerEndpoint("/swagger/v1/swagger.json", "MySvc API");
                });
                app.UseAbpSerilogEnrichers();
                app.UseAuditing();
                app.UseUnitOfWork();
                app.UseConfiguredEndpoints();
            }
    
            public override void OnPostApplicationInitialization(ApplicationInitializationContext context)
            {
                using (var scope = context.ServiceProvider.CreateScope())
                {
                    AsyncHelper.RunSync(
                        () => scope.ServiceProvider
                            .GetRequiredService<MySvcDatabaseMigrationChecker>()
                            .CheckAsync()
                    );
                }
            }
        }
    
        [Dependency(ReplaceServices = true)]
        public class MyUnitOfWorkManager : IUnitOfWorkManager, ISingletonDependency
        {
            private readonly UnitOfWorkManager _innerUnitOfWorkManager;
    
            public MyUnitOfWorkManager(UnitOfWorkManager innerUnitOfWorkManager)
            {
                _innerUnitOfWorkManager = innerUnitOfWorkManager;
            }
    
            public IUnitOfWork Begin(AbpUnitOfWorkOptions options, bool requiresNew = false)
            {
                options.IsTransactional = false;
                return _innerUnitOfWorkManager.Begin(options, requiresNew);
            }
    
            public IUnitOfWork Reserve(string reservationName, bool requiresNew = false)
            {
                return _innerUnitOfWorkManager.Reserve(reservationName, requiresNew);
            }
    
            public void BeginReserved(string reservationName, AbpUnitOfWorkOptions options)
            {
                options.IsTransactional = false;
                _innerUnitOfWorkManager.BeginReserved(reservationName, options);
            }
    
            public bool TryBeginReserved(string reservationName, AbpUnitOfWorkOptions options)
            {
                options.IsTransactional = false;
                return _innerUnitOfWorkManager.TryBeginReserved(reservationName, options);
            }
    
            public IUnitOfWork Current => _innerUnitOfWorkManager.Current;
        }
    }
    
  • User Avatar
    0
    Neozzz created

    I have done the above changes as you suggested. But I am getting the following error. I'd request you to take a look at the code.

    2021-11-24 22:37:37.642 +04:00 [ERR] An exception was thrown while activating ZW.mySvc.DbMigrations.mySvcDatabaseMigrationEventHandler.
    Autofac.Core.DependencyResolutionException: An exception was thrown while activating ZW.mySvc.DbMigrations.mySvcDatabaseMigrationEventHandler.
     ---> Autofac.Core.DependencyResolutionException: None of the constructors found with 'Volo.Abp.Autofac.AbpAutofacConstructorFinder' on type 'ZW.mySvc.DbMigrations.mySvcDatabaseMigrationEventHandler' can be invoked with the available services and parameters:
    Cannot resolve parameter 'System.String databaseName' of constructor 'Void .ctor(Volo.Abp.MultiTenancy.ICurrentTenant, Volo.Abp.Uow.IUnitOfWorkManager, Volo.Abp.MultiTenancy.ITenantStore, Volo.Saas.Tenants.ITenantRepository, Volo.Abp.EventBus.Distributed.IDistributedEventBus, System.String, System.IServiceProvider)'.
       at Autofac.Core.Activators.Reflection.ReflectionActivator.GetAllBindings(ConstructorBinder[] availableConstructors, IComponentContext context, IEnumerable`1 parameters)
       at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters)
       at Autofac.Core.Activators.Reflection.ReflectionActivator.<ConfigurePipeline>b__11_0(ResolveRequestContext ctxt, Action`1 next)
       at Autofac.Core.Resolving.Middleware.DelegateMiddleware.Execute(ResolveRequestContext context, Action`1 next)
       at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext ctxt)
       at Autofac.Core.Resolving.Middleware.DisposalTrackingMiddleware.Execute(ResolveRequestContext context, Action`1 next)
       at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.&lt;&gt;c__DisplayClass14_0.&lt;BuildPipeline&gt;b__1(ResolveRequestContext ctxt)
       at Autofac.Builder.RegistrationBuilder`3.<>c__DisplayClass41_0.<PropertiesAutowired>b__0(ResolveRequestContext ctxt, Action`1 next)
       at Autofac.Core.Resolving.Middleware.DelegateMiddleware.Execute(ResolveRequestContext context, Action`1 next)
       at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext ctxt)
       at Autofac.Core.Resolving.Middleware.ActivatorErrorHandlingMiddleware.Execute(ResolveRequestContext context, Action`1 next)
       --- End of inner exception stack trace ---
    

    Please let me know how to proceed.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    Can you try this

    https://support.abp.io/QA/Questions/1269#answer-39041f27-8e29-5711-781e-3a0061b46232

  • User Avatar
    0
    Neozzz created

    I added the following:

    context.Services.AddSingleton(typeof(UnitOfWorkManager));
                context.Services.AddSingleton(typeof(UnitOfWorkMongoDbContextProvider<>));
                context.Services.AddSingleton(typeof(AbpMongoDbContextOptions));
    

    The error:

     [ERR] An exception was thrown while activating ZW.MySvc.DbMigrations.MySvcDatabaseMigrationEventHandler.
    Autofac.Core.DependencyResolutionException: An exception was thrown while activating ZW.MySvc.DbMigrations.MySvcDatabaseMigrationEventHandler.
     ---> Autofac.Core.DependencyResolutionException: None of the constructors found with 'Volo.Abp.Autofac.AbpAutofacConstructorFinder' on type 'ZW.MySvc.DbMigrations.MySvcDatabaseMigrationEventHandler' can be invoked with the available services and parameters:
    Cannot resolve parameter 'System.String databaseName' of constructor 'Void .ctor(Volo.Abp.MultiTenancy.ICurrentTenant, Volo.Abp.Uow.IUnitOfWorkManager, Volo.Abp.MultiTenancy.ITenantStore, Volo.Saas.Tenants.ITenantRepository, Volo.Abp.EventBus.Distributed.IDistributedEventBus, System.String, System.IServiceProvider)'.
       at Autofac.Core.Activators.Reflection.ReflectionActivator.GetAllBindings(ConstructorBinder[] availableConstructors, IComponentContext context, IEnumerable`1 parameters)
       at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters)
       at Autofac.Core.Activators.Reflection.ReflectionActivator.<ConfigurePipeline>b__11_0(ResolveRequestContext ctxt, Action`1 next)
       at Autofac.Core.Resolving.Middleware.DelegateMiddleware.Execute(ResolveRequestContext context, Action`1 next)
       at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext ctxt)
       at Autofac.Core.Resolving.Middleware.DisposalTrackingMiddleware.Execute(ResolveRequestContext context, Action`1 next)
       at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.&lt;&gt;c__DisplayClass14_0.&lt;BuildPipeline&gt;b__1(ResolveRequestContext ctxt)
       at Autofac.Builder.RegistrationBuilder`3.<>c__DisplayClass41_0.<PropertiesAutowired>b__0(ResolveRequestContext ctxt, Action`1 next)
       at Autofac.Core.Resolving.Middleware.DelegateMiddleware.Execute(ResolveRequestContext context, Action`1 next)
       at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext ctxt)
       at Autofac.Core.Resolving.Middleware.ActivatorErrorHandlingMiddleware.Execute(ResolveRequestContext context, Action`1 next)
    
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    https://github.com/abpframework/abp/pull/10658/files

  • User Avatar
    0
    Neozzz created

    after adding that only i added the DI code.

  • User Avatar
    0
    Neozzz created

    I had created a new class DatabaseMigrationEventHandlerBaseMongo for handling mongo db instead of DatabaseMigrationEventHandlerBase which uses ef core. It inherits from AbpMongoDbContext instead of AbpDbContext.

    I think this is causing the issue.

  • User Avatar
    0
    Neozzz created

    If you could check the code that'd be great. Taking remote would be easier for you as the infra is running and debugging may be easier. Let me know.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    I don't know about your project details, can you create a new project to reproduce the problem and share to me? shiwei.liang@volosoft.com thanks.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    The guide: https://github.com/abpio/abp-commercial-docs/pull/146

Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11