0

I have created an Azure function that's triggered by the ServiceBus Queue.

.Net 7 Isolated and Service Bus Queue Trigger

For the message datatype, if I use the "string" data type I can able to process the message in the Function App.

But If I change the type to ServiceBusReceivedMessage it throws an exception. I need to retrieve some information from the header. The actual message is a simple JSON query parameter.

Just logging throws an error.

public void Run([ServiceBusTrigger("myqueue", Connection = "connectionString")] ServiceBusReceivedMessage message)
    {
        _logger.LogInformation("Message ID: {id}", message.MessageId);
        _logger.LogInformation("Message Body: {body}", message.Body);
        _logger.LogInformation("Message Content-Type: {contentType}", message.ContentType);
    }

What am I missing here?

2
  • 1
    1. Share your code w/o omitting important information, such as attributes. 2. What SDK and versions are you using? Most likely, you're not using the right/up-to-date packages. Commented Oct 17, 2023 at 2:19
  • Thanks for the pointer SDK version is the issue here. Commented Oct 17, 2023 at 5:53

1 Answer 1

0

I have reproduced in my environment and below are my expected results:

csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <OutputType>Exe</OutputType>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.19.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.ServiceBus" Version="5.12.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.14.0" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
  <ItemGroup>
    <Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" />
  </ItemGroup>
</Project>

Function.cs:

using System;
using Azure.Messaging.ServiceBus;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;

namespace FunctionApp76
{
    public class Function1
    {
        private readonly ILogger<Function1> _logger;

        public Function1(ILogger<Function1> logger)
        {
            _logger = logger;
        }

        [Function(nameof(Function1))]
        public void Run([ServiceBusTrigger("rithwik", Connection = "rithwikcon")] ServiceBusReceivedMessage message)
        {
            _logger.LogInformation("Message ID: {id}", message.MessageId);
            _logger.LogInformation("Message Body: {body}", message.Body);
            _logger.LogInformation("Message Content-Type: {contentType}", message.ContentType);
        }
    }
}

local.settings.json:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
    "rithwikcon": "Endpoint=sb://rithwik8989.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=ZWQ2pBE="

  }
}

Output:

Sending json and string message both worked for me:

enter image description here

If still this does not work, i would suggest you to raise a support request as the code and process i am doing is right.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the complete code. SDK version is the issue. Microsoft.Azure.Functions.Worker.Sdk I had was 1.10. Once I updated that, It's working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.