c# - Azure 函数 SignalR |协商功能|失败并显示 500 错误代码

标签 c# azure azure-functions negotiate azure-signalr

我已使用 extensions.csproj 安装了 Microsoft.Azure.WebJobs.Extensions.SignalRService。

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <WarningsAsErrors />
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.CosmosDB" Version="3.0.1" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator" Version="1.0.*" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.SignalRService" Version="1.0.0" />
  </ItemGroup>
</Project>

我能够使用以下 Azure 函数通过 Azure SignalR 服务发布消息。当 CosmosDB 集合中存在任何 AddOrUpdate() 操作时,该函数会被触发,并且它会在集线器中通过名称“fruitUpdated”发送信号。

#r "Microsoft.Azure.DocumentDB.Core"
#r "Microsoft.Azure.WebJobs.Extensions.SignalRService"
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Microsoft.Azure.Documents;
using Microsoft.Azure.WebJobs.Extensions.SignalRService;

public static void Run(IReadOnlyList<Document> input, ILogger log, ICollector<SignalRMessage> signalRMessages)
{
    if (input != null && input.Count > 0)
    {
        log.LogInformation("Documents modified " + input.Count);
        foreach (var fruit in input)
        {
            signalRMessages.Add( 
                new SignalRMessage 
                {
                    Target = "fruitUpdated",
                    Arguments = new [] { fruit } 
                });
        }
    }
}

但是协商功能现在好像不起作用。它抛出 500 错误代码,没有任何线索。以下是示例,

run.csx

#r "Newtonsoft.Json"

using System;
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;

public static IActionResult Run(HttpRequest req, ILogger log, object connectionInfo)
{
    return (ActionResult)new OkObjectResult(connectionInfo);
}

function.json

{
  "bindings": [
    {
      "authLevel": "anonymous",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "signalRConnectionInfo",
      "name": "connectionInfo",
      "hubName": "flights",
      "direction": "in",
      "connectionStringSetting": "AzureSignalRConnectionString"
    }
  ]
}

我尝试了所有组合。 Azure 文档在某些情况下很烦人!没有输入\输出模式它应该被皱眉。 Azure 产品团队应该认真对待

SignalR 版本彼此不兼容。如果我们在服务器和客户端中使用不同版本的 SignalR dll,它们只是被动地消除消息而不会引发错误。

我想我需要connectionInfo对象的JSON Payload。您能帮我一下吗?

最佳答案

我在这里找到了答案 Serverless360 。原来是类型问题和方法参数修饰。引用SignalService库和装饰器后,它开始工作。在我看来,object 是一个基本类型,它应该已经接收或注入(inject)了connectionInfo。

#r "Newtonsoft.Json"
#r "Microsoft.Azure.WebJobs.Extensions.SignalRService"

using System;
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Microsoft.Azure.WebJobs.Extensions.SignalRService;

public static IActionResult Run(HttpRequest req, ILogger log, [SignalRConnectionInfo(HubName = "flights")]SignalRConnectionInfo connectionInfo)
{
    return (ActionResult)new OkObjectResult(connectionInfo);
}

现在,协商功能正在按预期运行。 希望对有类似问题的人有所帮助!

协商功能的示例输出负载

{"url":"https://myazuresignalrservice.service.signalr.net/client/?hub=myhubname","accessToken":"mybeareraccesstoken"}

更新

问题出在方法签名中,而不是使用 *[SignalRConnectionInfo(HubName = "fruits")]SignalRConnectionInfo*

public static IActionResult Run(HttpRequest req, ILogger log, [SignalRConnectionInfo(HubName = "fruits")]SignalRConnectionInfo connectionInfo) { }

还发布了预期负载供您引用。如果您有任何疑问,请在评论中告诉我。

关于c# - Azure 函数 SignalR |协商功能|失败并显示 500 错误代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55585650/

相关文章:

c# - 静音时 CSCore 环回录音

azure - 类型或命名空间名称 'Diagnostics' 在命名空间 'Microsoft.WindowsAzure' 中不存在

android - 404 未找到 - 仅来自移动设备

azure - 角色遇到错误并已停止

python - 如何在 Azure Functions 中添加和执行二进制文件?

c# - 使用 MessagingCenter 和标准 .NET 事件处理程序来通知相关方更改有什么区别?

c# - 检查多个 boolean 值是否具有相同的值

.net-core - Azure函数错误: BinaryFormatter serialization and deserialization are disabled within this application

json - 在 Azure Functions 中将 F# 记录类型返回为 JSON

c# - 有谁知道如何在 ParallelExtensionExtras 中使用 IOCompletionPortTaskScheduler 和 IOTaskScheduler 以及它们的用途是什么?