Open Closed

Switching Context #1219


User avatar
0
MarekH created
  • ABP Framework version: v4.3.0
  • UI type: Angular
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): yes
  • Exception message and stack trace:
  • Steps to reproduce the issue:

Hello, we are trying to resolve issues related to switching between host and customer DBs.

Our purpose :- Insert,Update and Get data as per the connectionString of tenant from Host layer API as well as from newly created Microservice layer API

I was trying to switch the database using connection string So I added one method ContextFactory in DbContext.cs file(EntityFrameworkCore layer)

Create a sample API in Demo.HttpApi.Host layer and Call the ContextFactory method which returns a given connectionString context object.

Using that object i am able to access all the tables but getting error 'Object reference not set to an instance of an object'

My question is:how can we change the context and get the data from Host layer API as well as Microservice API ?


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

    hi

    If your tenant has a separate connection string, you should switch the tenant of the current context to switch the DbContext using a different connection string.

    https://docs.abp.io/en/abp/latest/Multi-Tenancy#change-the-current-tenant

    You should not create DbCotext manually, abp will manage it automatically.

  • User Avatar
    0
    MarekH created

    hi

    If your tenant has a separate connection string, you should switch the tenant of the current context to switch the DbContext using a different connection string.

    https://docs.abp.io/en/abp/latest/Multi-Tenancy#change-the-current-tenant

    You should not create DbCotext manually, abp will manage it automatically.

    Hello Maliming,

    Thanks for giving above answer its helpful to us.

    I have check that we are able to switch the tenant by using CurrentTenant.Change() method in HttpApi.Host Layer you can see the below image.It's working

    I have tried the same way with micro-service (Create new micro-service) and write similar code which is in above image and call the api. At that time I am not able to call api, it gives a 500 error. I have also added reference of Application layer and added related reference project.

    Can we access the Repository of any table in micro-service ? Please provide some step for access the table repository in micro-service

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    it gives a 500 error.

    Can you share the error logs?

  • User Avatar
    0
    MarekH created

    it gives a 500 error.

    Can you share the error logs?

    Hello maliming,

    I have downloaded a sample code from Abp.io and added a microservice project named FD.Demo.MicroService.Host to it. I have created PostGreController in V1 folder of controller folder inside FD.Demo.MicroService.Host project. Now that controller contains a method test(API) which calls IAppUserRepository method (GetListAsync for get the list of users) in FD.Demo.Domain project and returns the list of users. When executing Test API of PostGreController from FD.Demo.MicroService.Host, swagger is showing 500 error. I have attached sample project file which is given below.

    Steps:- 1)download the below project 2)Execute the microservice Test api

    Project Link:- deleted

    Our Purpose is to Change Tenant using CurrentTenant.Change() in MicroService. We are not able to access the Repository in MicroService Layer.We have added the Reference of FD.Demo.Domain for accessing Repository of AppUser. We are not getting any compile time error however we are not able to access the Repository on MicroService API.

    Can you please check above example and let me know how can I Access the reposity in MicroService Layer.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Please share the proejct via email liming.ma@volosoft.com

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Please check PostGreController in FD.Demo.MicroService.Host project. We are trying to access appuserRepository for specific tenant but without any luck.

    You can use CurrentTenant.Change to switch host and tenant.

    [ApiController]
    public class PostGreController : AbpController
    {
    
        private readonly IAppUsers _appuserRepository;
        private readonly IDbContextProvider<DemoDbContext> _dbContextProvider;
    
        public PostGreController(IAppUsers appuserRepository, IDbContextProvider<DemoDbContext> dbContextProvider)
        {
            _appuserRepository = appuserRepository;
            _dbContextProvider = dbContextProvider;
        }
    
    
        [Route("/api/v1/test")]
        [HttpGet]
        [ProducesResponseType((int)HttpStatusCode.Accepted)]
        public async Task<IActionResult> test()
        {
            using (CurrentTenant.Change(null))
            {
                var abcInHostDatabase = await _appuserRepository.GetListAsync();
            }
    
            var ten1 = new Guid("your tenant id to switch tenant");
            using (CurrentTenant.Change(ten1))
            {
                var abcInTenantDatabase = await _appuserRepository.GetListAsync();
            }
    
    
            var demoDbContext = await _dbContextProvider.GetDbContextAsync();
    
            return Ok();
        }
    }
    

    Our general issue is, how to access DemoDBContext from FD.Demo.Microservice.Host project.

    Reference FD.Demo.EntityFrameworkCore.csproj in your FD.Demo.Microservice.Host project.

    var demoDbContext = await _dbContextProvider.GetDbContextAsync();

  • User Avatar
    0
    MarekH created

    hi

    Please check PostGreController in FD.Demo.MicroService.Host project. We are trying to access appuserRepository for specific tenant but without any luck.

    You can use CurrentTenant.Change to switch host and tenant.

    [ApiController] 
    public class PostGreController : AbpController 
    { 
     
        private readonly IAppUsers _appuserRepository; 
        private readonly IDbContextProvider<DemoDbContext> _dbContextProvider; 
     
        public PostGreController(IAppUsers appuserRepository, IDbContextProvider<DemoDbContext> dbContextProvider) 
        { 
            _appuserRepository = appuserRepository; 
            _dbContextProvider = dbContextProvider; 
        } 
     
     
        [Route("/api/v1/test")] 
        [HttpGet] 
        [ProducesResponseType((int)HttpStatusCode.Accepted)] 
        public async Task<IActionResult> test() 
        { 
            using (CurrentTenant.Change(null)) 
            { 
                var abcInHostDatabase = await _appuserRepository.GetListAsync(); 
            } 
     
            var ten1 = new Guid("your tenant id to switch tenant"); 
            using (CurrentTenant.Change(ten1)) 
            { 
                var abcInTenantDatabase = await _appuserRepository.GetListAsync(); 
            } 
     
     
            var demoDbContext = await _dbContextProvider.GetDbContextAsync(); 
     
            return Ok(); 
        } 
    } 
    

    Our general issue is, how to access DemoDBContext from FD.Demo.Microservice.Host project.

    Reference FD.Demo.EntityFrameworkCore.csproj in your FD.Demo.Microservice.Host project.

    var demoDbContext = await _dbContextProvider.GetDbContextAsync();

    Hello Maliming,

    As per your reply, I tried giving reference of FD.Demo.EntityFrameworkCore.csproj to my FD.Demo.Microservice.Host project but I am still getting the same error. Can we please schedule a call?

  • User Avatar
    0
    gterdem created
    Senior .NET Developer

    Hello Maliming,

    As per your reply, I tried giving reference of FD.Demo.EntityFrameworkCore.csproj to my FD.Demo.Microservice.Host project but I am still getting the same error. Can we please schedule a call?

    Can you double check if you add the DependsOn attribute in the FDDemoMicroserviceHostModule as well?

  • User Avatar
    0
    MarekH created

    Yes We have added DependsOn attribute in FDDemoMicroserviceHostModule. We are still not able to access the dbContext.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Send me an email when you're available, I can remote check it again.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    I back the Question Credits to you, you can open a new quesion.

Made with ❤️ on ABP v9.1.0-preview. Updated on November 19, 2024, 12:56