32

Hi I have an MVC application where I have defined some dependencies to my Web API.

public class AutofacWebApiDependenceResolver : IDependencyResolver
{
    private readonly IComponentContext container;
    public AutofacWebApiDependenceResolver(IContainer container)
    {

     if (container == null)
     {
         throw new ArgumentNullException("container");
     }
     this.container = container;
    }

    public object GetService(Type serviceType)
    {
        if (serviceType == null)
        {
            throw new ArgumentNullException("serviceType");
        }
        var ret = this.container.ResolveOptional(serviceType) ;
        return ret;
    }
    public IEnumerable<object> GetServices(Type serviceType)
    {            
        if (serviceType == null)
        {
            throw new ArgumentNullException("serviceType");
        }            
        Type enumerableType = typeof(IEnumerable<>).MakeGenericType(serviceType);
        var ret = (IEnumerable<object>)this.container.ResolveOptional(enumerableType);
        return ret;
    }
}

Then in my bootstrapper class I am calling it in Application_Start as follows:

GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependenceResolver((IContainer)container);

When I debug the code, I can see there are registrations of all services with my DependencyResolver, but I am still getting the following error:

An error has occurred.Type 'WebApp.Controllers.AuthenticateController' does not have a default constructor

Here is the code to my controller:

public class AuthenticateController : ApiController
{
    private readonly IFormsAuthenticationService formsAuthenticationService;
    private readonly IMemberShipProvider memberShip;
    private readonly IDataService dataService;

    public AuthenticateController(
        IMemberShipProvider memberShip,
        IFormsAuthenticationService formsAuthenticationService, IDataService dataService)
    {
        this.memberShip = memberShip;
        this.formsAuthenticationService = formsAuthenticationService;
        this.dataService= dataService;
     }

}

How can I bring parameters to the api controllers. The Simple controllers are working fine.

11
  • have you registered your controllers with calling builder.RegisterApiControllers(Assembly.GetExecutingAssembly());? Commented Oct 14, 2014 at 10:20
  • take look at this: stackoverflow.com/questions/9450282/… Commented Oct 14, 2014 at 10:23
  • @freshbm Yes I saw that, didn't get the solution :-S Commented Oct 14, 2014 at 10:28
  • @nemesv I am unable to call builder.RegisterApiControllers(), can you please tell me which namespace do I have to import for that? Commented Oct 14, 2014 at 10:28
  • 3
    you need to call builder.RegisterApiControllers before calling builder.Build()... Commented Oct 14, 2014 at 10:44

2 Answers 2

71

I would suggest the following to make this work in your application with both MVC and WebApi.

First your project will need to have references to the following

  • Autofac
  • Autofac.WebApi
  • Autofac.Mvc5 (change the number at the end to match your aspnet mvc version)

Then in your Autofac registration you would need the following which will Register both MVC and WebApi Controllers and any other registrations you require. Then attach the Container to both the MVC DI and the WebApi DI.

var builder= new ContainerBuilder();
builder.RegisterControllers(Assembly.GetExecutingAssembly()); //Register MVC Controllers
builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); //Register WebApi Controllers
//Register any other components required by your code....

var container = builder.Build();

DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); //Set the MVC DependencyResolver
GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver((IContainer)container); //Set the WebApi DependencyResolver

I hope this helps.

Sign up to request clarification or add additional context in comments.

6 Comments

GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver((IContainer)container); That is the killer bit in my case.
This sorted it out for me thanks! I also realised I had installed the wrong nuget package as I was using WebApi2. So once I had remove Autofac.WebApi and installed Autofac.WebApi2 it all worked like a charm, thanks @jonhoare
I soooo needed that!
For newbies: code from @jonhoare answer you should put in App_Start/WebApiConfig.cs in Register method.
@Jennet. TY! this was my final problem, not having the Autofac.WebAPI2 installed. soon as that was installed, everything fell into place. thx!
|
9

nemesv's guidance did the trick

builder.RegisterApiControllers(Assembly.GetExecutingAssembly())

Calling builder.RegisterApiControllers before calling builder.Build()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.