尝试通过XML配置设置Unity我遇到了这个异常
The type name or alias ServiceHost could not be resolved. Please check your configuration file and verify this type name.
试着评论一下,我得到了下一个然后的下一个相同的异常,所以问题必须出现在我的设置中。
<unity>
<typeAliases>
<!-- Lifetime manager types -->
<typeAlias alias="singleton"
type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager,
Microsoft.Practices.Unity" />
<typeAlias alias="external"
type="Microsoft.Practices.Unity.ExternallyControlledLifetimeManager,
Microsoft.Practices.Unity" />
<typeAlias alias="perThread"
type="Microsoft.Practices.Unity.PerThreadLifetimeManager,
Microsoft.Practices.Unity" />
<!-- User-defined type aliases -->
<typeAlias alias="ServiceHost"
type="System.ServiceModel.ServiceHost, System.ServiceModel" />
<typeAlias alias="UnityServiceHost"
type="MyProjectServiceLibrary.Hosting.UnityServiceHost" />
<typeAlias alias="IServiceBehavior"
type="System.ServiceModel.Descriptions.IServiceBehavior, System.ServiceModel" />
<typeAlias alias="UnityServiceBehavior"
type="MyProjectServiceLibrary.Hosting.UnityServiceBehavior, MyProjectServiceLibrary" />
<typeAlias alias="IInstanceProvider"
type="System.ServiceModel.Dispatcher.IInstanceProvider, System.ServiceModel" />
<typeAlias alias="UnityInstanceProvider"
type="MyProjectServiceLibrary.Hosting.UnityInstanceProvider, MyProjectServiceLibrary" />
<typeAlias alias="MyProjectService"
type="MyProjectServiceLibrary.Service.MyProjectService, MyProjectServiceLibrary" />
<typeAlias alias="IRepositoryFactory"
type="MyProjectDataModelLibrary.Repository.IRepositoryFactory, MyProjectDataModelLibrary" />
<typeAlias alias="RepositoryFactory"
type="MyProjectDataModelLibrary.Repository.RepositoryFactory, MyProjectDataModelLibrary" />
<typeAlias alias="IDbContext"
type="MyProjectDataModelLibrary.DataContext.IDbContext, MyProjectDataModelLibrary" />
<typeAlias alias="MyProjectDatabase"
type="MyProjectDataModelLibrary.DataContext.MyProjectDatabase, MyProjectDataModelLibrary" />
<typeAlias alias="IRepositoryOfT"
type="MyProjectDataModelLibrary.Repository.IRepository`1, MyProjectDataModelLibrary" />
<typeAlias alias="EntityRepositoryOfT"
type="MyProjectDataModelLibrary.Repository.EntityRepository`1, MyProjectDataModelLibrary" />
</typeAliases>
<containers>
<container>
<types>
<type type="MyProjectService" />
<type type="ServiceHost"
mapTo="UnityServiceHost" />
<type type="IServiceBehavior"
mapTo="UnityServiceBehavior" />
<type type="IInstanceProvider"
mapTo="UnityInstanceProvider" />
<type type="IRepositoryFactory"
mapTo="RepositoryFactory" />
<type type="IDbContext"
mapTo="MyProjectDatabase" />
<type type="IRepositoryOfT"
mapTo="EntityRepositoryOfT" />
</types>
</container>
</containers>
</unity>
我试过四处看看我做错了什么。我怀疑这是完全合格的名字,虽然我不能确定,因为我之前没有真正使用过。
IUnityContainer container = new UnityContainer();
container.RegisterInstance<IUnityContainer>(container);
UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
section.Configure(container);
此处的最后一行引发了异常。
答案 0 :(得分:2)
由于您正在引用系统类型,因此最有可能尝试从GAC中提取这些类型。在这种情况下,您需要使用完全限定的程序集名称,包括版本号,区域性和公钥标记。像这样:
<typeAlias alias="ServiceHost"
type="System.ServiceModel.ServiceHost, System.ServiceModel,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089,
processorArchitecture=MSIL" />
当然,您需要查找适用于您正在使用的.NET版本的#和公钥标记。