远程通用组件的异常"an item with the same key has already been added"



由于各种原因,我试图将一个项目从旧版本的Castle升级到v 2.5.3(由于中断更改,我无法移动到v3),并且遇到了一个远程通用组件的问题:

   Container.Register(Component.For(typeof(IStore<>))
        .Named("GenericStore")
        .AddAttributeDescriptor("remoteserver", "RecoverableComponent")
        .AddAttributeDescriptor("marshalByRefProxy", "true")
        .ImplementedBy(typeof(MyStore<>)));

组件似乎注册正常,但在我尝试解决的问题上:

   Container.Resolve<IStore<Users>>()

我得到一个异常"已经添加了具有相同密钥的项目",堆栈跟踪(缩短):

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.System.Collections.IDictionary.Add(Object key, Object value)
at Castle.Facilities.Remoting.RemotingInspector.ConfigureServerComponent(RemotingStrategy server, Type type, ComponentModel model)
at Castle.Facilities.Remoting.RemotingInspector.ProcessModel(IKernel kernel, ComponentModel model)
at Castle.MicroKernel.ModelBuilder.DefaultComponentModelBuilder.BuildModel(String key, Type service, Type classType, IDictionary extendedProperties)
at Castle.MicroKernel.Handlers.DefaultGenericHandler.GetSubHandler(CreationContext context, Type genericType)
at Castle.MicroKernel.Handlers.DefaultGenericHandler.ResolveCore(CreationContext context, Boolean requiresDecommission, Boolean instanceRequired)
at Castle.MicroKernel.Handlers.AbstractHandler.Resolve(CreationContext context, Boolean instanceRequired)
at Castle.MicroKernel.Handlers.AbstractHandler.Resolve(CreationContext context)

从堆栈跟踪中可以看到,它似乎又在"构建模型"(调用DefaultComponentModelBuilder)。

我是否错误地注册了组件

我下载了一些源代码,试图找出我做错了什么,但不知道这是否真的是由Generic和Remoting组合引起的问题?

该异常是由Castle.Friences.Remoting.RemotingInspector试图将已存在的属性添加到ExtendedProperties字典中引起的。在Castle.MicroKernel.Handlers.DefaultGenericHander中,它似乎没有检测到模型已经存在的事实(是我还是没有实际添加到Dictionarytype2SubHandler?)。

有人能告诉我我做错了什么吗,或者真的有bug吗

我的建议是,这对温莎城堡来说根本不是问题。您可能在组件中定义了一个静态字典,Castle Windsor试图解析哪个字典(或其他具有uniqe键约束的集合)具有双联键。可能来自复制粘贴操作。如果尝试手动实例化类,则会出现此错误。代码可能看起来像这样:

public class MissTypedDictionaryClass
{
... some ctors here
... some other methods and props
... and somewhere here lies the mistyped dict
    private static readonly Dictionary<string, string> MyDeclaredDict = new Dictionary<string, string>()
    {
        {"Key1", "Val1"},
        {"Key2", "Val2"},
        {"Key1", "Val3"}, // Here is the problem.
    };
 }

相关内容

  • 没有找到相关文章

最新更新