C# 信号机';SendCoreAsync';如果连接未处于活动状态,则无法调用方法

C# 信号机';SendCoreAsync';如果连接未处于活动状态,则无法调用方法,c#,asp.net,.net-core,razor,signalr,C#,Asp.net,.net Core,Razor,Signalr,我正在开发一个ASP.NET核心web应用程序,在该应用程序中,通过客户端(OrderClient)的用户将能够使用信号集线器(OrderHub)发送和接收消息。我不熟悉剃须刀页面,所以我可能遗漏了一些明显的东西 为简单起见,OrderHub中的UserConnected方法如下所示: public async Task UserConnected(string someString) { log.Info(someString); } public c

我正在开发一个ASP.NET核心web应用程序,在该应用程序中,通过客户端(OrderClient)的用户将能够使用信号集线器(OrderHub)发送和接收消息。我不熟悉剃须刀页面,所以我可能遗漏了一些明显的东西

为简单起见,OrderHub中的UserConnected方法如下所示:

    public async Task UserConnected(string someString)
    {
        log.Info(someString);
    }
public class IndexModel : PageModel
{
    private readonly ILogger<IndexModel> _logger;
    private HubConnection _hubConnection;

    [BindProperty]
    public string Test { get; set; }

    public IndexModel(ILogger<IndexModel> logger)
    {
        _logger = logger;
        _hubConnection = new HubConnectionBuilder().WithUrl("https://localhost:44336/orderhub").WithAutomaticReconnect().Build();
    }

    public async void OnGet()
    {
        await _hubConnection.StartAsync();
    }

    public async Task<IActionResult> OnPostAsync()
    {
        if (!ModelState.IsValid)
        {
            return Page();
        }
        //await _hubConnection.StartAsync();

        await _hubConnection.SendAsync("UserConnected", Test);

        return RedirectToPage("./Index");
    }
}
@page
@model IndexModel
@{
    ViewData["Title"] = "Home page";
}

<form method="post">
    Name:
    <input asp-for="Test" />
    <input type="submit" />
</form>
我的
index.cshtml.cs
如下所示:

    public async Task UserConnected(string someString)
    {
        log.Info(someString);
    }
public class IndexModel : PageModel
{
    private readonly ILogger<IndexModel> _logger;
    private HubConnection _hubConnection;

    [BindProperty]
    public string Test { get; set; }

    public IndexModel(ILogger<IndexModel> logger)
    {
        _logger = logger;
        _hubConnection = new HubConnectionBuilder().WithUrl("https://localhost:44336/orderhub").WithAutomaticReconnect().Build();
    }

    public async void OnGet()
    {
        await _hubConnection.StartAsync();
    }

    public async Task<IActionResult> OnPostAsync()
    {
        if (!ModelState.IsValid)
        {
            return Page();
        }
        //await _hubConnection.StartAsync();

        await _hubConnection.SendAsync("UserConnected", Test);

        return RedirectToPage("./Index");
    }
}
@page
@model IndexModel
@{
    ViewData["Title"] = "Home page";
}

<form method="post">
    Name:
    <input asp-for="Test" />
    <input type="submit" />
</form>
我已经尝试在OrderHub中进行调试,当IndexModel构造函数与OnGet()方法一起调用时,连接已正确建立。但是,当我单击页面上的按钮时,连接已丢失,并显示错误“InvalidOperationException:如果连接未激活,则无法调用'SendCoreAncy'方法”。调试时,集线器的状态也显示为“断开”。我假设我错误地处理了中心的生命周期,但是我看不到如何修复它

如果我每次重新创建连接,代码就会工作,并且集线器会正确接收来自输入字段的文本。但是,由于每次不重新连接时连接都会关闭,因此我假设集线器将无法向客户端发送信息,这是我预计会遇到的问题。这也可能是错误的,客户端能够接收消息-我还没有尝试过