Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to register ServiceBusClient for dependency injection?

I’m trying to register ServiceBusClient from the new Azure.Messaging.ServiceBus package for dependency injection as recommended in this article using ServiceBusClientBuilderExtensions, but I can’t find any documentation or any help online on how exactly to go about this.

I'm trying to add as below

public override void Configure(IFunctionsHostBuilder builder)
{
    ServiceBusClientBuilderExtensions.AddServiceBusClient(builder, Typsy.Domain.Configuration.Settings.Instance().Connections.ServiceBusPrimary);
}

but I'm getting the error

The type 'Microsoft.Azure.Functions.Extensions.DependencyInjection.IFunctionsHostBuilder' must be convertible to 'Azure.Core.Extensions.IAzureClientFactoryBuilder' in order to use it as parameter 'TBuilder' in the generic method 'IAzureClientBuilder<ServiceBusClient,ServiceBusClientOptions> Microsoft.Extensions.Azure.ServiceBusClientBuilderExtensions.AddServiceBusClient(this TBuilder, string)'

enter image description here

If anyone can help with this that'll be great!

like image 871
Shanuka Gomes Avatar asked Aug 07 '21 01:08

Shanuka Gomes


3 Answers

ServiceBusClientBuilderExtensions.AddServiceBusClient is an extension method of IAzureClientFactoryBuilder:

public static IAzureClientBuilder<ServiceBusClient, ServiceBusClientOptions> AddServiceBusClient<TBuilder>(this TBuilder builder, string connectionString)
            where TBuilder : IAzureClientFactoryBuilder

To get an instance of IAzureClientFactoryBuilder, you need to call AzureClientServiceCollectionExtensions.AddAzureClients(IServiceCollection, Action<AzureClientFactoryBuilder>) for a given IServiceCollection, which provides a delegate giving an instance of IAzureClientFactoryBuilder. (this method is in the Microsoft.Extensions.Azure NuGet package)

To call that method, you can use the IServiceCollection provided by IFunctionsHostBuilder. With all of that, what you have should look something like:

public override void Configure(IFunctionsHostBuilder builder)
{
    builder.Services.AddAzureClients(clientsBuilder =>
    {
        clientsBuilder.AddServiceBusClient(Typsy.Domain.Configuration.Settings.Instance().Connections.ServiceBusPrimary)
          // (Optional) Provide name for instance to retrieve by with DI
          .WithName("Client1Name")
          // (Optional) Override ServiceBusClientOptions (e.g. change retry settings)
          .ConfigureOptions(options =>
          {
              options.RetryOptions.Delay = TimeSpan.FromMilliseconds(50);
              options.RetryOptions.MaxDelay = TimeSpan.FromSeconds(5);
              options.RetryOptions.MaxRetries = 3;
          });
    });
}

To retrieve the named instance, instead of using ServiceBusClient as the injected type you use IAzureClientFactory<ServiceBusClient>. The ServiceBusClient is a Singleton regardless of whether you use a named instance or not.

 public Constructor(IAzureClientFactory<ServiceBusClient> serviceBusClientFactory)
 {
     // Wherever you need the ServiceBusClient
     ServiceBusClient singletonClient1 = serviceBusClientFactory.CreateClient("Client1Name")
 }
like image 116
devNull Avatar answered Nov 01 '22 13:11

devNull


For those only in need of single servicebus client a simple singleton would suffice

(Tested with .Net 6 Azure Functions v4):

using Azure.Messaging.ServiceBus;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using System;

[assembly: FunctionsStartup(typeof(YourProjName.Startup))]

namespace YourProjName
{
 public class Startup : FunctionsStartup
 {
    public override void Configure(IFunctionsHostBuilder builder)
    {
       
        var serviceBusConnectionString = Environment.GetEnvironmentVariable("ServiceBusConnectionString");
        if (string.IsNullOrEmpty(serviceBusConnectionString))
        {
            throw new InvalidOperationException(
                "Please specify a valid ServiceBusConnectionString in the Azure Functions Settings or your local.settings.json file.");
        }

        //using AMQP as transport
        builder.Services.AddSingleton((s) => {
            return new ServiceBusClient(serviceBusConnectionString, new ServiceBusClientOptions() { TransportType = ServiceBusTransportType.AmqpWebSockets }); 
        });

    }
 }
}

Then you could use it in the Azure Function constructor:

public Function1(ServiceBusClient client)
   {
       _client = client;
   }
like image 30
sergiu Avatar answered Nov 01 '22 12:11

sergiu


I was wondering the exact same thing, and while I like there is a specialized azure extension I did find another way I do prefer that seems less complicated and hopefully this can help others.

The code uses the function delegate provided by .AddSingleton Method. This function delegate will be called At the very end of the Build where you can make use of the service provider and retrieve your options (please correct me if I am wrong as documentation is dense and sparse at the same time :) ) .

Below is the key part:

 serviceCollection.AddSingleton((serviceProvider) =>
            {
                ServiceBusOptions options = serviceProvider.GetService<IOptions<ServiceBusOptions>>().Value;
                return new ServiceBusClient(options.ConnectionString);
            });

**Full Code - speaks more :) **

using Azure.Messaging.ServiceBus; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Options;

namespace ConsoleJson.Example { class Startup { private static IHost DIHost;

    static void Main(string[] args)
    {
        IHostBuilder hostbuilder = Host.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration(GetAppConfigurationDefinition);

        //Create Host (/build configuration)
        ConfigureSettings(hostbuilder);
        ConfigureServices(hostbuilder);

        DIHost = hostbuilder.Build();

        // Application code should start here.
        DIHost.Run();
    }


    static void GetAppConfigurationDefinition(HostBuilderContext ctx, IConfigurationBuilder config)
    {
        config.SetBasePath(Environment.CurrentDirectory)
       .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) // for simplicity appsettings.production.json / appsettings.development.json are not there.  This is where settings would go.
        .Build();
    }

    public static void ConfigureServices(IHostBuilder hostbuilder)
    {
        hostbuilder.ConfigureServices((hostContext, serviceCollection) =>
        {

            serviceCollection.AddSingleton<ServiceBusClient>((serviceProvider) =>
            {
                // options from appSettings.json
                // leverages IOptions Pattern to get an options object from the DIHost Service provider
                var myServiceBusOptions = serviceProvider.GetService<IOptions<ServiceBusOptions>>().Value;

                var sbClientOptions=new ServiceBusClientOptions() { 
                TransportType=ServiceBusTransportType.AmqpTcp,
                RetryOptions=new ServiceBusRetryOptions() { Mode = ServiceBusRetryMode.Exponential }
                // ...
                };

                // returns the ServiceBusClient Object configured per options we wanted
                return new ServiceBusClient(myServiceBusOptions.ConnectionString, sbClientOptions);
            });

        });
    }

    public static void ConfigureSettings(IHostBuilder hostbuilder)
    {
        hostbuilder.ConfigureServices((hostBuilderContext, serviceCollection) =>
        {
            IConfiguration configurationRoot = hostBuilderContext.Configuration;
            serviceCollection.Configure<ServiceBusOptions>(configurationRoot.GetSection("ServiceBusOptions"));
        });
    }

    public class ServiceBusOptions
    {
        public string ConnectionString { get; set; }
    }


}

}

AppSettings.Json

   {
      "ServiceBusOptions": {
        "ConnectionString": ""
      }
    }
like image 37
codea Avatar answered Nov 01 '22 13:11

codea