Concurrent resolving of same PluginType · Issue #646 · structuremap/structuremap · GitHub
Skip to content

Concurrent resolving of same PluginType #646

Description

@sm-g

I have Task.WhenAll(tasks), and each task uses AutoMapper's ValueResolver which has dependency, resolved via DI.

public AutoMapperRegistry()
        {
            ForSingletonOf<MapperConfiguration>().Use("Build AutoMapper config", ctx =>
            {
                var profiles = ctx.GetAllInstances<Profile>();
                var config = new MapperConfiguration(cfg =>
                {
                    cfg.AllowNullCollections = true;

                    foreach (var profile in profiles)
                    {
                        cfg.AddProfile(profile);
                    }
                });
                return config;
            });
            For<IMapper>().Use(ctx => ctx.GetInstance<MapperConfiguration>().CreateMapper(ctx.GetInstance));
        }

When AutoMapper creates ValueResolver instance, same dependency added to dictionary more than once:

System.ArgumentException: An item with the same key has already been added.
   at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at StructureMap.SessionCache.GetDefault(Type pluginType, IPipelineGraph pipelineGraph)
   at AutoMapper.MappingOperationOptions`2.CreateInstance[T]()

Looks like expcetion from https://github.com/structuremap/structuremap/blob/master/src/StructureMap/SessionCache.cs#L77

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions