[英]SignalR .NET Client connecting to Azure SignalR Service in a Blazor .NET Core 3 application
I'm trying to make a connection between my ASP.NET Core 3.0 Blazor (server-side) application and the Azure SignalR Service. 我最终会将我的 SignalR 客户端(服务)注入到一些 Blazor 组件中,以便它们实时更新我的 UI/DOM。
我的问题是当我在集线器连接上调用.StartAsync()
方法时收到以下消息:
响应状态码不表示成功:404(未找到)。
此文件加载我对 SignalR 服务的配置,包括 URL、连接字符串、密钥、方法名称和集线器名称。 这些设置在 static class SignalRServiceConfiguration
中捕获并稍后使用。
public static class BootstrapSignalRClient
{
public static IServiceCollection AddSignalRServiceClient(this IServiceCollection services, IConfiguration configuration)
{
SignalRServiceConfiguration signalRServiceConfiguration = new SignalRServiceConfiguration();
configuration.Bind(nameof(SignalRServiceConfiguration), signalRServiceConfiguration);
services.AddSingleton(signalRServiceConfiguration);
services.AddSingleton<ISignalRClient, SignalRClient>();
return services;
}
}
public class SignalRServiceConfiguration
{
public string ConnectionString { get; set; }
public string Url { get; set; }
public string MethodName { get; set; }
public string Key { get; set; }
public string HubName { get; set; }
}
public class SignalRClient : ISignalRClient
{
public delegate void ReceiveMessage(string message);
public event ReceiveMessage ReceiveMessageEvent;
private HubConnection hubConnection;
public SignalRClient(SignalRServiceConfiguration signalRConfig)
{
hubConnection = new HubConnectionBuilder()
.WithUrl(signalRConfig.Url + signalRConfig.HubName)
.Build();
}
public async Task<string> StartListening(string id)
{
// Register listener for a specific id
hubConnection.On<string>(id, (message) =>
{
if (ReceiveMessageEvent != null)
{
ReceiveMessageEvent.Invoke(message);
}
});
try
{
// Start the SignalR Service connection
await hubConnection.StartAsync(); //<---I get an exception here
return hubConnection.State.ToString();
}
catch (Exception ex)
{
return ex.Message;
}
}
private void ReceiveMessage(string message)
{
response = JsonConvert.DeserializeObject<dynamic>(message);
}
}
我有使用 SignalR 和 .NET Core 的经验,您可以在其中添加它,因此Startup.cs
文件使用.AddSignalR().AddAzureSignalR()
和 Z1D78DC8ED51214E518B5114FE244900 需要在“配置”参数中建立某些集线器,并以这种方式建立集线器即连接字符串)。
鉴于我的情况, HubConnectionBuilder
在哪里获取连接字符串或密钥以对 SignalR 服务进行身份验证?
404 消息是否可能是缺少密钥/连接字符串的结果?
好的,事实证明文档在这里缺少关键信息。 If you're using the .NET SignalR Client connecting to the Azure SignalR Service, you need to request a JWT token and present it when creating the hub connection.
如果您需要代表用户进行身份验证,可以使用此示例。
Otherwise, you can set up a "/negotiate" endpoint using a web API such as an Azure Function to retrive a JWT token and client URL for you; 这就是我最终为我的用例所做的。 有关创建 Azure Function 以获取您的 JWT 令牌和 ZE6B391A8D2C4D65802DZ23A 令牌的信息可以在这里找到。
我创建了一个 class 来保存这两个值:
public class SignalRConnectionInfo
{
[JsonProperty(PropertyName = "url")]
public string Url { get; set; }
[JsonProperty(PropertyName = "accessToken")]
public string AccessToken { get; set; }
}
我还在SignalRService
中创建了一个方法来处理与 Azure 中 web API 的“/negotiate”端点的交互,集线器连接的实例化,以及使用事件 + 委托来接收消息,如下所示:
public async Task InitializeAsync()
{
SignalRConnectionInfo signalRConnectionInfo;
signalRConnectionInfo = await functionsClient.GetDataAsync<SignalRConnectionInfo>(FunctionsClientConstants.SignalR);
hubConnection = new HubConnectionBuilder()
.WithUrl(signalRConnectionInfo.Url, options =>
{
options.AccessTokenProvider = () => Task.FromResult(signalRConnectionInfo.AccessToken);
})
.Build();
}
The functionsClient
is simply a strongly typed HttpClient
pre-configured with a base URL and the FunctionsClientConstants.SignalR
is a static class with the "/negotiate" path which is appended to the base URL.
完成所有设置后,我调用了await hubConnection.StartAsync();
它“连接”了!
毕竟我设置了一个 static ReceiveMessage
事件和一个委托如下(在同一个SignalRClient.cs
):
public delegate void ReceiveMessage(string message);
public static event ReceiveMessage ReceiveMessageEvent;
最后,我实现了ReceiveMessage
委托:
await signalRClient.InitializeAsync(); //<---called from another method
private async Task StartReceiving()
{
SignalRStatus = await signalRClient.ReceiveReservationResponse(Response.ReservationId);
logger.LogInformation($"SignalR Status is: {SignalRStatus}");
// Register event handler for static delegate
SignalRClient.ReceiveMessageEvent += signalRClient_receiveMessageEvent;
}
private async void signalRClient_receiveMessageEvent(string response)
{
logger.LogInformation($"Received SignalR mesage: {response}");
signalRReservationResponse = JsonConvert.DeserializeObject<SignalRReservationResponse>(response);
await InvokeAsync(StateHasChanged); //<---used by Blazor (server-side)
}
我已经向 Azure SignalR 服务团队提供了文档更新,当然希望这对其他人有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.