Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autofac / MVC4 / WebApi (RC) Dependency Injection issue after upgrading from beta

var resolver = new AutofacWebApiDependencyResolver(container);
configuration.ServiceResolver.SetResolver(resolver);

after updating to ASP.NET MVC4 (RC) I get the following error:

'System.Web.Http.HttpConfiguration' does not contain a definition for 'ServiceResolver' and no extension method 'ServiceResolver' accepting a first argument of type 'System.Web.Http.HttpConfiguration' could be found (are you missing a using directive or an assembly reference?)

I realize after reading this (http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver) that these interfaces have changed, but I am not sure how to apply this change to how I use Autofac.

Do i need to wait for a new release from Autofac or is there another way I can get past this.

like image 660
George D. Avatar asked Jun 02 '12 23:06

George D.


2 Answers

Edit: As James Bradt mentions in his post below, the Autofac package has now been updated to fix this issue, so anyone coming across this thread in the future should probably try the new package first :)

Basically, with the new package you just need to do this in your global.asax.cs:

GlobalConfiguration.Configuration.DependencyResolver = new Autofac.Integration.WebApi.AutofacWebApiDependencyResolver(container);

/Edit

I just came across the same issue - I was able to resolve it in my situation by creating a simple IDependencyResolver implementation that wraps the existing AutofacDependencyResolver.

As the class name suggests, I'm treating this as a temporary resolution - the BeginScope and Dispose methods will need some work and are obviously not suitable for a production environment but this allows me to continue development until a proper solution emerges.

So, with those caveats, the IDependencyResolver implementation looks like this:

public class TemporaryDependencyResolver : IDependencyResolver
{
    private readonly AutofacDependencyResolver _autofacDependencyResolver;

    public TemporaryDependencyResolver(AutofacDependencyResolver autofacDependencyResolver)
    {
        _autofacDependencyResolver = autofacDependencyResolver;
    }

    public void Dispose()
    {
    }

    public object GetService(Type serviceType)
    {
        return _autofacDependencyResolver.GetService(serviceType);
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        return _autofacDependencyResolver.GetServices(serviceType);
    }

    public IDependencyScope BeginScope()
    {
        return this;
    }
}

and I set it like this in Global.asax.cs:

var container = builder.Build();
var resolver = new AutofacDependencyResolver(container);
GlobalConfiguration.Configuration.DependencyResolver = new TemporaryDependencyResolver(resolver);
like image 170
Steve Willcock Avatar answered Sep 20 '22 15:09

Steve Willcock


The AutoFac.WebApi package has been updated to (RC) - version 2.6.2.859

This appears to have been adjusted for the change in the dependencies between RC and Beta

like image 20
James Bradt Avatar answered Sep 19 '22 15:09

James Bradt