Unity:无法解析类型名称或别名x。请检查您的配置文件并验证此类型名称

时间:2015-04-28 13:58:45

标签: c# .net dependency-injection unity-container

错误代码

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

我正在抓取谷歌搜索结果/试图调试2天,我还没有找到任何解决方案。

提及" ApplicationService"正在解决。

  • 我多次验证程序集和命名空间的名称
  • 我已在配置文件中尝试过这些概念:https://stackoverflow.com/a/18671286/3264998和其他概念。
  • 我验证了与数据库的连接。
  • 我在VS中尝试了调试模式而没有任何成功。
  • 可能还有其他一些我现在不记得的东西。

Bellow你有我的代码,希望它足够了。如果我遗漏了任何其他文件/信息,我道歉,我会立即编辑帖子。

IApplicationService.cs

using System.Collections.Generic;
using Abc.Project.Domain.Model.DTO;

namespace Abc.Project.Application.Interfaces
{
    public interface IApplicationService
    {
        void AddFile(FileDTO fileDTO);
    }
}

ApplicationService.cs

using System.Collections.Generic;
using AutoMapper;
using Microsoft.Practices.Unity;
using Abc.Project.Application.Interfaces;
using Abc.Project.Domain.Model.DTO;
using Abc.Project.Domain.Model.Poco.Entities;
using Abc.Project.Domain.Repository.UnitOfWork;
using Abc.Project.Domain.Unity;

namespace Abc.Project.Application.Services.Global
{
    public class ApplicationService : IApplicationService
    {
        public void AddFile(FileDTO fileDTO)
        {
            File file = new File
            {
                Id = fileDTO.ID,
                FileObs = fileDTO.FileObs,
                Ind = fileDTO.Ind,
                Levels = fileDTO.Levels,
            };

            using (var uow = IoC.Container.Resolve<IUnitOfWorkFactory>().Create())
            {
                uow.Context.File.Add(file);
                uow.Commit();
            }
        }
    }
}

IUnitOfWorkFactory.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Abc.Project.Domain.Repository.UnitOfWork
{
    public interface IUnitOfWorkFactory
    {
        IUnitOfWork Create();
    }
}

UnitOfWorkFactory.cs

using System.Data;
using System.Reflection;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Tool.hbm2ddl;
using Abc.Project.Domain.Repository.UnitOfWork;

namespace Abc.Project.DataAccess.NHibernate.UnitOfWork
{
    public class UnitOfWorkFactory : IUnitOfWorkFactory
    {
        private static ISessionFactory CreateSessionFactory()
        {
            return Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008
                .ConnectionString(c => c.FromConnectionStringWithKey("FilesDB"))
                )
                .Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly()))
                .ExposeConfiguration(cfg => new SchemaExport(cfg)
                .Create(false, false))
                .BuildSessionFactory();
        }

        public IUnitOfWork Create()
        {
            UnitOfWork UnitOfWork = new UnitOfWork(CreateSessionFactory().OpenSession());
            UnitOfWork.BeginTransaction(IsolationLevel.ReadCommitted);
            return UnitOfWork;
        }
    }
}

的App.config

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>

  </configSections>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <connectionStrings>
    <add name="FilesDB" providerName="System.Data.SqlClient" connectionString="server=SP2010;database=FilesDB;User ID=sa;Password=password;"/>
  </connectionStrings>
  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <typeAliases>
      <!-- Lifetime manager types -->
      <typeAlias alias="singlecall" type="Microsoft.Practices.Unity.TransientLifetimeManager, Microsoft.Practices.Unity"/>
      <typeAlias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity"/>
      <typeAlias alias="external" type="Microsoft.Practices.Unity.ExternallyControlledLifetimeManager, Microsoft.Practices.Unity"/>
      <typeAlias alias="percall" type="Abc.Project.Domain.Unity.StaticPerCallLifeTimeManager, Abc.Project.Domain.Model"/>

      <!-- SERVICE APPLICATION INTERFACES-->
      <typeAlias alias="IApplicationService" type="Abc.Project.Application.Interfaces.IApplicationService, Abc.Project.Application.Interfaces"/>

      <!-- DOMAIN INTERFACES-->
      <typeAlias alias="IUnitOfWorkFactory" type="Abc.Project.Domain.Repository.UnitOfWork.IUnitOfWorkFactory, Abc.Project.Domain.Repository"/>

      <!-- CONCRETE CLASSES-->

      <!-- SERVICE APPLICATION-->
      <typeAlias alias="ApplicationService" type="Abc.Project.Application.Services.Global.ApplicationService, Abc.Project.Application.Services"/>

      <!--DATA ACCESS-->
      <typeAlias alias="UnitOfWorkFactory" type="Abc.Project.DataAccess.NHibernate.UnitOfWork.UnitOfWorkFactory, Abc.Project.DataAccess.NHibernate"/>

    </typeAliases>
    <containers>
      <container>
        <!--<extension type="Interception" />-->
        <types>

          <type type="IApplicationService" mapTo="ApplicationService">
            <lifetime type="singlecall"/>
          </type>
          <type type="IUnitOfWorkFactory" mapTo="UnitOfWorkFactory">
            <lifetime type="singleton"/>
          </type>

        </types>
      </container>
    </containers>
  </unity>

  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="Abc.Project.WcfService.WcfServiceAspNetAjaxBehavior">
          <enableWebScript/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="metadataAndDebug">
          <serviceMetadata httpGetEnabled="true" httpGetUrl=""/>
          <serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
    <services>
      <service name="Abc.Project.WcfService.WcfService" behaviorConfiguration="metadataAndDebug">
        <endpoint address="" behaviorConfiguration="Abc.Project.WcfService.WcfServiceAspNetAjaxBehavior" binding="webHttpBinding" contract="Abc.Project.WcfService.WcfService"/>
      </service>
    </services>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

我该如何解决这个问题? 英语不是我的母语;请原谅输入错误。

2 个答案:

答案 0 :(得分:2)

请改用<alias> - 元素。这是Unity配置架构的link

<unity>
   <alias alias="IUnitOfWorkFactory" type="Abc.Project.Domain.Repository.UnitOfWork.IUnitOfWorkFactory, Abc.Project.Domain.Repository" />

   <alias alias="UnitOfWorkFactory" type="Abc.Project.DataAccess.NHibernate.UnitOfWork.UnitOfWorkFactory, Abc.Project.DataAccess.NHibernate"/>

</unity>

<typeAlias> - 元素已过时,取决于您的Unity版本不再受支持。

答案 1 :(得分:2)

UnitOfWorkFactory 的程序集未被引用