![](/img/trans.png)
[英]Hub method from client is not invoking in ASP.NET Core SignalR
[英]ASP.NET Core SignalR acces Hub method from anywhere
如果在这个问题上花了很多时间,我发现了很多不同的策略,但是没有一个对我有用。 (此代码只是课程概念的证明。)
我使用Asp.net core 2.1(在.Net Framwork 4.7.2上)具有以下设置:
我已经制作了一个信号发送器中心,它具有一种发送号码的方法:
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
namespace TestRandomNumberSignalR
{
public class TestHub : Hub
{
public async Task SendRandomNumber(int number)
{
await Clients.All.SendAsync("ReceiveRandomBumber", number);
}
}
}
我还制作了一个类,每3秒更新一次随机数,并将其作为单例添加:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace TestRandomNumberSignalR
{
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.AddSingleton(new UpdateRandomNumber());
services.AddSignalR();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
// 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();
}
app.UseMvc();
app.UseSignalR(routes =>
{
routes.MapHub<TestHub>("/testHub");
});
}
}
}
这是随机数类:
using System;
using System.Threading;
using System.Threading.Tasks;
namespace TestRandomNumberSignalR
{
public class UpdateRandomNumber
{
private bool _continue = true;
public UpdateRandomNumber()
{
var task = new Task(() => RandomNumberLoop(),
TaskCreationOptions.LongRunning);
task.Start();
}
private void RandomNumberLoop()
{
Random r = new Random();
while (_continue)
{
Thread.Sleep(3000);
int number = r.Next(0, 100);
Console.WriteLine("The random number is now " + number);
// Send new random number to connected subscribers here
// Something like TestHub.SendRandomNumber(number);
}
}
public void Stop()
{
_continue = false;
}
}
}
现在从这个类中(就像我在评论中所写的那样),我想使用SignalR发送新的随机数。 仅如何在其中获得中心上下文?
我还希望能够从控制器内部访问类的Stop()方法,我该如何访问呢?
我现在这是一个经过充分讨论的主题,但仍然找不到任何可行的解决方案。 希望您能够帮助我。
编辑
问题1
虽然随机循环现在开始(非常感谢rasharasha),但仍然存在一些问题。 我现在无法将适当的UpdateRandomNumber注入到控制器中。 可以说我希望能够停止调用UpdateRandomNumber.Stop()方法的循环,如何将UpdateRandomNumber单例注入到控制器中。 我尝试创建一个接口:
public interface IUpdateRandomNumber
{
void Stop();
}
更改RandomNumber方法以实现此目的:
public class UpdateRandomNumber : IUpdateRandomNumber
{
private bool _continue = true;
private IHubContext<TestHub> testHub;
public UpdateRandomNumber(IHubContext<TestHub> testHub)
{
this.testHub = testHub;
var task = new Task(() => RandomNumberLoop(),
TaskCreationOptions.LongRunning);
task.Start();
}
private void RandomNumberLoop()
{
Random r = new Random();
while (_continue)
{
Thread.Sleep(3000);
int number = r.Next(0, 100);
Console.WriteLine("The random number is now " + number);
// Send new random number to connected subscribers here
// Something like TestHub.SendRandomNumber(number);
}
}
public void Stop()
{
_continue = false;
}
}
并更改添加单例方法,使其可以使用以下接口:
services.AddSingleton<IUpdateRandomNumber>(provider =>
{
var hubContext = provider.GetService<IHubContext<TestHub>>();
var updateRandomNumber = new UpdateRandomNumber(hubContext);
return updateRandomNumber;
});
现在,我可以使用一种方法来创建一个控制器来停止randomnumber循环:
[Route("api/[controller]")]
[ApiController]
public class RandomController : ControllerBase
{
private readonly IUpdateRandomNumber _updateRandomNumber;
public RandomController(IUpdateRandomNumber updateRandomNumber)
{
_updateRandomNumber = updateRandomNumber;
}
// POST api/random
[HttpPost]
public void Post()
{
_updateRandomNumber.Stop();
}
但是,此实现将阻止循环再次开始。 那么如何从控制器访问rondomnumber单例呢?
问题2
现在可以从我的UpdateRandomNumber类调用:
testHub.Clients.All.SendAsync("ReceiveRandomBumber", number);
但是,为什么要在我的testhub中使用该方法:
public async Task SendRandomNumber(int number)
{
await Clients.All.SendAsync("ReceiveRandomBumber", number);
}
在中心中创建方法并直接调用它们会更加方便。 能做到吗?
您可以使用构造函数注入将TestHub注入控制器。 由于它已经在DI容器中注册。
public class UpdateRandomNumber
{
private bool _continue = true;
private IHubContext<TestHub> testHub;
private Task randomNumberTask;
public UpdateRandomNumber(IHubContext<TestHub> testHub)
{
this.testHub=testHub;
randomNumberTask = new Task(() => RandomNumberLoop(),
TaskCreationOptions.LongRunning);
randomNumberTask.Start();
}
private async void RandomNumberLoop()
{
Random r = new Random();
while (_continue)
{
Thread.Sleep(3000);
int number = r.Next(0, 100);
Console.WriteLine("The random number is now " + number);
// Send new random number to connected subscribers here
await testHub.Clients.All.SendAsync($"ReceiveRandomNumber", number);
}
}
public void Stop()
{
_continue = false;
}
}
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.AddSignalR();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddSingleton(provider =>
{
var hubContext = provider.GetService<IHubContext<TestHub>>();
var updateRandomNumber = new UpdateRandomNumber(hubContext);
return updateRandomNumber;
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
var updateRandonNumber = app.ApplicationServices.GetService<UpdateRandomNumber>();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
app.UseSignalR(routes =>
{
routes.MapHub<TestHub>("/testHub");
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.