Castle IOC 怪异的“已存在具有相同键的条目”问题

本文记录了一个关于IOC容器中出现重复键异常的问题排查过程。通过调整配置文件中的组件注册顺序,成功解决了异常。文章探讨了组件依赖关系对于配置顺序的影响。
      突然之间,毫无征兆、莫名其妙地IOC就蹦出了个“已存在具有相同键的条目”的错误。
ContractedBlock.gifExpandedBlockStart.gif错误信息
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: System.ArgumentException: 已存在具有相同键的条目。

源错误: 


ExpandedBlockStart.gifContractedBlock.gif行 
24:     {
ExpandedSubBlockStart.gifContractedSubBlock.gif行 
25:         get {
行 
26:             return new WindsorContainer("Appconfigs.xml"); }

行 
27:     }

行 
28
 

源文件: d:\DHis\CoreWebUI\App_Code\ContainerInit.cs    行: 
26 

堆栈跟踪: 


[ArgumentException: 已存在具有相同键的条目。]
   System.Collections.Specialized.ListDictionary.Add(Object key, Object value) 
+2247376
   System.Collections.Specialized.HybridDictionary.Add(Object key, Object value) 
+132
   Castle.MicroKernel.Handlers.AbstractHandler.AddDependency(DependencyModel dependency) 
+423
   Castle.MicroKernel.Handlers.AbstractHandler.EnsureDependenciesCanBeSatisfied() 
+592
   Castle.MicroKernel.Handlers.AbstractHandler.Init(IKernel kernel) 
+143
   Castle.MicroKernel.Handlers.DefaultHandlerFactory.Create(ComponentModel model) 
+134
   Castle.MicroKernel.DefaultKernel.AddComponent(String key, Type serviceType, Type classType, LifestyleType lifestyle, Boolean overwriteLifestyle) 
+449
   Castle.MicroKernel.DefaultKernel.AddComponent(String key, Type serviceType, Type classType, LifestyleType lifestyle) 
+44
   Castle.MicroKernel.DefaultKernel.AddComponent(String key, Type serviceType, Type classType) 
+40
   Castle.Windsor.WindsorContainer.AddComponent(String key, Type serviceType, Type classType) 
+42
   Castle.Windsor.Installer.DefaultComponentInstaller.SetUpComponents(IConfiguration[] configurations, IWindsorContainer container) 
+419
   Castle.Windsor.Installer.DefaultComponentInstaller.SetUp(IWindsorContainer container, IConfigurationStore store) 
+72
   Castle.Windsor.WindsorContainer.RunInstaller() 
+75
   Castle.Windsor.WindsorContainer..ctor(IConfigurationInterpreter interpreter) 
+150
   Castle.Windsor.WindsorContainer..ctor(String xmlFile) 
+51
   ContainerInit.get_Container() 
in d:\DHis\CoreWebUI\App_Code\ContainerInit.cs:26
   ASP.global_asax.Session_Start(Object sender, EventArgs e) 
in d:\DHis\CoreWebUI\Global.asax:35
   System.Web.SessionState.SessionStateModule.RaiseOnStart(EventArgs e) 
+2163166
   System.Web.SessionState.SessionStateModule.CompleteAcquireState() 
+154
   System.Web.SessionState.SessionStateModule.BeginAcquireState(Object source, EventArgs e, AsyncCallback cb, Object extraData) 
+542
   System.Web.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 
+90
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean
& completedSynchronously) +155

可是配置文件里面明明没有重复的Id呀。经过1小时的排查,发现只要配置文件中的这个
<component
    id="Manager.HospPatientDrsAdvice"
    service="Dawn.HIS.Core.ManagerInterface.Hospitalized.IHospPatientDrsAdviceManager, Dawn.HIS.Core.ManagerInterface"
    type="Dawn.HIS.Core.BusinessManager.Hospitalized.HospPatientDrsAdviceManager, Dawn.HIS.Core.BusinessManager" />
标记注释掉就没问题了。可是这个标记还有用呀,然后发现只要把这个标记移到另一个我新添加的标记
<component
    id="Manager.PatientStateAdv"
    service="Dawn.HIS.Core.ManagerInterface.Hospitalized.IPatientStateAdvManager, Dawn.HIS.Core.ManagerInterface"
    type="Dawn.HIS.Core.BusinessManager.Hospitalized.PatientStateAdvManager, Dawn.HIS.Core.BusinessManager" />
的下面就没问题了。真是奇怪,难道标记的顺序也很重要?
      确实,HospPatientDrsAdviceManager类使用了IPatientStateAdvManager,难道如果必须按照引用顺序来排列标记的顺序?

转载于:https://www.cnblogs.com/1-2-3/articles/1432833.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值