简体   繁体   English

SignalR、Blazor WebAssembly 应用程序出现问题

[英]Problem with SignalR, Blazor WebAssembly app

I write dynamic application using Blazor WebAssembly.我使用 Blazor WebAssembly 编写动态应用程序。 I register service我注册服务

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

Also added endpoints.MapHub<SimpleClientHub>("hubs/simpleclient");还添加了endpoints.MapHub<SimpleClientHub>("hubs/simpleclient"); endpoint for hub to routing.集线器到路由的端点。

In page Initialization I Add在页面初始化我添加

@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");
    }
}

On running the page, I get an error: Am unhandled error has occurred在运行页面时,我收到一个错误: Am unhandled error has occurred

In dev console:在开发控制台中:

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 

The error is caused by line which try to start hubconnection.该错误是由尝试启动集线器连接的线路引起的。

Edit: Here is a Hub code:编辑:这是一个集线器代码:

public class SimpleClientHub : Hub
{

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

You can refer to this open source project https://github.com/DaniJG/so-signalr.git可以参考这个开源项目https://github.com/DaniJG/so-signalr.git

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有 docker 支持的 Blazor WebAssembly 应用程序 (linux) - Blazor WebAssembly App with docker support (linux) 使用 Blazor WebAssembly 和 WebApi 应用程序设置 Google 登录 - Setting up Google Sign in with Blazor WebAssembly and WebApi app Blazor WebAssembly + Amazon Cognito - Blazor WebAssembly + Amazon Cognito Blazor WebAssembly 断点不起作用 - Blazor WebAssembly Breakpoints not working 将变量声明从 Blazor 页面分离到 Blazor WebAssembly 应用程序中的数据存储库中的 Class - Separating Variable Declarations from a Blazor page into a Class in Data repository in a Blazor WebAssembly App Azure 托管 Blazor WebAssembly 的替代方案 - Azure alternatives for hosting Blazor WebAssembly 将 WebAssembly Blazor 转换为托管的 - Convert WebAssembly Blazor to a Hosted one Blazor WebAssembly 中的伪本地化 - Pseudo-localization in Blazor WebAssembly Blazor webassembly,在应用程序的 OnInitializedAsync 中设置服务,无法进入 Counter.razor - Blazor webassembly, Setting service in App's OnInitializedAsync, can't get in Counter.razor 无法从 Z98AD8B3C99B34CA16F1F7Assembly WebZEE6 本地访问 Azure Function Api - Can't access Azure Function Api locally from Blazor WebAssembly app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM