damienc88
damienc88

Reputation: 1977

Autofac + MVC3 + SignalR integration issue

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

Upvotes: 1

Views: 1072

Answers (1)

nemesv
nemesv

Reputation: 139758

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.

Upvotes: 2

Related Questions