Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diagnostic extension causes crash of ASP.NET Core applications #47

Open
hypercodeplace opened this issue Jun 5, 2019 · 14 comments
Open
Assignees
Labels

Comments

@hypercodeplace
Copy link

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.

@ENikS
Copy link
Contributor

ENikS commented Jun 6, 2019

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 ENikS added the Question ❔ Issue is a question label Jun 6, 2019
@Hieronymus1
Copy link

Hieronymus1 commented Jun 6, 2019

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

@ENikS
Copy link
Contributor

ENikS commented Jun 6, 2019

I think this should be fixed in this package

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

@Hieronymus1
Copy link

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

@ChristophWeigert
Copy link

Any update on this?

@ENikS ENikS added Bug 🐛 and removed Question ❔ Issue is a question labels Jan 9, 2020
@ENikS ENikS self-assigned this Jan 9, 2020
@mmulhearn
Copy link

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

@rjgotten
Copy link

rjgotten commented Jul 1, 2020

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.

@ENikS
Copy link
Contributor

ENikS commented Jul 1, 2020

@rjgotten

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

@rjgotten
Copy link

rjgotten commented Jul 2, 2020

@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.

😞

@ENikS
Copy link
Contributor

ENikS commented Jul 2, 2020

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

@turbcool
Copy link

turbcool commented Mar 3, 2023

I got same exception on ASP NET Core 7

@ENikS
Copy link
Contributor

ENikS commented Mar 3, 2023 via email

@fvaillancourt
Copy link

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

@ENikS
Copy link
Contributor

ENikS commented Mar 4, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants