Base solution for your next web application
Open Closed

Db access within WebModule PostInitialize #9121


User avatar
0
PhilWynn created

Hi,

I have a requirement to perform a database query withing the web module. I have implemented this within PostInitialize as follows:

    public override void PostInitialize()
   {
            ...
            
            var idpConfigurationManager = IocManager.Resolve<IdpConfigurationManager>();

            var idps = idpConfigurationManager.GetAllIdps();
            ...
            
   }
        
    public class IdpConfigurationManager : IppexCloudDomainServiceBase
    {
        private readonly IRepository<IdpConfiguration> _idpConfigRepository;

        public IdpConfigurationManager(IRepository<IdpConfiguration> idpConfigRepository)
        {
            _idpConfigRepository = idpConfigRepository;
        }

        public IQueryable<IdpConfiguration> IdpConfigurations => _idpConfigRepository.GetAll();

        public IEnumerable<IdpConfiguration> GetAllIdps()
        {
            using (UnitOfWorkManager.Begin(new UnitOfWorkOptions {IsTransactional = false}))
            {
                    return IdpConfigurations.AsNoTracking().ToList();
            }
        }
    }
}
        

The code is working, however I am getting the following warning logged:

System.Web.HttpException (0x80004005): Request is not available in this context at System.Web.HttpContext.get_Request() at Abp.Web.MultiTenancy.HttpCookieTenantResolveContributor.ResolveTenantId() in D:\Github\aspnetboilerplate\src\Abp.Web\Web\MultiTenancy\HttpCookieTenantResolveContributor.cs:line 20 at Abp.MultiTenancy.TenantResolver.GetTenantIdFromContributors() in D:\Github\aspnetboilerplate\src\Abp\MultiTenancy\TenantResolver.cs:line 75

Please could you advise me as to whether or not this is an acceptible approach, and how to rid myself of the warning.

I am using Aspnet Zero MVC with ABP v4.5.0

Many thanks


11 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    If you want to get records of specific tenant or host, you can switch to Hosts or Tenants context using https://aspnetboilerplate.com/Pages/Documents/Multi-Tenancy#switching-between-host-and-tenants

  • User Avatar
    0
    PhilWynn created

    Hi,

    Sorry, but this does not answer my question.

    Because I am querying in module code, there is no UOW currently running, hence the need for me to call:

    using (UnitOfWorkManager.Begin(new UnitOfWorkOptions {IsTransactional = false})){
    ..
    }
    

    The query is on the Host data, so no switching to a tenancy is required.

    My questions are:

    Is it acceptable to code in this way (i.e. querying from the module code)? How can I rid myself of the warning I am getting (the warning appears 3 times when the GetAllIdps method runs)

    Thank you

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @philwynn,

    Is it acceptable to code in this way (i.e. querying from the module code)?

    Yes, accessing to DB in PostInitializeMethod is acceptable.

    How can I rid myself of the warning I am getting (the warning appears 3 times when the GetAllIdps method runs)

    I offered you to switch the context because for multi tenant entities, AspNet Zero tries to find TenantId using a few approaches and adds it to your database query. So, if you set tenantId to null in the unitOfWork you have created using UnitOfWorkManager.SetTenantId, this warning should be gone.

    Let me know if it doesn't.

  • User Avatar
    0
    PhilWynn created

    Hi,

    I have modified my code as follows:

    public IEnumerable<IdpConfiguration> GetAllActiveIdps()
            {
                using (UnitOfWorkManager.Begin(new UnitOfWorkOptions {IsTransactional = false}))
                {
                    UnitOfWorkManager.Current.SetTenantId(null);
    
                    return IdpConfigurations.AsNoTracking().Where(idp => idp.IsActive).ToList();
                }
            }
    

    However I am still getting the following warnings (3 warnings everytime the method executes):

    System.Web.HttpException (0x80004005): Request is not available in this context at System.Web.HttpContext.get_Request() at Abp.Web.MultiTenancy.DomainTenantResolveContributor.ResolveTenantId() in D:\Github\aspnetboilerplate\src\Abp.Web\Web\MultiTenancy\DomainTenantResolveContributor.cs:line 34 at Abp.MultiTenancy.TenantResolver.GetTenantIdFromContributors() in D:\Github\aspnetboilerplate\src\Abp\MultiTenancy\TenantResolver.cs:line 75

    Note that I am running ABP v4.5

  • User Avatar
    0
    maliming created
    Support Team

    hi Can you try the code below?

    var idpConfigurationManager = IocManager.Resolve<IdpConfigurationManager>();
    var UnitOfWorkManager =  IocManager.Resolve<IUnitOfWorkManager>();
    
    using (UnitOfWorkManager.Begin(new UnitOfWorkOptions {IsTransactional = false}))
    {
    	using(UnitOfWorkManager.Current.SetTenantId(null))
    	{
    		var idps = idpConfigurationManager.GetAllIdps();
    	}
    }
    
    
    
  • User Avatar
    0
    PhilWynn created

    Hi,

    I had already tried it that way. Unfortunately still the same result.

  • User Avatar
    0
    maliming created
    Support Team

    hi @philwynn

    Can you share a project to reproduce the problem?

    You can use Free Startup Templates or Zero's demo project.

    My email: [email protected]

  • User Avatar
    0
    PhilWynn created

    Hi,

    I have re-structured my code and this is no longer an issue.

    Thank you for your help

  • User Avatar
    0
    peopleteq created

    Hi everyone.

    @philwynn, can you please share your solution please? I need same functionality.

  • User Avatar
    0
    PhilWynn created

    @peopleteq

    Sorry, I have no solution to offer. I refactored my code so that I no longer needed to perform db access within module initialisation.

    However, there was never an issue with this, just an unwanted warning message.

    Phil

  • User Avatar
    0
    peopleteq created

    @philwynn

    Thanks for reply. I used UoF in Preinitialize and that works for me