azureservicebusazure-webjobsazure-webjobssdkazure-webjobs-continuous

How do I automate creation of missing ServiceBus topics and queues in a .NET 7 WebJobs project?


I'm migrating a WebJobs project from .NET 4.6 to .NET 7 and I came across a quite significant problem - my new project does not seem to create missing topics and queues in the ServiceBus namespace. I can't find a way to force it to do so either.

I found AccessRights argument in ServiceBusTriggerAttribute but that's not exposed by Microsoft.Azure.WebJobs.Extensions.ServiceBus.

I also tried tweaking configuration in AddServiceBus(this IWebJobsBuilder builder, Action<ServiceBusOptions> configure) but it doesn't seem to expose anything relevant to provisioning these topics/queues.

Am I missing something here or is there no way to automate it and I'll need to provision them myself?

P.S. I'm authenticating using a shared access key with "manage" permissions (just FYI)

#EDIT I also found this issue raised against the WebJobs SDK. Unfortunately there's no explanation of any recommended approach in place of the AccessRights option - https://github.com/Azure/azure-webjobs-sdk/issues/1509


Solution

  • In the newer versions of the Microsoft.Azure.WebJobs.Extensions.ServiceBus package, there is no built-in capability to automatically create missing Service Bus topics and queues. You need to provision them manually.

    To automate the creation of missing Service Bus topics and queues in a .NET 7 WebJobs project, you can leverage the Service Bus management libraries provided by Azure SDK.

    And I couldn't find any information on how to force the creation of missing topics and queues in the ServiceBus namespace in .NET 7 WebJobs project.

    Code reference taken from Github code.

    using Microsoft.Azure.ServiceBus.Management;
    
    string connectionString = "ConnectionString";
    string topic_Name = "mytopic11";
    string queue_Name = "myqueue11";
    
    var mgmtClnt = new ManagementClient(connectionString);
    if (!await mgmtClnt.TopicExistsAsync(topic_Name))
    {
        await mgmtClnt.CreateTopicAsync(new TopicDescription(topic_Name));
        Console.WriteLine($"Topic '{topic_Name}' created successfully.");
    }
    else
    {
        Console.WriteLine($"Topic '{topic_Name}' already exists.");
    }
    if (!await mgmtClnt.QueueExistsAsync(queue_Name))
    {
        await mgmtClnt.CreateQueueAsync(new QueueDescription(queue_Name));
        Console.WriteLine($"Queue '{queue_Name}' created successfully.");
    }
    else
    {
        Console.WriteLine($"Queue '{queue_Name}' already exists.");
    }
    await mgmtClnt.CloseAsync();
    

    .CSProj file

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net7.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="Microsoft.Azure.ServiceBus" Version="5.2.0" />
      </ItemGroup>
    </Project>
    

    enter image description here

    enter image description here

    For further information, refer to the MSDoc1.