Autofac + SignalR

I'm completely new to autofac and singalR, so please be calm! I have the following code in bootstrapper that works on its own without a signal.

var builder = new ContainerBuilder(); builder.RegisterControllers(Assembly.GetExecutingAssembly()); builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).AsImplementedInterfaces(); builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>().InstancePerHttpRequest(); builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerHttpRequest(); builder.RegisterAssemblyTypes(typeof(adminRepository).Assembly).Where(t => t.Name.EndsWith("Repository")).AsImplementedInterfaces().InstancePerHttpRequest(); builder.RegisterAssemblyTypes(typeof(adminService).Assembly).Where(t => t.Name.EndsWith("Service")).AsImplementedInterfaces().InstancePerHttpRequest(); builder.RegisterFilterProvider(); IContainer container = builder.Build(); DependencyResolver.SetResolver(new Autofac.Integration.Mvc.AutofacDependencyResolver(container)); 

The code above works fine, but after registering my hub with this code below, it just doesn't work.

 builder.RegisterType<Chat>().InstancePerLifetimeScope(); builder.RegisterFilterProvider(); IContainer container = builder.Build(); DependencyResolver.SetResolver(new Autofac.Integration.Mvc.AutofacDependencyResolver(container)); SignalR.IDependencyResolver resolver = new SignalR.Autofac.AutofacDependencyResolver(container); GlobalHost.DependencyResolver = resolver; RouteTable.Routes.MapHubs(resolver); 

I am using SignalR dependency binding binding to Autofac from this (https://github.com/pszmyd/SignalR.Autofac).

I have such a simple hub

 public class Chat : Hub { private readonly IadminService adminService; public Chat(IadminService adminService) { this.adminService = adminService; } public void Send(string message) { Clients.addMessage(message); } } 

This is the error I received when I tried to use DI in a hub.

"No area with a tag matching" httpRequest "is visible from the area in which the instance was requested.

No matter what I do, I cannot get it to work, and I would appreciate if anyone could tell me what happened to the code above.

Many thanks Leo

+4
source share
2 answers

For an ASP.NET application hosted in IIS, add it to Application_Start:

 var container = AutofacConfig.BuildContainer(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); var signalRDependencyResolver = new SignalRAutofacDependencyResolver(container); // old SignalR 1.0 way - routes.MapHubs(signalRDependencyResolver); RouteTable.Routes.MapHubs(new HubConfiguration { Resolver = signalRDependencyResolver }); 

Here you can find SignalRAutofacDependencyResolver.

An example of a hub introducing the ISecurity service:

 public class ExampleHub : Hub { private static int _count = 0; private readonly ISecurity _security; public ExampleHub(ISecurity security) { _security = security; } public void GetCount() { _count++; Clients.All.SetCount(_count); } } 

It also works with SignalR as a standalone host version . Just use the launch class as follows:

 // These are static variables in Program.cs - Probably a better way to do this _container = AutofacConfig.BuildContainer(); _webServer = WebApp.Start<WebServerStartup>("http://localhost:8080"); public class WebServerStartup { private readonly SignalRAutofacDependencyResolver _signalRDependencyResolver; public WebServerStartup() { _signalRDependencyResolver = new SignalRAutofacDependencyResolver(_container); } public void Configuration(IAppBuilder app) { app.MapSignalR(new HubConfiguration { Resolver = _signalRDependencyResolver }); } } 
+3
source

After setting DependencyResolver you still need to pass it to MapHubs, try instead:

 GlobalHost.DependencyResolver = resolver; RouteTable.Routes.MapHubs(); 

I know SignalR, but have never used Autofac, but it might be worth looking at these answers for more information on a possible Autofac problem:

How to enable Autofac InstancePerHttpRequest

Autofac, ASP.NET MVC 3 area httpRequest and AutoMapper: the area with the tag matching "httpRequest" is not visible

+1
source

Source: https://habr.com/ru/post/1436154/


All Articles