Gilney
Gilney

Reputation: 381

Azure function not sending message to Service Bus

I wrote this Azure Function and I'm able to invoke from a http request:

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

The problem is that message will never hit Service Bus (no errors either). After a lot of tries I looked at the auto-generated function.json (that's actually called function.metadata in my machine) and what I observed is that the "Out biding" section seems wrong. I suppose type should be something like "servicebus" or maybe something like that.

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

I'm using .Net SDK 5.0.301 w/ runtime 5.0.7 and the following imports:

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

Any sugestions?

Upvotes: 2

Views: 935

Answers (1)

user1672994
user1672994

Reputation: 10839

It seems that you are not using the right package for ServiceBus binding. You should use Microsoft.Azure.Functions.Worker.Extensions.ServiceBus package for .net5 SDK for isolated function instead of Microsoft.Azure.WebJobs.Extensions.ServiceBus.

Once you use the right package you will see that functions.metatadata file will have correct bindings. For ex, for below code:

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

The generated binding is

{
    "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"
      }
    ]
 }

You can read this office documentation for further information.

enter image description here

Upvotes: 3

Related Questions