Autofac - 例外:已添加具有相同密钥的项目

时间:2016-02-19 08:21:46

标签: asp.net webforms autofac

我刚刚切换到Autofac,它似乎运作良好。但是,在调试应用程序时,我收到了一条令人不安的错误消息。但是我没有能够复制它。我认为这与多线程竞争条件有关。

Autofac.Core.DependencyResolutionException: An exception was thrown while executing a resolve operation. See the InnerException for details. ---> An item with the same key has already been added. (See inner exception for details.) ---> 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 System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
   at Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(Guid id, Func`1 creator)
   at Autofac.Core.Resolving.InstanceLookup.Execute()
   at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters)
   at Autofac.Core.Resolving.ResolveOperation.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters)
   at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable`1 parameters)
   --- End of inner exception stack trace ---
   at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable`1 parameters)
   at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters)
   at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable`1 parameters, Object& instance)
   at Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable`1 parameters)
   at Autofac.ResolutionExtensions.Resolve(IComponentContext context, Type serviceType, IEnumerable`1 parameters)
   at Autofac.ResolutionExtensions.Resolve[TService](IComponentContext context, IEnumerable`1 parameters)
   at Autofac.ResolutionExtensions.Resolve[TService](IComponentContext context)
   at Tuna.TunaKernel.Get[T]() in S:\Tuna\TunaKernel.cs:line 32

如果您想知道TunaKernel是什么样的......

using Autofac;
using Autofac.Core;
using System.Collections.Generic;

namespace Tuna
{
    /// <summary>
    /// A class used for providing instances of classes to Tuna applications 
    /// </summary>
    /// <remarks>Wrapper around Autofac IOC solution</remarks>
    public class TunaKernel : ITunaKernel
    {
        ILifetimeScope _container;

        /// <summary>
        /// Prevents a default instance of the <see cref="TunaKernel"/> class from being created.
        /// </summary>
        /// <param name="modules">The modules.</param>
        public TunaKernel(ILifetimeScope container)
        {
            _container = container;
        }

        /// <summary>
        /// Gets an instance of T
        /// </summary>
        /// <typeparam name="T">The object to get an instance of</typeparam>
        /// <returns></returns>
        public T Get<T>() 
        {
            return _container.Resolve<T>();
        }
    }
}

该项目的绑定是

builder.RegisterType<EditProgramHelper>().As<IEditProgramHelper>().InstancePerRequest();

是的,我知道直接使用范围,不建议使用范围,但是我处理的是大量已编写的代码。

0 个答案:

没有答案