Unity.WebApi |确保控制器具有一个无参数的构造函数公开 [英] Unity.WebApi | Make sure that the controller has a parameterless public constructor
问题描述
我使用在ASP.NET的WebAPI的解决方案Unity.WebApi的NuGet包(统一4.0.1和5.2.3 Unity.WebApi)。我现在面临的问题是,在尝试运行code当,我得到的错误:确保该控制器具有一个无参数的公共构造
。我搜索类似的主题在这里,但我无法找到符合我的问题的任何线索。
I am using the Unity.WebApi NuGet package (Unity 4.0.1 and Unity.WebApi 5.2.3) in an ASP.NET WebApi solution. The issue I am facing is that when attempting to run the code, I get the error: Make sure that the controller has a parameterless public constructor
. I've searched similar threads here, but I couldn't find any thread that matched my issue.
请不要只说添加参数的构造函数,因为它表明你显然不知道什么国际奥委会,为什么这种说法使得完全没有意义和失败的IoC的目的。我这么说是因为我看到这个了很多,我看着迄今其他线程的。
Please don't just say "Add a parameterless constructor" because it shows you obviously have no clue what IoC is and why that statement makes absolutely no sense and defeats the purpose of IoC. I say this because I saw this on a lot of the other threads I've looked at thus far.
这是我的Startup.cs(我用Owin,所以我没有一个Global.asax中):
This is my Startup.cs (I'm using Owin, so I do not have a Global.asax):
public void Configuration(IAppBuilder app) {
var config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
// Registering unity stuff
UnityConfig.RegisterComponents();
app.UseWebApi(config);
}
这是我的UnityConfig.cs:
This is my UnityConfig.cs:
public static class UnityConfig {
public static void RegisterComponents() {
var container = new UnityContainer();
// Register controller
container.RegisterType<MyController>();
// Register interface
container.RegisterType<IService, Service>();
GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
}
}
这是我的API控制器:
This is my API Controller:
public class MyController : ApiController {
private IService service
public MyController(IService service) {
this.service = service;
}
public IHttpActionResult Get() {
return Ok("All systems go!");
}
}
的修改的
我把using语句,因为人们有一个很难理解发生的问题using语句是否是存在的。
I took the using statements out because people are having a hard time understanding that the issue occurs whether or not the using statements are there.
推荐答案
在我的经验中有2种不同的场景时,发生这种情况。
In my experience there's 2 different scenarios when this occur.
1。
确保在服务所有的参数
构造可以解决。是否有您还没有注册任何依赖关系?
1.
Make sure that all parameters in the Service
constructor can be resolved. Does it have any dependencies that you haven't registered?
<一个href=\"http://stackoverflow.com/questions/13346557/make-sure-that-the-controller-has-a-parameterless-public-constructor-in-unity?rq=1\">Make确保该控制器具有一个无参数的公共构造在Unity
2。
确保使用的Web API来注册统一。像这样:
2. Make sure to register Unity with Web API. Like so:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Routes and other stuff here...
var container = IocContainer.Instance; // Or any other way to fetch your container.
config.DependencyResolver = new UnityDependencyResolver(container);
}
}
下面的例子:
<一个href=\"http://stackoverflow.com/questions/34930678/injecting-dependency-into-web-api-controller/34945118#34945118\">Injecting依赖转换为Web API控制器
<一个href=\"http://$c$cclimber.net.nz/archive/2015/02/20/Using-Entity-Framework-within-an-Owin-hosted-Web-API-with.aspx\" rel=\"nofollow\">http://$c$cclimber.net.nz/archive/2015/02/20/Using-Entity-Framework-within-an-Owin-hosted-Web-API-with.aspx
编辑1:
作为@ NightOwl888在评论中提到的,你不应该处理的容器。的登记注册在容器上,并将该容器被设置时,它不知道如何解决的依赖关系。这可能会导致您所看到的错误。
As @NightOwl888 mentioned in the comments, you should not dispose the container. The registrations are registered on the container, and when the container is disposed it will not know how to resolve the dependencies. It may result in the error you are seeing.
编辑2:
由于您使用Owin,你应该能够做这样的事:
Since you're using Owin you should be able to do something like this:
public void Configuration(IAppBuilder appBuilder)
{
// Configure Web API for self-host.
HttpConfiguration config = new HttpConfiguration();
config.DependencyResolver = new UnityDependencyResolver(
UnityConfig.GetConfiguredContainer());
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
appBuilder.UseWebApi(config);
}
和更新UnityConfig这样:
And update your UnityConfig to this:
public static class UnityConfig {
public static IUnityContainer GetConfiguredContainer() {
var container = new UnityContainer();
// Register controller
container.RegisterType<MyController>();
// Register interface
container.RegisterType<IService, Service>();
//This is done in Startup instead.
//GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
return container;
}
}
从这个来源:
如何使用DI容器时OwinStartup
http://damienbod.com /十分之二千零一十三/ 01 /自主机的WebAPI与 - owin-和团结/
在该方法中,HttpConfiguration.DependencyResolver被设置为一个
UnityDependencyResolver实例。这是必需的,这样构造器
注射可在的WebAPI控制器一起使用。该
UnityDependencyResolver类是完全一样的
Unity.WebApi.UnityDependencyResolver类。 Unity.WebApi不使用
因为这是一个托管自OWIN应用程序。
In this method, the HttpConfiguration.DependencyResolver is set to a UnityDependencyResolver instance. This is required so that contructor injection can be used in the webApi controllers. The UnityDependencyResolver class is exactly the same as the Unity.WebApi.UnityDependencyResolver class. Unity.WebApi is not used because this is a self hosted OWIN application.
这篇关于Unity.WebApi |确保控制器具有一个无参数的构造函数公开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!