簡體   English   中英

SignalR、Blazor WebAssembly 應用程序出現問題

[英]Problem with SignalR, Blazor WebAssembly app

我使用 Blazor WebAssembly 編寫動態應用程序。 我注冊服務

services.AddSignalR();
services.AddResponseCompression(opts =>
{
     opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
     new[] { "application/octet-stream" });
});

還添加了endpoints.MapHub<SimpleClientHub>("hubs/simpleclient"); 集線器到路由的端點。

在頁面初始化我添加

@page "/accessevent"

@using Microsoft.AspNetCore.SignalR.Client
@using TimeAttendance.WebApp.Shared.Models;
@inject NavigationManager NavigationManager
<button class="btn btn-primary" @onclick="SendStart">Start</button>

<h3>@Name</h3>
<h3>@InternalNumber</h3>

@code {
    public string Name { get; set; } = "";
    public string InternalNumber { get; set; } = "";

    private HubConnection hubConnection;

    protected override async Task OnInitializedAsync()
    {
        hubConnection = new HubConnectionBuilder()
            .WithUrl(NavigationManager.ToAbsoluteUri("hubs/simpleclient"))
            .Build();  

        hubConnection.On<Message>("SendAccessEvent", (message) =>
        {
            Name = message.Name;
            InternalNumber = message.InternalNumber;
            StateHasChanged();
        });
        
        await hubConnection.StartAsync();
    }


    private async Task SendStart()
    {
        await hubConnection.SendAsync("StartListeningAccessEvents");
    }
}

在運行頁面時,我收到一個錯誤: Am unhandled error has occurred

在開發控制台中:

hubs/simpleclient/negotiate?negotiateVersion=1:1 Failed to load resource: the server responded with a status of 500 ()
blazor.webassembly.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: Response status code does not indicate success: 500 (Internal Server Error).
System.Net.Http.HttpRequestException: Response status code does not indicate success: 500 (Internal Server Error).
  at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode () <0x313fe00 + 0x00052> in <filename unknown>:0 
  at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.NegotiateAsync (System.Uri url, System.Net.Http.HttpClient httpClient, Microsoft.Extensions.Logging.ILogger logger, System.Threading.CancellationToken cancellationToken) <0x3095988 + 0x003d6> in <filename unknown>:0 
  at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.GetNegotiationResponseAsync (System.Uri uri, System.Threading.CancellationToken cancellationToken) <0x30a41a8 + 0x000dc> in <filename unknown>:0 
  at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.SelectAndStartTransport (Microsoft.AspNetCore.Connections.TransferFormat transferFormat, System.Threading.CancellationToken cancellationToken) <0x30a2630 + 0x0024e> in <filename unknown>:0 
  at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.StartAsyncCore (Microsoft.AspNetCore.Connections.TransferFormat transferFormat, System.Threading.CancellationToken cancellationToken) <0x30928b0 + 0x001f4> in <filename unknown>:0 
  at System.Threading.Tasks.ForceAsyncAwaiter.GetResult () <0x3157cb0 + 0x00026> in <filename unknown>:0 
  at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.StartAsync (Microsoft.AspNetCore.Connections.TransferFormat transferFormat, System.Threading.CancellationToken cancellationToken) <0x308ce98 + 0x0010a> in <filename unknown>:0 
  at Microsoft.AspNetCore.Http.Connections.Client.HttpConnectionFactory.ConnectAsync (System.Net.EndPoint endPoint, System.Threading.CancellationToken cancellationToken) <0x3021640 + 0x001be> in <filename unknown>:0 
  at Microsoft.AspNetCore.Http.Connections.Client.HttpConnectionFactory.ConnectAsync (System.Net.EndPoint endPoint, System.Threading.CancellationToken cancellationToken) <0x3021640 + 0x002c8> in <filename unknown>:0 
  at System.Threading.Tasks.ValueTask`1[TResult].get_Result () <0x315c0b8 + 0x00034> in <filename unknown>:0 
  at Microsoft.AspNetCore.SignalR.Client.HubConnection.StartAsyncCore (System.Threading.CancellationToken cancellationToken) <0x301fb50 + 0x00114> in <filename unknown>:0 
  at Microsoft.AspNetCore.SignalR.Client.HubConnection.StartAsyncInner (System.Threading.CancellationToken cancellationToken) <0x2ffcbf0 + 0x002e8> in <filename unknown>:0 
  at System.Threading.Tasks.ForceAsyncAwaiter.GetResult () <0x315e308 + 0x00026> in <filename unknown>:0 
  at Microsoft.AspNetCore.SignalR.Client.HubConnection.StartAsync (System.Threading.CancellationToken cancellationToken) <0x2ffbae0 + 0x0010e> in <filename unknown>:0 
  at TimeAttendance.WebApp.Client.Pages.AccessEvent.OnInitializedAsync () [0x000a2] in C:\Users\Piotr\source\repos\TimeAttendance.WebApp\TimeAttendance.WebApp\Client\Pages\AccessEvent.razor:32 
  at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync () <0x2cf6118 + 0x0013a> in <filename unknown>:0 
  at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask (System.Threading.Tasks.Task taskToHandle) <0x2f5bf08 + 0x000b6> in <filename unknown>:0 

該錯誤是由嘗試啟動集線器連接的線路引起的。

編輯:這是一個集線器代碼:

public class SimpleClientHub : Hub
{

    public async Task StartListeningAccessEvents()
    {
        await Clients.All.SendAsync("SendAccessEvent", 
        new Message { Name = "Piotr", InternalNumber = "InternalNumber"});
    }
}

可以參考這個開源項目https://github.com/DaniJG/so-signalr.git

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM