我已使用 Azure Functions 创建了 Azure SignalR 无服务器服务。 我的客户端是一个 .NET 6 WPF 应用程序。
协商功能按预期工作,连接成功建立。
CosmosDBTrigger
、HttpTrigger
和 TimerTrigger
函数也按预期工作。
但是,SignalRTrigger
不起作用,我不明白为什么。
SignalRTrigger函数:
[FunctionName("SignalRTest")]
public async Task SignalRTest([SignalRTrigger("myHub", "messages", "SignalRTest")] InvocationContext invocationContext, string message, ILogger logger)
{
logger.LogInformation($"Receive {message} from {invocationContext.ConnectionId}.");
await Clients.All.SendAsync("signalRTestMessage", message);
}
客户端配置:
connection = new HubConnectionBuilder()
.WithUrl("https://<SiteURL>.azurewebsites.net/api")
.Build();
await connection.StartAsync().ContinueWith(async (e) =>
{
try
{
await connection.InvokeAsync("SignalRTest", "TestMessage");
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
});
异常总是返回错误消息:
Invocation failed, status code 404
我已使用 Azure Functions 中生成的 signalr_extension
key 配置了 SignalR 上游。
我已关注 Microsoft docs 上的官方文档但仍然无法解决问题。
最佳答案
我对你表示同情,因为我们一直在努力发送消息,尽管如此多的文档使它显得微不足道。我对您的情况的一个想法是:创建 hubConnection 时是否应该使用协商响应中的 URL?
下面是我们的 typescript 示例,该示例使用 NegotiateResponse 对象以及从协商 HTTP 调用返回的 URL 和 AccessToken:
this.signalRService.negotiate().subscribe({
next: (negotiateResponse) => {
let options = {
accessTokenFactory: () => negotiateResponse.accessToken,
};
const connection = new signalR.HubConnectionBuilder()
.withUrl(negotiateResponse.url, options)
.build();
附注如前所述,我们也在消息发送方面遇到困难,因此这可能不是您的解决方案,尽管我希望如此。
关于c# - Azure SignalRTrigger 无法与 Azure Functions 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74287409/