dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.56k stars 4.54k forks source link

Unable to resolve type: Microsoft.Extensions.Hosting.IHostApplicationLifetime #104372

Open vsfeedback opened 4 days ago

vsfeedback commented 4 days ago

This issue has been moved from a ticket on Developer Community.


[severity:I'm unable to use this version] [regression] [worked-in:17.9] I updated my visual studio from 17.9 to 17.10.1. Without changing anything to the dotnet code, the api won't start anymore and gives an exception in the program.cs file on the code rule:

var app = builder.Build();

Note: we are using Lightinject for depencency injection.

Callstack:

System.InvalidOperationException
  HResult=0x80131509
  Message=Unable to resolve type: Microsoft.Extensions.Hosting.IHostApplicationLifetime, service name: 
  Source=LightInject
  StackTrace:
   at LightInject.ServiceContainer.CreateDelegate(Type serviceType, String serviceName, Boolean throwError)
   at LightInject.ServiceContainer.CreateDefaultDelegate(Type serviceType, Boolean throwError)
   at LightInject.ServiceContainer.GetInstance(Type serviceType, Scope scope)
   at LightInject.Scope.GetInstance(Type serviceType)
   at LightInject.Microsoft.DependencyInjection.LightInjectServiceProvider.GetRequiredService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.Extensions.Hosting.HostBuilder.<>c__DisplayClass35_0.<PopulateServiceCollection>b__2(IServiceProvider _)
   at LightInject.Microsoft.DependencyInjection.DependencyInjectionContainerExtensions.<>c__DisplayClass10_0`1.<CreateTypedFactoryDelegate>b__0(IServiceFactory serviceFactory)
   at LightInject.Microsoft.DependencyInjection.PerRootScopeLifetime.GetInstance(GetInstanceDelegate createInstance, Scope scope, Object[] arguments)
   at LightInject.ServiceContainer.GetInstance(Type serviceType, Scope scope)
   at LightInject.Scope.GetInstance(Type serviceType)
   at LightInject.Microsoft.DependencyInjection.LightInjectServiceProvider.GetRequiredService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.Extensions.Hosting.HostBuilder.ResolveHost(IServiceProvider serviceProvider, DiagnosticListener diagnosticListener)
   at Microsoft.Extensions.Hosting.HostApplicationBuilder.Build()
   at Microsoft.AspNetCore.Builder.WebApplicationBuilder.Build()
   at Program.<Main>$(String[] args) in C:\Projects\Interreg\Eloket\Interreg.Eloket.Web.Api\Program.cs:line 40

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
InvalidOperationException: Unresolved dependency [Target Type: Microsoft.Extensions.Hosting.Internal.ApplicationLifetime], [Parameter: logger(Microsoft.Extensions.Logging.ILogger`1[Microsoft.Extensions.Hosting.Internal.ApplicationLifetime])], [Requested dependency: ServiceType:Microsoft.Extensions.Logging.ILogger`1[Microsoft.Extensions.Hosting.Internal.ApplicationLifetime], ServiceName:]

Inner Exception 2:
InvalidOperationException: Unresolved dependency [Target Type: Microsoft.Extensions.Logging.Logger`1[Microsoft.Extensions.Hosting.Internal.ApplicationLifetime]], [Parameter: factory(Microsoft.Extensions.Logging.ILoggerFactory)], [Requested dependency: ServiceType:Microsoft.Extensions.Logging.ILoggerFactory, ServiceName:]

Inner Exception 3:
InvalidOperationException: Unresolved dependency [Target Type: Microsoft.Extensions.Logging.LoggerFactory], [Parameter: providers(System.Collections.Generic.IEnumerable`1[Microsoft.Extensions.Logging.ILoggerProvider])], [Requested dependency: ServiceType:System.Collections.Generic.IEnumerable`1[Microsoft.Extensions.Logging.ILoggerProvider], ServiceName:]

Inner Exception 4:
InvalidOperationException: Unresolved dependency [Target Type: <>z__ReadOnlySingleElementList`1[Microsoft.Extensions.Logging.ILoggerProvider]], [Parameter: item(Microsoft.Extensions.Logging.ILoggerProvider)], [Requested dependency: ServiceType:Microsoft.Extensions.Logging.ILoggerProvider, ServiceName:]

Inner Exception 5:
InvalidOperationException: Unresolved dependency [Target Type: Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider], [Parameter: telemetryConfigurationOptions(Microsoft.Extensions.Options.IOptions`1[Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration])], [Requested dependency: ServiceType:Microsoft.Extensions.Options.IOptions`1[Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration], ServiceName:]

Inner Exception 6:
InvalidOperationException: Unresolved dependency [Target Type: Microsoft.Extensions.DependencyInjection.TelemetryConfigurationOptions], [Parameter: applicationInsightsServiceOptions(Microsoft.Extensions.Options.IOptions`1[Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions])], [Requested dependency: ServiceType:Microsoft.Extensions.Options.IOptions`1[Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions], ServiceName:]

Inner Exception 7:
InvalidOperationException: Unresolved dependency [Target Type: Microsoft.Extensions.Options.UnnamedOptionsManager`1[Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions]], [Parameter: factory(Microsoft.Extensions.Options.IOptionsFactory`1[Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions])], [Requested dependency: ServiceType:Microsoft.Extensions.Options.IOptionsFactory`1[Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions], ServiceName:]

Inner Exception 8:
InvalidOperationException: Unresolved dependency [Target Type: Microsoft.Extensions.Options.OptionsFactory`1[Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions]], [Parameter: postConfigures(System.Collections.Generic.IEnumerable`1[Microsoft.Extensions.Options.IPostConfigureOptions`1[Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions]])], [Requested dependency: ServiceType:System.Collections.Generic.IEnumerable`1[Microsoft.Extensions.Options.IPostConfigureOptions`1[Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions]], ServiceName:]

Inner Exception 9:
InvalidOperationException: Unresolved dependency [Target Type: <>z__ReadOnlySingleElementList`1[Microsoft.Extensions.Options.IPostConfigureOptions`1[Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions]]], [Parameter: item(Microsoft.Extensions.Options.IPostConfigureOptions`1[Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions])], [Requested dependency: ServiceType:Microsoft.Extensions.Options.IPostConfigureOptions`1[Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions], ServiceName:]

Original Comments

Arnaud Boussaer on 6/10/2024, 06:31 AM:

(private comment, text removed)

Feedback Bot on 6/10/2024, 11:12 PM:

(private comment, text removed)


Original Solutions

Constantine Nevostruev solved on 6/19/2024, 08:37 AM, 0 votes:

The problem is with z__ReadOnlySingleElementList which is embedded into your assembly at compile time. As far as I understand, it is used for the new style of array initialization like var nums = [5]. Seems you are using automatic type registration to their implemented interfaces which register z__ReadOnlySingleElementList<> to IEnumerable<>. Then the container tries to use z__ReadOnlySingleElementList<> in the cases where it needs to collect all implementations of an interface.

You can, for instance, filter out all the types that implement IEnumerable. Example using Autofac:

containerBuilder
    . RegisterAssemblyOpenGenericTypes(assemblies)
    . Where(x => !x.IsAssignableTo(typeof(IEnumerable)))
    . AsImplementedInterfaces()
    . AsSelf();
dotnet-policy-service[bot] commented 4 days ago

Tagging subscribers to this area: @dotnet/area-extensions-dependencyinjection See info in area-owners.md if you want to be subscribed.