SignalR .NET Client 连接到 Blazor .NET Core 3 应用程序中的 Azure Sig



我正在尝试在我的 ASP.NET Core 3.0 Blazer(服务器端(应用程序和 Azure SignalR 服务之间建立连接。我最终会将我的 SignalR 客户端(服务(注入到几个 Blazor 组件中,以便它们实时更新我的 UI/DOM。

我的问题是,当我在集线器连接上调用我的.StartAsync()方法时,我收到以下消息:

响应状态代码不指示成功:404(未找到(。

BootstrapSignalRClient.cs

此文件加载我的 SignalR 服务配置,包括 URL、连接字符串、密钥、方法名称和中心名称。这些设置在静态类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;
}
}

信号RS服务配置.cs

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; }
}

信号.cs

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 一起使用的经验,您可以在其中添加它,以便使用.AddSignalR().AddAzureSignalR()并在应用程序配置中映射中心的Startup.cs文件,并且这样做需要建立某些"配置"参数(即连接字符串(。

鉴于我的情况,HubConnectionBuilder从何处获取用于向 SignalR 服务进行身份验证的连接字符串或密钥?

404 消息是否有可能是缺少密钥/连接字符串的结果?

好的,事实证明文档在这里缺少关键信息。如果使用连接到 Azure 信号服务的 .NET 信号客户端,则需要请求 JWT 令牌,并在创建中心连接时提供该令牌。

如果需要代表用户进行身份验证,可以使用此示例。

否则,可以使用 Web API(如 Azure 函数(设置"/negotiate"终结点,以为你检索 JWT 令牌和客户端 URL;这就是我最终为我的用例所做的。可在此处找到有关创建 Azure 函数以获取 JWT 令牌和 URL 的信息。

我创建了一个类来保存这两个值:

SignalRConnectionInfo.cs

public class SignalRConnectionInfo
{
[JsonProperty(PropertyName = "url")]
public string Url { get; set; }
[JsonProperty(PropertyName = "accessToken")]
public string AccessToken { get; set; }
}

我还在SignalRService中创建了一个方法,用于处理与 Azure 中 Web API 的"/negotiate"终结点的交互、中心连接的实例化以及使用事件 + 委托接收消息,如下所示:

信号.cs

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();
}

functionsClient只是一个预配置了基 URL 的强类型HttpClientFunctionsClientConstants.SignalR是一个静态类,其中包含附加到基 URL 的"/negotiate"路径。

一旦我设置好了这一切,我就打电话给await hubConnection.StartAsync();,它"连接"了!

完成所有这些之后,我设置了一个静态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 服务团队提供了文档更新,当然希望这对其他人有所帮助!

更新:管理SDK(示例(已弃用包含无服务器示例的示例。管理 SDK 使用协商服务器。

最新更新