【问题标题】:415 Unsupported Media Type on Azure Function -> SignalR Service post415 Azure 函数上不支持的媒体类型-> SignalR 服务帖子
【发布时间】:2019-04-12 14:39:29
【问题描述】:

我正在尝试从 Azure 函数向 Azure SignalR 服务发送消息并返回 415,但我不确定如何确定问题的原因。

我已经为应用程序的前端提供了一个工作实现,其中用户和操作能够通过 SignalR 服务广播消息,但是我现在需要一个后端函数来在完成时通知用户。

我所遵循的例子就是这里的例子; https://github.com/Microsoft/MTC_WhackAMole.NET/blob/master/MoleDeploy.SignalR/AzureSignalR.cs

执行函数时,成功完成了SendAsync()函数,但实际上并没有触发任何东西。

var payload = new PayloadMessage()
{
    Target = methodName,
    Arguments = args
};
var url = $"{endpoint}/api/v1/hubs/{hubName}";
var bearer = GenerateJwtBearer(null, url, null, DateTime.UtcNow.AddMinutes(30), accessKey);
await PostJsonAsync(url, payload, bearer);

只有在 Postman 中尝试相同的请求时,我才会收到 415 错误。

在 Postman 中,我发布到以下网址;

https://xxxxxx.service.signalr.net/api/v1/hubs/{hubName}

使用不记名身份验证令牌(我知道这是正确的,因为更改它会生成与 415 相对的 401)

它发送以下有效载荷;

{"Target":"hubFunctionName","Arguments":"message to broadcast"}

我不完全确定的是发布的正文。 Target 应该是 hub 中的函数,而 Arguments 应该是要发送的消息吗?

查看在 PostJsonAsync() 中发送的有效负载,结果如下:

payload {AzureFunctionName.AzureSignalR.PayloadMessage}
--Arguments {object[1]}
----[0] "message to broadcast"
--Target    "hubFunctionName"

hub中的功能超级基础;

public void Send(string messageText)
{
    Clients.All.broadcastMessage(messageText);
}

如果有人能指出我正确的方向,将不胜感激。

谢谢, 汤姆

【问题讨论】:

    标签: c# azure signalr azure-functions signalr-service


    【解决方案1】:

    基于docs,我相信arguments 必须是要发送的消息数组。您能否确认您正在 Postman 中进行相同的测试。

    此外,Azure Functions v2 有一个 Azure SignalR Service Output Binding,您可以直接使用,而不是自己编写此代码。

    【讨论】:

      【解决方案2】:

      感谢您的回复 - 我猜我仍在努力理解这个功能。

      为了扩展,我有一个从队列中读取的基本 Azure 函数,我需要它来向我的信号器服务发送消息。

      队列读取是直接的(并且有效)

      public static void Run([QueueTrigger("signalr-messages", Connection = "Azure.Storage.Main")]string myQueueItem, ILogger log)
      {
          <Deserialise>
          SendMessage(message, null);
      }
      

      我的 SendMessage 函数看起来像;

      public static Task SendMessage(Message message, [SignalR(HubName = "baseHub")]IAsyncCollector<SignalRMessage> signalRMessages)
      {
          return signalRMessages.AddAsync(
          new SignalRMessage
            {
              Target = "sendMessage",
              Arguments = new[] { message }
            });
      }
      

      我在 SendMessage 中传递了“null”,但我知道这是错误的 - 我可以找到的示例来自 HTTP 触发器而不是队列触发器

      我什至走在正确的道路上吗?

      【讨论】:

        猜你喜欢
        • 2021-05-20
        • 2014-05-10
        • 2019-05-01
        • 1970-01-01
        • 2014-09-15
        • 1970-01-01
        • 2017-06-30
        相关资源
        最近更新 更多