Can't resolve types from unity config file

2019-09-13 10:48发布

问题:

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

The exception is thrown in the last line here.

回答1:

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

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