无法从统一配置文件解析类型 [英] Can't resolve types from unity config file

查看:195
本文介绍了无法从统一配置文件解析类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试通过XML配置设置Unity我遇到了这个异常

Trying to set up Unity via XML configurations I'm getting this exception

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

尝试发表评论,我在下一个和之后的下一个中都得到了相同的异常,因此问题必须出在我的设置中.

Trying to comment that out I get the same exception for the next and then the next after that so the problem must lie somewhere in my setup.

<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>

我尝试阅读周围的内容,以查看我做错了什么.我不确定这是完全限定名称,但我不确定,因为我之前从未真正使用过.

I've tried reading around to see what I'm doing wrong. I doubt it's the fully qualified name though I can't be sure as I haven't really used that before.

IUnityContainer container = new UnityContainer();
container.RegisterInstance<IUnityContainer>(container);
UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
section.Configure(container);

在最后一行抛出异常.

推荐答案

由于您正在引用系统类型,因此最有可能尝试从GAC中提取这些类型.在这种情况下,您将需要使用完全合格的程序集名称,包括版本号,区域性和公共密钥令牌.像这样:

Since you're referencing system types, it's most likely trying to pull those types from the GAC. In that case, you'll need to use the fully qualified assembly names, including version number, culture, and public key token. Something like this:

<typeAlias alias="ServiceHost"
  type="System.ServiceModel.ServiceHost, System.ServiceModel,
  Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, 
  processorArchitecture=MSIL" />

当然,您需要为正在使用的.NET版本查找适当的版本号和公共密钥令牌.

You'll need to look up the appropriate version # and public key tokens for the version of .NET you're using, of course.

这篇关于无法从统一配置文件解析类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆