由于各种原因,我正在尝试将项目从旧版本的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 尝试将属性添加到存在alreadt的ExtendedProperties字典引起的。在 Castle.MicroKernel.Handlers.DefaultGenericHander 中,它似乎没有检测到模型已经存在的事实(是我还是实际上没有添加到Dictionary type2SubHandler ?)。
任何人都可以告诉我,如果我做错了,或者确实有错误吗?
答案 0 :(得分:3)
我谦虚的建议是,温莎城堡根本不是问题。可能你在组件中定义了一个静态字典,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.
};
}