Controller parameterless public constructor error using Unity.WebApi and unity config in the web.config
Controller parameterless public constructor error using Unity.WebApi and unity config in the web.config
我的 WebApi 服务在使用运行时 Unity 配置时工作,但是当我将其更改为使用来自 web.config 的统一配置时,我收到错误
"An error occurred when trying to create a controller of type
'MyController'. Make sure that the controller has a parameterless
public constructor."
我正在使用 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 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>
但是,此时当我转到 url 应用程序时出现错误;
<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>
...
引用官方文档,
The <register>
element is the basic building block for any
configuration file. It enables you to specify type mappings and
injection configuration for a type. If you specify a name, that name
is used for the type mapping. If you do not specify a name, it creates
a default mapping for the specified types. You can specify a
lifetime manager for each mapping. If no explicit lifetime manager is
configured for a type, it uses the transient lifetime manager.
所以我建议从网络配置中的 register 标签中删除 name 属性。
我的 WebApi 服务在使用运行时 Unity 配置时工作,但是当我将其更改为使用来自 web.config 的统一配置时,我收到错误
"An error occurred when trying to create a controller of type 'MyController'. Make sure that the controller has a parameterless public constructor."
我正在使用 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 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>
但是,此时当我转到 url 应用程序时出现错误;
<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>
...
引用官方文档,
The
<register>
element is the basic building block for any configuration file. It enables you to specify type mappings and injection configuration for a type. If you specify a name, that name is used for the type mapping. If you do not specify a name, it creates a default mapping for the specified types. You can specify a lifetime manager for each mapping. If no explicit lifetime manager is configured for a type, it uses the transient lifetime manager.
所以我建议从网络配置中的 register 标签中删除 name 属性。