我做了什么:
- blazor服务器应用程序模板与VS 2022 + .Net6.0与Windows认证
- 添加简单的Hub
- 在Windows Server 2019 + IIS 10上托管应用程序
(btw在Windows2012R2 + IIS8.5上托管时所有都在工作)
正在工作:
- 所有在我的本地IIS Express…当然:)
- blazor服务器应用程序页面
- 只要客户端是TestConsole应用程序或另一台服务器上的另一个Web应用程序,Hub就可以工作
什么是不是工作:
- HubConnectionBuilder,只要我尝试从blazor应用程序本身连接到它
我也打开了一个(GitHub上的问题),但我希望我在这里得到更多的提示。
我最近尝试的是创建一个新的blazor服务器应用程序没有Windows身份验证只是为了确保我的"客户端代码";HubUrl for HubConnectionBuilder是对的。是的,没有Windows身份验证,一切都像预期的那样工作…
下面是我的代码:TestHub.cs
using Microsoft.AspNetCore.SignalR;
namespace BlazorServerTestHub.Hubs
{
public class TestHub :Hub
{
public async Task SendSomeMessage(int currentCount, string someMessage)
{
await Clients.Others.SendAsync("OnSendMyMessage", currentCount, someMessage);
}
}
}
Programm.cs
using BlazorServerTestHub.Data;
using BlazorServerTestHub.Hubs;
using Microsoft.AspNetCore.Authentication.Negotiate;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
.AddNegotiate();
builder.Services.AddAuthorization(options =>
{
// By default, all incoming requests will be authorized according to the default policy.
options.FallbackPolicy = options.DefaultPolicy;
});
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor(options =>
{
options.DetailedErrors = true;
options.DisconnectedCircuitMaxRetained = 100;
options.DisconnectedCircuitRetentionPeriod = TimeSpan.FromMinutes(3);
options.JSInteropDefaultCallTimeout = TimeSpan.FromMinutes(1);
options.MaxBufferedUnacknowledgedRenderBatches = 10;
}).AddHubOptions(options =>
{
//https://learn.microsoft.com/en-us/aspnet/core/signalr/configuration?view=aspnetcore-6.0&tabs=dotnet#configure-server-options-1
options.EnableDetailedErrors = true;
options.ClientTimeoutInterval = TimeSpan.FromSeconds(60);
options.HandshakeTimeout = TimeSpan.FromSeconds(15);
options.KeepAliveInterval = TimeSpan.FromSeconds(15);
options.MaximumParallelInvocationsPerClient = 1;
options.MaximumReceiveMessageSize = 32 * 1024;
options.StreamBufferCapacity = 10;
});
builder.Services.AddSingleton<WeatherForecastService>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseWebSockets();
app.UseAuthentication();
app.UseAuthorization();
app.MapBlazorHub();
app.MapHub<TestHub>("/mytesthub");
app.MapFallbackToPage("/_Host");
app.Run();
Counter.razor
@page "/counter"
@using Microsoft.AspNetCore.SignalR.Client
@inject NavigationManager navigationManager;
@implements IAsyncDisposable;
<PageTitle>Counter</PageTitle>
<h1>Counter</h1>
<p role="status">Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
<p>@MyMessage</p>
@code {
private int currentCount = 0;
private HubConnection hubConnection = null!;
public string MyMessage { get; set; } = null!;
private async void IncrementCount()
{
currentCount++;
await hubConnection.SendAsync("SendSomeMessage", currentCount, DateTime.Now.ToLongTimeString());
}
protected override async Task OnInitializedAsync()
{
var hubUrl = navigationManager.BaseUri + "mytesthub";
hubConnection = new HubConnectionBuilder()
.WithUrl(hubUrl, options =>
{
options.UseDefaultCredentials = true;
options.SkipNegotiation = true;
options.Transports = Microsoft.AspNetCore.Http.Connections.HttpTransportType.WebSockets;
})
.WithAutomaticReconnect()
.Build();
hubConnection.On<int, string>("OnSendMyMessage", async (counter, msg) =>
{
MyMessage = $"Counter: {counter}! {msg}";
await InvokeAsync(StateHasChanged);
});
await hubConnection.StartAsync();
}
public async ValueTask DisposeAsync()
{
if (hubConnection is not null)
{
await hubConnection.DisposeAsync().ConfigureAwait(false);
}
hubConnection = null!;
}
}
这是我得到的错误信息:
当状态码为"101"时,服务器返回状态码为"401">
我找到了一个解决方案,但不是很满意。也许有人有更好的办法。
如果我在Windows 2019服务器上设置了以下注册表项:
ComputerHKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlLsaDisableLoopbackCheck = 1
那么我的应用程序就像预期的那样工作了。