3

由于各种原因,我试图将项目从旧版本的 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.Facilities.Remoting.RemotingInspector试图将属性添加到已经存在的 ExtendedProperties 字典引起的。在Castle.MicroKernel.Handlers.DefaultGenericHander中,它似乎没有检测到模型已经存在的事实(是我还是没有实际添加到 Dictionary type2SubHandler 中?)。

谁能告诉我我做错了什么,或者实际上是否存在错误?

4

1 回答 1

3

我谦虚的建议是,温莎城堡根本不是问题。可能您在 Castle Windsor 尝试解析哪个字典(或具有唯一键约束的其他集合)具有重复键的组件中定义了一个静态字典。可能来自复制粘贴操作。如果您尝试手动实例化该类,您将收到此错误。代码可能如下所示:

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.
    };
 }
于 2014-08-15T07:00:29.580 回答