Base solution for your next web application
Open Closed

Service 'Microsoft.AspNetCore.Hosting.IHostingEnvironment' which was not registered. #10406


User avatar
0
bulutyonetim created

Prerequisites

Please answer the following questions before submitting an issue. YOU MAY DELETE THE PREREQUISITES SECTION.

-What is your product version? 10.3 -What is your product type (Angular or MVC)? Angular -What is product framework type (.net framework or .net core)? .netCore

If issue related with ABP Framework

  • What is ABP Framework version? 6.3.1

Hi I have a ConnectionStringResolver like this:

` public class ProjectNameConnectionStringResolver : DefaultConnectionStringResolver { private readonly IConfigurationRoot _appConfiguration; private readonly IAmbientDataContext _ambientDataContext;

    public ProjectNameConnectionStringResolver(IAbpStartupConfiguration configuration, IHostingEnvironment hostingEnvironment, IAmbientDataContext ambientDataContext)
        : base(configuration)
    {
        _appConfiguration = AppConfigurations.Get(hostingEnvironment.ContentRootPath, hostingEnvironment.EnvironmentName);
        _ambientDataContext = ambientDataContext;
    }

    public override string GetNameOrConnectionString(ConnectionStringResolveArgs args)
    {
        if ((string)_ambientDataContext.GetData(ProjectNameConsts.DatabaseSelection) == ProjectNameConsts.SelectedDatabase.ReadDatabase)
        {
            string xx = _appConfiguration.GetConnectionString(ProjectNameConsts.ReadReplicaConnectionStringName);
            return xx;
        }

        return base.GetNameOrConnectionString(args);
    }
}

And it has been register like this in EntityFrameworkCoreModule:

            Configuration.ReplaceService(typeof(IConnectionStringResolver), () =>
            {
                IocManager.Register<IConnectionStringResolver, PuduxConnectionStringResolver>(DependencyLifeStyle.Transient);
            });

Everything works fine. but when I try to run migrator I recive following error:

2021-06-28 18:15:20 | HOST database migration started...
2021-06-28 18:15:20 | An error occured during migration of host database:
2021-06-28 18:15:20 | Castle.MicroKernel.Handlers.HandlerException: Can't create component 'ProjectName.EntityFrameworkCore.ProjectNameConnectionStringResolver' as it has dependencies to be satisfied.

'ProjectName.EntityFrameworkCore.ProjectNameConnectionStringResolver' is waiting for the following dependencies:
- Service 'Microsoft.AspNetCore.Hosting.IHostingEnvironment' which was not registered.

   at Castle.MicroKernel.Handlers.DefaultHandler.AssertNotWaitingForDependency()
   at Castle.MicroKernel.Handlers.DefaultHandler.ResolveCore(CreationContext context, Boolean requiresDecommission, Boolean instanceRequired, Burden& burden)
   at Castle.MicroKernel.Handlers.DefaultHandler.Resolve(CreationContext context, Boolean instanceRequired)
   at Castle.MicroKernel.Resolvers.DefaultDependencyResolver.ResolveFromKernelByType(CreationContext context, ComponentModel model, DependencyModel dependency)
   at Castle.MicroKernel.Resolvers.DefaultDependencyResolver.TryResolveCore(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency, Object& value)
   at Castle.MicroKernel.Resolvers.DefaultDependencyResolver.Resolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency)
   at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateConstructorArguments(ConstructorCandidate constructor, CreationContext context)
   at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.Instantiate(CreationContext context)
   at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.InternalCreate(CreationContext context)
   at Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.Create(CreationContext context, Burden burden)
   at Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.CreateInstance(CreationContext context, Boolean trackedExternally)
   at Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.Resolve(CreationContext context, IReleasePolicy releasePolicy)
   at Castle.MicroKernel.Handlers.DefaultHandler.ResolveCore(CreationContext context, Boolean requiresDecommission, Boolean instanceRequired, Burden& burden)
   at Castle.MicroKernel.Handlers.DefaultHandler.Resolve(CreationContext context, Boolean instanceRequired)
   at Castle.MicroKernel.DefaultKernel.ResolveComponent(IHandler handler, Type service, Arguments additionalArguments, IReleasePolicy policy, Boolean ignoreParentContext)
   at Castle.MicroKernel.DefaultKernel.Castle.MicroKernel.IKernelInternal.Resolve(Type service, Arguments arguments, IReleasePolicy policy, Boolean ignoreParentContext)
   at Castle.MicroKernel.DefaultKernel.Resolve(Type service, Arguments arguments)
   at Castle.Windsor.WindsorContainer.Resolve[T]()
   at Abp.Domain.Uow.UnitOfWorkManager.Begin(UnitOfWorkOptions options)
   at Abp.Domain.Uow.UnitOfWorkManager.Begin(TransactionScopeOption scope)
   at Abp.Zero.EntityFrameworkCore.AbpZeroDbMigrator`1.CreateOrMigrate(AbpTenantBase tenant, Action`1 seedAction)
   at Abp.Zero.EntityFrameworkCore.AbpZeroDbMigrator`1.CreateOrMigrateForHost(Action`1 seedAction)
   at ProjectName.Migrator.MultiTenantMigrateExecuter.Run(Boolean skipConnVerification, Boolean isDockerEnabled) in D:\Workspace\Work\XX\ProjectNameProjects\ProjectName-10.3.0\src\ProjectName.Migrator\MultiTenantMigrateExecuter.cs:line 62
2021-06-28 18:15:20 | Canceled migrations.

As I understand the problem, main project has an registered IHostingEnvironment injection but this service is not injected to Migrator.

Can you guide me how to inject this to migrator.

Thank you

Markdown is supported
Copy & paste or drag & drop images (max 30 MB per image)

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

    Hi,

    Could you try getting the configuration as we do here and remove usage of IHostingEnvironment ?

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    bulutyonetim created

    Hi,

    Solved the problem by changing code to this.

        public ProjectNameConnectionStringResolver(IAbpStartupConfiguration configuration,IAmbientDataContext ambientDataContext)
                : base(configuration)
            {
                _appConfiguration = AppConfigurations.Get(
                    typeof(ProjectNameConnectionStringResolver).GetAssembly().GetDirectoryPathOrNull(),
                    Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")
                );
                _ambientDataContext = ambientDataContext;
            }
    

    Thanks @ismcagdas

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)