microsoft-dependency-injection
microsoft-dependency-injection copied to clipboard
Diagnostic extension causes crash of ASP.NET Core applications
How to reproduce
Use standard ASP.NET Core application (I'm tried on netcoreapp2.1
, netcoreapp2.2
and netcoreapp3.0
)
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Unity;
using Unity.Microsoft.DependencyInjection;
namespace WebApplication1
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseUnityServiceProvider(new UnityContainer()
.AddExtension(new Diagnostic()) // <---- without this line everything is ok
)
.UseStartup<Startup>();
}
}
The following exception throws:
System.InvalidOperationException: No service for type 'Microsoft.Extensions.Logging.ILogger`1[Microsoft.AspNetCore.Hosting.Internal.WebHost]' has been registered.
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
at WebApplication1.Program.Main(String[] args)
The real under hood exception from Unity.Microsoft.DependencyInjection.ServiceProvider
is
Unity.ResolutionFailedException: Ambiguous constructor: Failed to choose between LoggerFactory(System.Collections.Generic.IEnumerable`1[Microsoft.Extensions.Logging.ILoggerProvider], Microsoft.Extensions.Options.IOptionsMonitor`1[Microsoft.Extensions.Logging.LoggerFilterOptions]) and LoggerFactory(System.Collections.Generic.IEnumerable`1[Microsoft.Extensions.Logging.ILoggerProvider], Microsoft.Extensions.Logging.LoggerFilterOptions)
_____________________________________________________
Exception occurred while:
•resolving type: 'ILoggerFactory' mapped to 'LoggerFactory'
for parameter: 'factory'
on constructor: Logger`1(ILoggerFactory factory)
•resolving type: 'ILogger`1' mapped to 'Logger`1'
If remove Diagnostic
extension from the container then everything works fine.
Yes, Diagnostic extension does not work with ASP.NET Core. It follows original Unity spec and is too strict for .NET implementation. Microsoft uses much more relaxed validation.
This library needs its own validation implementation.
I also came across this issue and I think this should be fixed in this package if it's a dependency of the Unity.Diagnostic extension. I'm not sure of which spec you are talking about but we just need to tell the UnityContainer how to resolve this ambiguous dependency because there is two constructors with the same number of arguments.
I also came across similar issues with ISwaggerProvider and ISchemaRegistryFactory for Swashbuckle but this did the trick to resolve the ILoggerFactory dependency:
container.RegisterType<ILoggerFactory, LoggerFactory>(new ContainerControlledLifetimeManager(), new InjectionConstructor(new ResolvedParameter<IEnumerable<ILoggerProvider>>()));
I think this should be fixed in this package
I would be happy to accept a pull request with the fix
OK I am swamped this week but I should be able to send the pull request next week.
Any update on this?
Any movement on this? I'm running into this as well.
Also still facing this. Build-up problems are a total pain to debug without the the diagnostics extension enabled; but enabling the diagnostics extension completely hoses the entire application.
This really, really, REALLY needs a fix as a top priority. It basically makes Unity a total liability when used for a large scale ASP.NET application, which is probably the single most major type of application being developed with .NET Core.
There's more issues with the diagnostics extension than just the logger disambiguation; I've also had it crap out with a seg-fault in the past.
@rjgotten
Can you create a small test app with all of these? I'll try to fix them asap.
@ENikS If I knew what actually caused the diagnostics extension to trigger an application seg fault, then I could and would create a reduced test case. Sadly, that sort of requires the diagnostics extension to, well... work.
I'm not exactly at liberty to share the full source code for the projects in which my teams ran across these issues either. It's owned by our employer and/or its customers.
😞
@rjgotten Makes it harder to help you, don't you think?
I got same exception on ASP NET Core 7
It would help if you explained how it happened
Took the code from original post, added an empty Startup like below, added Unity.Microsoft.DependencyInjection
nuget and ran => SegFault
class Startup
{
public void ConfigureServices(IServiceCollection services)
{
}
public void Configure(WebApplication app, IWebHostEnvironment env)
{
}
}
It seems NET7 is not compatible with 5.x Unity requires upgrading