14
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.

Kyle Trauberman
  • 25,414
  • 13
  • 85
  • 121
George D.
  • 321
  • 1
  • 4
  • 12

3 Answers3

5

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);
Steve Willcock
  • 26,111
  • 4
  • 43
  • 40
  • @StevenWilcock I think this works, except I have a new error that I am not sure is related. on builder.ConfigureWebApi(configuration); I get this error: Could not load type 'System.Web.Http.Dispatcher.DefaultHttpControllerFactory' from assembly 'System.Web.Http, Version=4.0.0.0. here is the lines before: var configuration = GlobalConfiguration.Configuration; var builder = new ContainerBuilder(); // Configure the container with the integration implementations. builder.ConfigureWebApi(configuration); – George D. Jun 03 '12 at 19:30
  • Maybe I need to update System.Net.Http to RC as well? nope...is there a new System.Web.Http package I need to install? – George D. Jun 03 '12 at 19:46
  • 1
    You probably need to grab nuget package Microsoft.Net.Http.2.0.20505.0, which contains the updated System.Net.Http assembly - there may be other ones you are missing too, not sure. I didn't upgrade my MVC4 project from Beta to RC - I created a new project and copied everything I needed from the old one - that way nothing gets missed, although it's hard work in itself :) I assume you already installed MVC4 from http://www.asp.net/mvc/mvc4 here so you have the project templates etc? – Steve Willcock Jun 03 '12 at 20:20
  • There really isn't a way to upgrade...so I uninstalled the beta and installed the RC from Nuget. and I have Microsoft.Net.Http.2.0.20505.0 installed, but I tried to install it again: Attempting to resolve dependency 'Microsoft.Net.Http (≥ 2.0.20505.0)'. 'System.Net.Http 2.0.20505.0' already installed. – George D. Jun 03 '12 at 20:29
  • 1
    I'm actually using AutofacDependencyResolver from Autofac.Mvc4, not Autofac.WebApi. Also, I'm manually registering the Api Controllers like this builder.RegisterType(); instead of using the ConfigureWebApi method - you might have more luck that way. – Steve Willcock Jun 03 '12 at 21:46
  • what were all the types you needed to register. It seems I am missing something as I am getting an autofac error after trying to call a GET method from my controller. – George D. Jun 04 '12 at 00:14
  • 1
    It depends on your controllers and their dependencies of course. I registered the types for my api controllers and all their dependencies (standard dependency injection configuration stuff). So for the simplest controller, I just needed to register the controller itself and the RavenDB DocumentStore which is the sole dependency of the controller. – Steve Willcock Jun 04 '12 at 22:31
  • got it. it now executes my GET in the controller! although sadly...I get no results back (just a 500 server error). I must need to register a serializer? (The autofac.webapi made this way to easy...) – George D. Jun 05 '12 at 18:41
  • Hmm, I didn't have to register a serialiser - I do customise the json serialiser in my global.asax and it is just available without me having to do anything special. var formatter = GlobalConfiguration.Configuration.Formatters.OfType().FirstOrDefault(); – Steve Willcock Jun 06 '12 at 17:51
  • my serializer issue was unrelated. thanks for the help Steve! – George D. Jun 06 '12 at 23:15
  • I'm surprised we still need code such as your `TemporaryDependencyResolver` (not to take away anything from your solution, it really helped me just now). I mean, you'd think that a dedicated Autofac.Mvc4 nuget would have all the necessary bits right? Hope this little guy gets done by the awesome Autofac team by the time MVC 4 gets RTM status :-) – Dav Jul 16 '12 at 21:14
4

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

James Bradt
  • 564
  • 5
  • 11
  • now that I have Autofac for MVC4 working with my web api, I wonder if there are benefits to going back to using AutoFac.WebApi – George D. Jun 12 '12 at 04:09
0

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
Dhrumil Bhankhar
  • 1,301
  • 1
  • 14
  • 15