1

I've got an existing, working web app written in MVC3, using Autofac and Autofac.Mvc3. I now need to use SignalR, so I've pulled in Autofac.SignalR via Nuget, which has upgraded Autofac.dll to 3.0.0.

What I'm noticing now, is that when I'm trying to register a type, I get a MissingMethodException when I call InstancePerHttpRequest as the lifetime for a type.

As an exercise, I tried doing what my current application's autofac registration is doing in a brand new MVC4 application, with Autofac.Mvc4 and Autofac.SignalR, which seems to work just fine.

Any help would be appreciated

Example lines that trigger the error

builder.RegisterType<CompanyController>().InstancePerHttpRequest();
// or
builder.Register(x => x.Resolve<ISessionFactory.
    ().OpenSession()).InstancePerHttpRequest();

Exception

Method not found: 'Autofac.Builder.IRegistrationBuilder3<!0,!1,!2> Autofac.Builder.IRegistrationBuilder3.InstancePerMatchingLifetimeScope(System.Object)'.

StackTrace

at Autofac.Integration.Mvc.RegistrationExtensions.InstancePerHttpRequest[TLimit,TActivatorData,TStyle](IRegistrationBuilder`3 registration) at DMS.Website.MvcApplication.Application_Start() in d:\Code\Onset\DOT\trunk\DMS\DMS.Website\Global.asax.cs:line 167

damienc88
  • 1,957
  • 19
  • 34
  • 1
    I'm afraid you are out of luck here. You cannot use SignalR with MVC3 and Autofac, because there is no support for Autofac3 in MVC3. See also: http://stackoverflow.com/a/16719213/872395 So you need to upgrade to MVC4 or you need build of own version of Autofac.MVC3 which depends on Autofac 3.0+. – nemesv May 30 '13 at 05:48
  • I've come to the same conclusion. Could you post that as an answer? – damienc88 May 30 '13 at 05:57
  • You can use `InstancePerLifetimeScope` instead. – jgauffin May 30 '13 at 07:26
  • @jgauffin Using `InstancePerLifetimeScope` has some problems in web application and it is not recommended to use it inside them. See at the end of this answer: http://stackoverflow.com/a/10901484/872395. That is why the `InstancePerHttpRequest` was introduced which uses a specific named lifetime scope internally to avoid these problems. – nemesv May 30 '13 at 09:18
  • @nemesv: Well. In my experience that is never a problem. – jgauffin May 30 '13 at 09:44

1 Answers1

2

I'm afraid that you are out of luck here. Autofac.SignalR was built against of Autofac3+ but the Autofac.Mvc3 does only support Autofac2.6.

And it seems there won't be any support added in the future either according to Travis Illig

Autofac has a general policy of staying current, so as new MVC releases are put out, older releases stop getting direct support. There is no version of Autofac 3.0 that has MVC3 support and there is no plan to back-port functionality.

So you either upgrade to MVC4 or can you try to build a custom version of Autofac.MVC3 which fits your needs.

Disclaimer: HACK alert use it at your own risk:

So if you are not afraid to get your hands dirty you can mess with the internals of Autofac with the help of a little reflection to "port" the InstancePerHttpRequest to work with Autofac3+

Just add this class to your project and make sure that you using this version of the InstancePerHttpRequest when doing the registrations.

public static class MyRegistrationExtensions
{
    public static IRegistrationBuilder<TLimit, TActivatorData, TStyle> 
         InstancePerHttpRequest<TLimit, TActivatorData, TStyle>(
         this IRegistrationBuilder<TLimit, TActivatorData, TStyle> registration)
    {
        if (registration == null)
            throw new ArgumentNullException("registration");
        var httpRequestTagField = 
              typeof(RequestLifetimeScopeProvider)
              .GetField("HttpRequestTag", 
                        BindingFlags.NonPublic | BindingFlags.Static)
        return registration.InstancePerMatchingLifetimeScope(
                   httpRequestTagField.GetValue(null));
    }
}

Basically this method does the same thing as the original version except it needs reflection to access the otherwise internal RequestLifetimeScopeProvider.HttpRequestTag field.

Community
  • 1
  • 1
nemesv
  • 138,284
  • 16
  • 416
  • 359
  • Thanks for the comprehensive answer. We decided to just upgrade the project to MVC4, but I'll keep that hack in mind. – damienc88 Jun 26 '13 at 04:45