jbogard / MediatR

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

DotNetAspire: This service descriptor is keyed. Your service provider may not support keyed services #1069

Closed TheDevelolper closed 2 months ago

TheDevelolper commented 2 months ago

Hey, when using MediatR with Aspire I seem to get this issue (it works perfectly fine without it though).

Unhandled exception. System.InvalidOperationException: This service descriptor is keyed. Your service provider may not support keyed services.
   at Microsoft.Extensions.DependencyInjection.ServiceDescriptor.ThrowKeyedDescriptor()
   at Microsoft.Extensions.DependencyInjection.ServiceDescriptor.get_ImplementationType()
   at MediatR.Registration.ServiceRegistrar.<>c.<RegisterBehaviorIfImplementationsExist>b__12_0(ServiceDescriptor service)
   at System.Linq.Enumerable.SelectIListIterator`2.MoveNext()
   at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
   at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
   at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
   at MediatR.Registration.ServiceRegistrar.RegisterBehaviorIfImplementationsExist(IServiceCollection services, Type behaviorType, Type subBehaviorType)
   at MediatR.Registration.ServiceRegistrar.AddRequiredServices(IServiceCollection services, MediatRServiceConfiguration serviceConfiguration)
   at Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions.AddMediatR(IServiceCollection services, Action`1 configuration)
   at OctoMenu.Backend.Menu.Application.ConfigureServices.AddBackendServices(IServiceCollection services) in ...\src\libs\backend\Menu.Application\ConfigureServices.cs:line 17
   at Program.<Main>$(String[] args) in ...\src\apps\menu-api\Program.cs:line 18

I'm registering like so:


  public static IServiceCollection AddBackendServices(this IServiceCollection services)
  {
    var assembly = Assembly.GetExecutingAssembly();
    services.AddValidatorsFromAssembly(assembly);
    services.AddMediatR(cfg =>
    {
      cfg.RegisterServicesFromAssembly(assembly);
      cfg.AddBehavior(typeof(IPipelineBehavior<,>), typeof(ValidationBehaviour<,>));
    });
    services.AddTransient<ProductMenuDbContext, ProductMenuDbContext>();
    return services;
  }
}

This was working just fine before I started using .NET Aspire do you know if there are any compatibility issues?

TheDevelolper commented 2 months ago

Okay literally just after writing this I discovered it's an issue with .NET Aspire specifically.

I solved it by downgrading a package: Microsoft.Extensions.Http.Resilience to version 8.0.0 and it worked.