3
votes

How do I get Autofac to inject dependencies into the signalr hubs, I have tried all the only questions many of which are now out of date, I have tried the signalr.autofac download from their site, but nothing seems to work?

My Autofac

var builder = new ContainerBuilder();
builder.RegisterControllers(typeof(MvcApplication).Assembly);

//Register Context
builder.Register(c => new myContext()).As<IUnitOfWork>().InstancePerLifetimeScope();

//Repositories
builder.RegisterGeneric(typeof(BaseRepository<>)).As(typeof(IBaseRepository<>)).InstancePerHttpRequest();           

//Service Layer
builder.RegisterType<UserService>().As<IUserService>().InstancePerHttpRequest();

//other services etc

builder.RegisterType<ChatHub>(); 

var container = builder.Build();           

DependencyResolver.SetResolver(new AutofacDependencyResolver(container));          

GlobalHost.DependencyResolver = new Autofac.Integration.SignalR.AutofacDependencyResolver(container);
RouteTable.Routes.MapHubs();

The dependency revolver I get from the autofac web site;

http://code.google.com/p/autofac/downloads/detail?name=Autofac.SignalR.3.0.0.zip

but I get;

No scope with a Tag matching 'AutofacWebRequest' is visible from the scope in which the instance was requested. This generally indicates that a component registered as per-HTTP request is being requested by a SingleInstance() component (or a similar scenario.) Under the web integration always request dependencies from the DependencyResolver.Current or ILifetimeScopeProvider.RequestLifetime, never from the container itself.

2
Please post the code what you have so far? How do you build your container how do you create and setup the dependency resolver? etc.nemesv
Code has been updated, the hub is just a hub with a service interface being added, I can post the code if needed, not sure if anything else is neededEnenDaveyBoy

2 Answers

2
votes

I got a solution from autofac, not perfect but it works:

http://code.google.com/p/autofac/issues/detail?id=416

0
votes

Worth a try, maybe this new docs page can help you finding your issue: https://code.google.com/p/autofac/wiki/SignalRIntegration