[英]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
This is the address of his article https://www.dotnetcurry.com/aspnet-core/1480/aspnet-core-vuejs-signalr-app这是他的文章https的地址://www.dotnetcurry.com/aspnet-core/1480/aspnet-core-vuejs-signalr-app
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.