找到你要的答案

Q:Web API: ApiController creation with DI

Q:Web API创建ApiController DI

I have a ApiController with a parameterized constructor. I read it is possible that the framework creates the controller using dependency injection.

So I does this (like explained here http://code.google.com/p/autofac/wiki/MvcIntegration):

Global.asax

builder.RegisterControllers(Assembly.GetExecutingAssembly());
//or this
builder.RegisterType<ObjectsController>().InstancePerHttpRequest();

The controller looks like this:

public class ObjectsController : ApiController
    {
        IMyObject m_myObject = null;

        public ObjectsController(IMyObject obj)
        {
            m_myObject = obj;
        }
}

Global.asax To resolve the dependency I used this:

builder.Register<IMyObject >((c, p) =>
            {
                //... create object ...

                return obj;
            }).InstancePerHttpRequest();

But I get this error:

"ObjectsController' does not have a default constructor.

I thought I can do this using DI

I have a ApiController with a parameterized constructor. I read it is possible that the framework creates the controller using dependency injection.

所以我这样做(如HTTP:/ /这里解释代码。谷歌。COM / P / autofac /维基/ mvcintegration):

Global.asax

builder.RegisterControllers(Assembly.GetExecutingAssembly());
//or this
builder.RegisterType<ObjectsController>().InstancePerHttpRequest();

控制器看起来像这样:

public class ObjectsController : ApiController
    {
        IMyObject m_myObject = null;

        public ObjectsController(IMyObject obj)
        {
            m_myObject = obj;
        }
}

Global.asax To resolve the dependency I used this:

builder.Register<IMyObject >((c, p) =>
            {
                //... create object ...

                return obj;
            }).InstancePerHttpRequest();

但我得到这个错误:

“objectscontroller '没有默认构造函数。

我想我可以用DI做这个

answer1: 回答1:

This answer could give you a better solution if you want to use DI in ASP.NET Web API: http://stackoverflow.com/a/10592456/538387

This answer could give you a better solution if you want to use DI in ASP.NET Web API: http://stackoverflow.com/a/10592456/538387

answer2: 回答2:

The most common solution is to use a DI specific IDependencyResolver implementation.

That article gives an example with Unity, but you should be able make it work with AutoFac rather easily.

最常见的解决方案是使用一个二IDependencyResolver实施具体。

本文给出了一个实例的统一,但你可以使它很容易的工作autofac。

answer3: 回答3:

If you are using Unity, This is all you need to do in ASP.NET 4 Web API RC (as on 8th August '13):

Add a reference to "Microsoft.Practices.Unity.dll" [I'm on version 3.0.0.0, added through NuGet] Add a reference to "Unity.WebApi.dll" [I'm on version 0.10.0.0, added through NuGet] Register your type mappings with the container - similar to the code in Bootstrapper.cs that gets added to your project by Unity.WebApi project. In the controllers that inherit from the ApiController class, create parameterized constructors that have their parameter types as the mapped types Lo and behold, you get the dependencies injected into your constructor without another line of code!

如果您使用的是统一的,这是所有你需要做的4 ASP.NET Web API的RC(如13年8月8日):

Add a reference to "Microsoft.Practices.Unity.dll" [I'm on version 3.0.0.0, added through NuGet] Add a reference to "Unity.WebApi.dll" [I'm on version 0.10.0.0, added through NuGet] Register your type mappings with the container - similar to the code in Bootstrapper.cs that gets added to your project by Unity.WebApi project. In the controllers that inherit from the ApiController class, create parameterized constructors that have their parameter types as the mapped types Lo and behold, you get the dependencies injected into your constructor without another line of code!

answer4: 回答4:

As others suggested, it seems to be a good idea to use any IDependencyResolver implementation.

Unfortunately for me this didn't solve the problem that WebAPI needs a parameterless contructor in their ApiController based classes. => I simply use property injection instead of contructor injection in my web api controller base class and provide an empty parameterless constructor. Not beautiful but KISS :-)

 public class ApiControllerBaseWithDependency : ApiController
{
    [Microsoft.Practices.Unity.Dependency]
    protected MyServiceType Service { get; set;}
}

别人的建议,这似乎是一个好主意,使用任何IDependencyResolver实施。

Unfortunately for me this didn't solve the problem that WebAPI needs a parameterless contructor in their ApiController based classes. => I simply use property injection instead of contructor injection in my web api controller base class and provide an empty parameterless constructor. Not beautiful but KISS :-)

 public class ApiControllerBaseWithDependency : ApiController
{
    [Microsoft.Practices.Unity.Dependency]
    protected MyServiceType Service { get; set;}
}
dependency-injection  constructor  controller  asp.net-web-api