将集线器上下文传递给非控制器类

Iba*_*408 1 c# signalr

我正在尝试使用 SignalR 向服务器中的客户端发送消息

我试图在一个不是控制器的类中做到这一点。我已经这样启动了:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.Configure<ConfigurationModel>(Configuration.GetSection("configurationModel"));

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

        services.AddSignalR();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
        }

        app.UseStaticFiles();
        app.UseCookiePolicy();

        app.UseMvc();

        app.UseSignalR(routes => { routes.MapHub<MoveViewHub>("/movehub"); });
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的程序中,这个:

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
        
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();
}
Run Code Online (Sandbox Code Playgroud)

这是在我的中心:

public class MoveViewHub : Hub
{
    private async void ReceiveTagNumber(object sender, EventArgs e)
    {
        await Clients.All.SendAsync("ReceivedFromServer", sender.ToString());
    }

    public async Task MoveViewFromServer(float newX, float newY)
    {
        Console.WriteLine(@"Receive position from Server app: " + newX + "/"  + newY);
        await Clients.Others.SendAsync("ReceivedNewPosition", newX, newY);
        //await Clients.All.SendAsync("ReceivedNewPosition", newX, newY);
    }

    public async Task WriteThisMessage(string message)
    {
        Console.WriteLine(message);
        await Clients.Others.SendAsync("ReceivedStatus", "Message was received. Thank you.");
    }

    public override Task OnConnectedAsync()
    {
        Console.WriteLine("Client has connected");
        RfidClass rfidClass = new RfidClass("THE HUB CONTEXT SHOULD BE HERE"); ====>> I NEED TO PASS MY HUBCONTEXT
        rfidClass.sas();
        RfidClass.SendTagNumber += ReceiveTagNumber;
        System.Diagnostics.Process.Start(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "Notepad++", @"notepad++.exe"));
        return base.OnConnectedAsync();
    }

    public override Task OnDisconnectedAsync(Exception exception)
    {
        Console.Write("Client has disconnected");
        return base.OnDisconnectedAsync(exception);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是 RfidClass:

private IHubContext<MoveViewHub> hubContext;
public RfidClass(IHubContext<MoveViewHub> hubContext)
{
    this.hubContext = hubContext;
}

public void sas()
{
    Start();
}

private void Start()
{
    try
    {
        hubContext.Clients.Others.SendAsync("ReceivedFromServer", "You are connected");
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
        throw;
    }
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到正确?

mic*_*cer 5

你需要通过 .NET Core DI注入IServiceProviderHub的(就像标准控制器一样,通过构造函数注入):

public class MoveViewHub : Hub
{
    private readonly IServiceProvider provider

    public MovieViewHub(IServiceProvider provider)
    {
        this.provider = provider
    }
}
Run Code Online (Sandbox Code Playgroud)

然后你可以做这样的事情:

    public override Task OnConnectedAsync()
    {
        Console.WriteLine("Client has connected");

        // you need to inject service provider to your hub, then get hub context from
        // registered services
        using (var scope = this.provider.CreateScope())
        {
            // get instance of hub from service provider
            var scopedServices = scope.ServiceProvider;
            var hub = scopedServices.GetRequiredService<IHubContext<MoveViewHub>>

            // pass hub to class constructor
            RfidClass rfidClass = new RfidClass(hub)
            rfidClass.sas();
            RfidClass.SendTagNumber += ReceiveTagNumber;
        }

        System.Diagnostics.Process.Start(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "Notepad++", @"notepad++.exe"));
        return base.OnConnectedAsync();
    }
Run Code Online (Sandbox Code Playgroud)

编辑:

如果你只是想SignalR工作,你不需要工作Hub。而是做服务。在您的此服务注入HubContext<>Hub

    // you need to make your own class and interface and inject hub context
    public interface ISignalRService
    {
        Task SendMessageToAll(string message);
    }

    public class SignalRService : ISignalRService
    {
        private readonly IHubContext<YourHub> hubContext;

        public SignalRService (IHubContext<NotificationHub> hubContext)
        {
            this.hubContext = hubContext;
        }

        public async Task SendMessageToAll(string message)
        {
            await this.hubContext.Clients.All.SendAsync("ReciveMessage", message);
        }
    }
Run Code Online (Sandbox Code Playgroud)

然后在您的Startup班级中注册该服务:

services.AddScoped<ISignalRService, SignalRService>();
Run Code Online (Sandbox Code Playgroud)

之后,您可以SignalRService从 .NetCore DI 容器调用任何您想要的普通服务:

private readonly ISignalRService notificationService;


public SomeController(ISignalRService notificationService)
{
    this.notificationService = notificationService;
}

[HttpGet]
public async Task<IActionResult> Send()
{
    await this.notificationService.SendMessageToAll("message");
    return Ok();
}

Run Code Online (Sandbox Code Playgroud)

你不需要像RfidClass.