2011-03-16 42 views
0

我試着用下面的腳本來註冊在容器類型,它工作得很好使用DI工具,團結奮進,共創EF ObjectContext的對象

Container.RegisterType<System.Data.Objects.ObjectContext, 
       ExSS.Repository.MyEntity>("myentity", new InjectionConstructor()); 
然而

,當我嘗試使用XML配置:

<alias alias="ObjectContext" 
     type="System.Data.Objects.ObjectContext,System.Data.Entity" /> 
<alias alias="MyEntity" type="ExSS.Repository.MyEntity,ExSS.Repository"/> 
<register type="ExSS.Repository.MyEntity,ExSS.Repository" mapTo="MyEntity" 
     name="myentity"> 
    <constructor></constructor> 
</register> 

這是行不通的。該錯誤信息是:

The type name or alias ObjectContext could not be resolved. Please check your 
configuration file and verify this type name. 

說明:

An unhandled exception occurred during the execution of the current web 
request. Please review the stack trace for more information about the error 
and where it originated in the code. 

異常詳細信息:

System.InvalidOperationException: The type name or alias ObjectContext could 
not be resolved. Please check your configuration file and verify this type name. 

堆棧跟蹤爲:

[InvalidOperationException: The type name or alias ObjectContext could not be resolved. Please check your configuration file and verify this type name.] 
    Microsoft.Practices.Unity.Configuration.ConfigurationHelpers.TypeResolverImpl.ResolveType(String typeNameOrAlias, Boolean throwIfResolveFails) +200 
    Microsoft.Practices.Unity.Configuration.ConfigurationHelpers.TypeResolver.ResolveType(String typeNameOrAlias) +59 
    Microsoft.Practices.Unity.Configuration.RegisterElement.GetRegisteringType() +70 
    Microsoft.Practices.Unity.Configuration.RegisterElement.ConfigureContainer(IUnityContainer container) +111 
    Microsoft.Practices.Unity.Configuration.ContainerConfiguringElement.ConfigureContainerInternal(IUnityContainer container) +39 
    Microsoft.Practices.Unity.Configuration.<>c__DisplayClass1.<ConfigureContainer>b__0(ContainerConfiguringElement element) +42 
    Microsoft.Practices.ObjectBuilder2.EnumerableExtensions.ForEach(IEnumerable`1 sequence, Action`1 action) +200 
    Microsoft.Practices.Unity.Configuration.ContainerElement.ConfigureContainer(IUnityContainer container) +269 
    Microsoft.Practices.Unity.Configuration.UnityConfigurationSection.Configure(IUnityContainer container, String configuredContainerName) +133 
    Microsoft.Practices.Unity.Configuration.UnityContainerExtensions.LoadConfiguration(IUnityContainer container, UnityConfigurationSection section, String containerName) +70 

任何人都可以給一些suggections?非常感謝

回答

0

您的配置錯誤。它應該是:

<alias alias="ObjectContext" type="System.Data.Objects.ObjectContext, System.Data.Entity" /> 
<alias alias="MyEntity" type="ExSS.Repository.MyEntity, ExSS.Repository"/> 
<register type="ObjectContext" mapTo="MyEntity" name="myentity"> 
    <constructor /> 
</register> 
+0

感謝您的回覆。我修改了上面的配置,得到了和以前一樣的錯誤信息。似乎對象不能從XML註冊,我註冊在代碼第一 – eason

+0

:_container.RegisterType (「myentity」,new InjectionConstructor()); 並在xml中配置其他類,其中一些類具有依賴於System.Data.Objects.ObjectContext的字段,在xml中它類似於: <依賴性名稱= 「myEntity所」 類型= 「ObjectContext的」/> eason

+0

但遺憾的是,我得到了相同的錯誤信息。似乎容器ExssUnityCfg無法解析代碼中定義的ObjectContext – eason

1

找到原因,它與該組件相關System.Data.Entity無法正確加載CLR。 publicktokenkey和文化和版本應該被添加。配置如下工作:

<alias alias="ObjectContext" type="System.Data.Objects.ObjectContext, System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> 
<alias alias="MyEntity" type="ExSS.Repository.MyEntity, ExSS.Repository"/> 
<register type="ObjectContext" mapTo="MyEntity" name="myentity"> 
    <constructor /> 
</register>