在web.config中使用Unity.WebApi和unity config的Controller无参数公共构造函数错误

时间:2017-02-10 11:26:06

标签: c# asp.net-web-api configuration unity-container

我的WebApi服务在使用运行时Unity配置时有效,但当我将其更改为使用web.config中的unity配置时,我收到错误

  

"尝试创建类型的控制器时发生错误   '&myController的#39 ;.确保控制器具有无参数   公共构造函数。"

我使用Unity和Unity.WebApi nuget包。

使用运行时配置(工作正常)时,我的RegisterComponents方法看起来像这样;

public static void RegisterComponents()  
{
    IUnityContainer container = new UnityContainer();
    container.RegisterType<Repository.Interfaces.IRepository, DataAccess.OracleRepository>(new HierarchicalLifetimeManager());

    GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container); 
}  

在web.config中使用xml配置时,我使用它;

public static void RegisterComponents()
{
    IUnityContainer container = new UnityContainer().LoadConfiguration();
    GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
}

我在web.config中的unity部分看起来像这样;

  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <alias alias="IRepository" type="Repository.Interfaces.IRepository, Repository"/>
    <alias alias="OracleRepository" type="DataAccess.OracleRepository, DataAccess"/>
    <container>
      <register type="IRepository" mapTo="OracleRepository" name="oracle">
        <lifetime type="hierarchical"/>
      </register>
    </container>
  </unity>

然而,此时,当我转到网址时,应用程序会给出错误;

<Error>
    <Message>An error has occurred.</Message>
    <ExceptionMessage>An error occurred when trying to create a controller of type 'MyController'. Make sure that the controller has a parameterless public constructor.</ExceptionMessage>
    <ExceptionType>System.InvalidOperationException</ExceptionType>
    <StackTrace>at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request) at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()</StackTrace>
    <InnerException>
        <Message>An error has occurred.</Message>
        <ExceptionMessage>Resolution of the dependency failed, type = "GatewayService.Controllers.MyController", name = "(none)". Exception occurred while: while resolving. Exception is: InvalidOperationException - The current type, Repository.Interfaces.IRepository, is an interface and cannot be constructed. Are you missing a type mapping? ----------------------------------------------- At the time of the exception, the container was: Resolving GatewayService.Controllers.MyController,(none) Resolving parameter "respository" of constructor GatewayService.Controllers.MyController(Repository.Interfaces.IRepository respository) Resolving Repository.Interfaces.IRepository,(none)</ExceptionMessage>
        <ExceptionType>Microsoft.Practices.Unity.ResolutionFailedException</ExceptionType>
        ...

1 个答案:

答案 0 :(得分:0)

引用官方文档,

  

The <register> Element

     

<register>元素是任何元素的基本构建块   配置文件。它使您可以指定类型映射和   一种类型的注射配置。 如果指定名称,则为该名称   用于类型映射。如果您未指定名称,则会创建   指定类型的默认映射。您可以指定   每个映射的生命周期管理器如果没有明确的终身经理   为类型配置,它使用瞬态生命周期管理器。

所以我建议从web配置中的register标签中删除name属性。