George D.
George D.

Reputation: 321

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.

Upvotes: 14

Views: 10568

Answers (3)

Dhrumil Bhankhar
Dhrumil Bhankhar

Reputation: 1321

I tried above solutions but didn't worked for me. Removing and Reinstalling these 2 specific packages solved the issue for me.

Microsoft.AspNet.WebApi.Tracing
Microsoft.AspNet.WebApi.OData

Upvotes: 0

Steve Willcock
Steve Willcock

Reputation: 26849

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);

Upvotes: 5

James Bradt
James Bradt

Reputation: 634

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

Upvotes: 4

Related Questions