sentialabs / MediatR

3 stars 0 forks source link

An item with the same key has already been added #1

Open Raj4git opened 3 years ago

Raj4git commented 3 years ago

An item with the same key has already been added. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: An item with the same key has already been added.

Source Error:

{ var castContext = context as IRequest; return await this.mediator.Send(castContext, token); } This is what our registry looks with structure map

public RegisterMediatr() { this.Scan(scanner => { scanner.AssemblyContainingType(typeof(IRequestHandler<,>)); scanner.ConnectImplementationsToTypesClosing(typeof(IRequestHandler<,>)); });

    this.For<ServiceFactory>().Use<ServiceFactory>(ctx => ctx.GetInstance);
    this.For<IMediator>().LifecycleIs<TransientLifecycle>().Use<Mediator>();

    // registerhandlers
    this.Handlers(); // This registers all the handllers
}

We are calling same handler in parallel.

[ArgumentException: An item with the same key has already been added.] System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) +56 System.Collections.Generic.Dictionary2.Insert(TKey key, TValue value, Boolean add) +12948692 System.Collections.Generic.Dictionary2.Add(TKey key, TValue value) +16 StructureMap.SessionCache.GetDefault(Type pluginType, IPipelineGraph pipelineGraph) +144 StructureMap.BuildSession.GetInstance(Type pluginType) +17 MediatR.ServiceFactoryExtensions.GetInstances(ServiceFactory factory) +84 MediatR.Internal.RequestHandlerWrapperImpl2.Handle(IRequest1 request, CancellationToken cancellationToken, ServiceFactory serviceFactory) +122 MediatR.Mediator.Send(IRequest`1 request, CancellationToken cancellationToken) +258

xudonax commented 3 years ago

Hi,

If you're only calling the RegisterMediatr function once (check this with a debugger), then this error is kinda weird. In any case, I suggest taking this up with the official MediatR repository, they'll be able to help you better. You can find that over at https://github.com/jbogard/MediatR