MediatR icon indicating copy to clipboard operation
MediatR copied to clipboard

AmbigousMatchException

Open simirajsp opened this issue 3 years ago • 3 comments

I have the following responses and request handlers in two different assemblies.

AssemblyA EmployeeDetails GetEmployeeQueryHandler: IRequestHandler<EmployeeDetalils>

AssemblyB EmployeeDetails GetEmployeeQueryHandler: IRequestHandler<EmployeeDetalils>

When I try to register dependencies, it gives an exception "System.Reflection.AmbiguousMatchException" when AddMediatR is called. Given below is the stack trace. My models and handlers are in different namespaces even though they have the same name. Can you please help me with this?

   at System.RuntimeType.GetInterface(String fullname, Boolean ignoreCase)

at System.Type.GetInterface(String name) at MediatR.Registration.ServiceRegistrar.IsMatchingWithInterface(Type handlerType, Type handlerInterface) at MediatR.Registration.ServiceRegistrar.<>c__DisplayClass1_0.<ConnectImplementationsToTypesClosing>b__3(Type m) at System.Collections.Generic.List1.RemoveAll(Predicate1 match) at MediatR.Registration.ServiceRegistrar.ConnectImplementationsToTypesClosing(Type openRequestInterface, IServiceCollection services, IEnumerable1 assembliesToScan, Boolean addIfAlreadyExists) at MediatR.Registration.ServiceRegistrar.AddMediatRClasses(IServiceCollection services, IEnumerable1 assembliesToScan) at MediatR.ServiceCollectionExtensions.AddMediatR(IServiceCollection services, IEnumerable1 assemblies, Action1 configuration) at MediatR.ServiceCollectionExtensions.AddMediatR(IServiceCollection services, Assembly[] assemblies) at MedAlert.Clinics.App.Server.Startup.ConfigureServices(IServiceCollection services) in Startup.cs:line 63 at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.InvokeCore(Object instance, IServiceCollection services) at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass9_0.<Invoke>g__Startup|0(IServiceCollection serviceCollection) at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.Invoke(Object instance, IServiceCollection services) at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass8_0.<Build>b__0(IServiceCollection services) at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.UseStartup(Type startupType, HostBuilderContext context, IServiceCollection services, Object instance) at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass13_0.<UseStartup>b__0(HostBuilderContext context, IServiceCollection services) at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider() at Microsoft.Extensions.Hosting.HostBuilder.Build() at MedAlert.Clinics.App.Server.Program.Main(String[] args) in D:\MainProject\ClinicsApis\Clinics.App\MedAlert.Clinics.App.Server\Program.cs:line 18

simirajsp avatar Jul 14 '21 03:07 simirajsp

Curious if this is not supported. I anticipate coding myself into this very scenario.

zachrybaker avatar Sep 05 '21 01:09 zachrybaker

@zachrybaker, did u find any solution for this?

simirajsp avatar Jun 28 '22 10:06 simirajsp

@simirajsp To be honest I have not hit this issue in practice. In fact at this point I would say that if you did you probably have a design issue. Conceptually, why would you need two different assemblies to provide that functionalities' implementation? Perhaps what you need, actually, is a single assembly to implement, with two other assemblies that reference the said assembly to expose it in two different manners.

zachrybaker avatar Jul 04 '22 13:07 zachrybaker