jbogard / MediatR

Simple, unambitious mediator implementation in .NET
Apache License 2.0
10.84k stars 1.15k forks source link

IoC container issue with .NET 8 & minimal api #997

Closed ghost closed 4 months ago

ghost commented 5 months ago

I have a minimal API project where I am using the MediatR v12.2.0. The code works fine when I debug using Visual Studio 2022, however, I am not able to get the code deployed to docker. In docker, once the app runs, I see the following messages in the logs.

2024-01-31 17:56:50 Unhandled exception. System.AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: MediatR.IMediator Lifetime: Transient ImplementationType: MediatR.Mediator': A suitable constructor for type 'MediatR.Mediator' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.) 2024-01-31 17:56:50 ---> System.InvalidOperationException: Error while validating the service descriptor 'ServiceType: MediatR.IMediator Lifetime: Transient ImplementationType: MediatR.Mediator': A suitable constructor for type 'MediatR.Mediator' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor. 2024-01-31 17:56:50 ---> System.InvalidOperationException: A suitable constructor for type 'MediatR.Mediator' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor. 2024-01-31 17:56:50 at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache, ServiceIdentifier, Type , CallSiteChain) 2024-01-31 17:56:50 at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor, ServiceIdentifier, CallSiteChain, Int32) 2024-01-31 17:56:50 at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite(ServiceDescriptor, CallSiteChain) 2024-01-31 17:56:50 at Microsoft.Extensions.DependencyInjection.ServiceProvider.ValidateService(ServiceDescriptor) 2024-01-31 17:56:50 --- End of inner exception stack trace --- 2024-01-31 17:56:50 at Microsoft.Extensions.DependencyInjection.ServiceProvider.ValidateService(ServiceDescriptor) 2024-01-31 17:56:50 at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(ICollection``1, ServiceProviderOptions) 2024-01-31 17:56:50 --- End of inner exception stack trace --- 2024-01-31 17:56:50 at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(ICollection``1, ServiceProviderOptions) 2024-01-31 17:56:50 at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection, ServiceProviderOptions) 2024-01-31 17:56:50 at Microsoft.Extensions.Hosting.HostApplicationBuilder.<>c__DisplayClass12_0.<.ctor>b__0() 2024-01-31 17:56:50 at Microsoft.Extensions.Hosting.HostApplicationBuilder.Build() 2024-01-31 17:56:50 at Microsoft.AspNetCore.Builder.WebApplicationBuilder.Build() 2024-01-31 17:56:50 at Program.<Main>$(String[]) in /app/apis/services/Pique.Services/Program.cs:line 119 2024-01-31 17:56:50 at Program.<Main>(String[])

I tried using the following two methods to register the MediatR in the IoC container.

But I still keep seeing the same error message. Does that message mean that .NET is not able to resolve to a concrete instance of the IMediator? This did work back in .NET 6. I simply upgraded the .net version to 8 and upgraded the referenced libraries in Visual Studio and started seeing this when I deploy to docker.

jbogard commented 5 months ago

No idea. Maybe post on Stack Overflow?

Ryujose commented 4 months ago

The issue could be related to some dependencies that aren't being registered properly

ghost commented 4 months ago

I figured out the issue. It was the Dockerfile. Apparently, the same Dockerfile in .NET 7 didn't work for .NET 8.