简体   繁体   中英

How to register strongly-typed SignalR Core hubs?

I have a ASP.Net Core 3.1 web app and I'm trying to inject strongly-typed hubs into the controller as follows:

public class MyController : ControllerBase
{
    private readonly IHubContext<MyHub, IMyHub> hubContext;

    public MyController(IHubContext<MyHub, IMyHub> hubContext)
    {
        this.hubContext = hubContext;
    }    
}

Autofac is already set up but I still didn't find a way to inject the hubs. (Versions or class SignalR had a RegisterHubs extension method.

I tried the following:

        builder.RegisterAssemblyTypes()
            .Where(t => typeof(IHubContext<>).IsAssignableFrom(t))
            .ExternallyOwned();

and this:

       builder.RegisterAssemblyTypes()
            .Where(t => t.Name.EndsWith("Hub"))
            .As(typeof(Hub<>))
            .ExternallyOwned();

But it didn't work.

What is the new way to do it?

Try using AsClosedTypesOf(Hub<>) instead of just As(Hub<>) . See docs here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM