microsoft-dependency-injection icon indicating copy to clipboard operation
microsoft-dependency-injection copied to clipboard

Diagnostic extension causes crash of ASP.NET Core applications

Open hypercodeplace opened this issue 5 years ago • 14 comments

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.

hypercodeplace avatar Jun 05 '19 10:06 hypercodeplace

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.

ENikS avatar Jun 06 '19 00:06 ENikS

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>>()));

Hieronymus1 avatar Jun 06 '19 09:06 Hieronymus1

I think this should be fixed in this package

I would be happy to accept a pull request with the fix

ENikS avatar Jun 06 '19 11:06 ENikS

OK I am swamped this week but I should be able to send the pull request next week.

Hieronymus1 avatar Jun 06 '19 13:06 Hieronymus1

Any update on this?

ChristophWeigert avatar Oct 28 '19 10:10 ChristophWeigert

Any movement on this? I'm running into this as well.

mmulhearn avatar May 29 '20 00:05 mmulhearn

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 avatar Jul 01 '20 11:07 rjgotten

@rjgotten

Can you create a small test app with all of these? I'll try to fix them asap.

ENikS avatar Jul 01 '20 16:07 ENikS

@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 avatar Jul 02 '20 08:07 rjgotten

@rjgotten Makes it harder to help you, don't you think?

ENikS avatar Jul 02 '20 15:07 ENikS

I got same exception on ASP NET Core 7

turbcool avatar Mar 03 '23 07:03 turbcool

It would help if you explained how it happened

ENikS avatar Mar 03 '23 16:03 ENikS

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)
    {
    }
}

fvaillancourt avatar Mar 04 '23 13:03 fvaillancourt

It seems NET7 is not compatible with 5.x Unity requires upgrading

ENikS avatar Mar 04 '23 17:03 ENikS