我有以下代碼:Unity.ResolutionFailedException - 依賴解析失敗
public static IEngine CreateEngine()
{
UnityContainer container = Unity.LoadUnityContainer(DefaultStrategiesContainerName);
IEnumerable<IStrategy> strategies = container.ResolveAll<IStrategy>();
ITraderProvider provider = container.Resolve<ITraderProvider>();
return new Engine(provider, new List<IStrategy>(strategies));
}
和配置:
<unity> <typeAliases>
<typeAlias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />
<typeAlias alias="weakRef" type="Microsoft.Practices.Unity.ExternallyControlledLifetimeManager, Microsoft.Practices.Unity" />
<typeAlias alias="Strategy" type="ADTrader.Core.Contracts.IStrategy, ADTrader.Core" />
<typeAlias alias="Trader" type="ADTrader.Core.Contracts.ITraderProvider, ADTrader.Core" /> </typeAliases> <containers>
<container name="strategies">
<types>
<type type="Strategy" mapTo="ADTrader.Strategies.ThreeTurningStrategy, ADTrader.Strategies" name="1" />
<type type="Trader" mapTo="ADTrader.MbTradingProvider.MBTradingProvider, ADTrader.MbTradingProvider" />
</types>
</container> </containers></unity>
我得到以下異常:
Microsoft.Practices.Unity.ResolutionFailedException:在 依賴解析失敗,TYPE =「ADTrader.Core.Contracts .ITraderProvider「, name =」「。異常消息是:當前的構建操作(構建 密鑰構建密鑰[ADTrader.MbTradingProvider.MBTradingProvider,null]) 失敗:試圖讀取或寫入受保護的內存。這通常是指示其他內存已損壞的 。 (策略 型BuildPlanStrategy,指數3)---> Microsoft.Practices.ObjectBuilder2.BuildFailedException:當前 構建操作(生成密鑰生成 鍵[ADTrader.MbTradingProvider.MBTradingProvider,空])失敗: 嘗試讀取或寫保護的內存。這通常是指示其他內存已損壞的 。 (策略類型 BuildPlanStrategy,索引3)---> System.AccessViolationException: 試圖讀取或寫入受保護的內存。這通常是指示其他內存已損壞的 。在 MBTCOMLib.MbtComMgrClass.EnableSplash(布爾bEnable)在 ADTrader.MbTradingProvider.MBTradingProvider..ctor()在 BuildUp_ADTrader.MbTradingProvider.MBTradingProvider(IBuilderContext) 在 Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext 上下文)在 Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext 上下文)在 Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext 上下文)內部異常堆棧跟蹤的---末尾在 Microsoft.Practices。 ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)at Microsoft.Practices.ObjectBuilder2.Builder.BuildUp(IReadWriteLocator 定位器,ILifetimeContainer壽命,IPolicyList政策, IStrategyChain策略,對象buildKey,對象存在的)在 Microsoft.Practices.Unity.UnityContainer.DoBuildUp(T型,對象 現有,字符串名稱)---內部異常堆棧跟蹤---
在Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t,Object existing,String name)at Microsoft.Practices.Unity.UnityContainer.Resolve(類型t,字符串名稱) at Microsoft.Practices.Unity.UnityContainerBase.ResolveT at ADTrader.Engine.EngineFactory。CreateEngine()
任何想法?