0

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

user437899
  • 8,879
  • 13
  • 51
  • 71
  • 2
    Related: http://stackoverflow.com/questions/11254292/does-simple-injector-supports-mvc-4-asp-net-web-api. That question is about a different DI container, but the solution is (about) the same. Or take a look at [this blog post](http://alexmg.com/post/2012/03/08/Autofac-ASPNET-Web-API-%28Beta%29-Integration.aspx). – Steven Jul 12 '12 at 18:14

4 Answers4

2

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

Community
  • 1
  • 1
Tohid
  • 6,175
  • 7
  • 51
  • 80
1

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.

jrummell
  • 42,637
  • 17
  • 112
  • 171
1

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!

Sudhanshu Mishra
  • 6,523
  • 2
  • 59
  • 76
0

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;}
}
Sebastian
  • 173
  • 10