Azure 函数未向服务总线发送消息

时间:2021-06-11 20:41:53

标签: azure-functions .net-5

我编写了这个 Azure 函数,并且可以从 http 请求中调用:

    public static class ZendeskWebhookToServiceBus
    {
        [Function("ZendeskWebhookToServiceBus")]
        [return: ServiceBus("functions", Connection = "ServiceBusConnection")]
        public static string Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req,
            FunctionContext executionContext)
        {
            return "{\"value\": 12345}";
        }
    }

问题是消息永远不会到达服务总线(也没有错误)。经过多次尝试,我查看了自动生成的 function.json(在我的机器中实际上称为 function.metadata),我观察到的是“出价”部分似乎是错误的。我想类型应该类似于“servicebus”或类似的东西。

    "bindings": [
      {
        "name": "req",
        "type": "HttpTrigger",
        "direction": "In",
        "authLevel": "Anonymous",
        "methods": [
          "get",
          "post"
        ]
      },
      {
        "name": "$return",
        "type": "http",
        "direction": "Out"
      }
    ]

我正在使用 .Net SDK 5.0.301 w/runtime 5.0.7 和以下导入:

  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.12" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.0.3" OutputItemType="Analyzer" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.1.0" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="4.3.0" />
  </ItemGroup>

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

您似乎没有使用正确的包进行 ServiceBus 绑定。您应该将 Microsoft.Azure.Functions.Worker.Extensions.ServiceBus 包用于 .net5 SDK 以实现隔离功能,而不是 Microsoft.Azure.WebJobs.Extensions.ServiceBus

一旦您使用了正确的包,您将看到 functions.metadata 文件将具有正确的绑定。例如,对于以下代码:

using System;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;

namespace Extensions
{
    public static class HttpInputServiceBusOutput
    {
        [Function("HttpInputServiceBusOutput")]
        [ServiceBusOutput("outputQueue", Connection = "ServiceBusConnection")]
        public static string Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req,
            FunctionContext executionContext)
        {
            var logger = executionContext.GetLogger("ServiceBusFunction");

            logger.LogInformation("Service Bus");

            var message = $"Output message created at {DateTime.Now}";
            return message;
        }
    }
}

生成的绑定是

{
    "name": "HttpInputServiceBusOutput",
    "scriptFile": "Extensions.dll",
    "entryPoint": "Extensions.HttpInputServiceBusOutput.Run",
    "language": "dotnet-isolated",
    "properties": {
      "IsCodeless": false
    },
    "bindings": [
      {
        "name": "req",
        "type": "HttpTrigger",
        "direction": "In",
        "authLevel": "Function",
        "methods": [
          "get",
          "post"
        ]
      },
      {
        "name": "$return",
        "type": "ServiceBus",
        "direction": "Out",
        "queueOrTopicName": "outputQueue",
        "connection": "ServiceBusConnection"
      }
    ]
 }

您可以阅读 this 办公室文档了解更多信息。

enter image description here