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

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

Open
Raj4git opened this issue Sep 9, 2021 · 1 comment
Open

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

Raj4git opened this issue Sep 9, 2021 · 1 comment

Comments

@Raj4git
Copy link

Raj4git commented Sep 9, 2021

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
Copy link
Contributor

xudonax commented Sep 12, 2021

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

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

No branches or pull requests

2 participants