question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[FEATURE REQ] Enable dependency injection for ServiceBusAdministrationClient

See original GitHub issue

Library name

Azure.Messaging.ServiceBus

Please describe the feature.

Hi,

In Compatability/ServiceBusClientBuilderExtensions.cs in the Azure.Messaging.ServiceBus.sln we have a method to register a ServiceBusClient.

    /// <summary>
    ///   Registers a <see cref="ServiceBusClient"/> instance with the provided <paramref name="fullyQualifiedNamespace"/>.
    /// </summary>
    ///
    public static IAzureClientBuilder<ServiceBusClient, ServiceBusClientOptions> AddServiceBusClientWithNamespace<TBuilder>(this TBuilder builder, string fullyQualifiedNamespace)
        where TBuilder : IAzureClientFactoryBuilderWithCredential
    {
        return builder.RegisterClientFactory<ServiceBusClient, ServiceBusClientOptions>((options, token) => new ServiceBusClient(fullyQualifiedNamespace, token, options));
    }

Which allows registering it for DI via the following method

 public static IServiceCollection AddServiceBusUtil(this IServiceCollection services, IConfiguration config)
    {
        _ = config ?? throw new ArgumentNullException(nameof(config));

        services.AddAzureClients(builder => {
            builder.AddServiceBusClientWithNamespace(config.GetConnectionString("ServiceBus"));

        });
        return services;
    }

Which is great and makes managing client / client lifetime a lot easier.

Perhaps I missed another extension method, but it appears that there is no equivalent for it for the ServiceBusAdministrationClient

It appears that using ServiceBusAdministrationClient ServiceBusAdministrationClientOptions would yield the same result. Is there more to it than adding a new extension method like this?

I’ve tested it out in my own application and it appears doing the same trick would be enough.

services.AddAzureClients(builder => {
            builder.AddServiceBusClientWithNamespace(config.GetConnectionString("ServiceBus"));
            (builder as IAzureClientFactoryBuilderWithCredential).RegisterClientFactory<ServiceBusAdministrationClient, ServiceBusAdministrationClientOptions>((options, token) => new ServiceBusAdministrationClient(config.GetConnectionString("ServiceBus"), token, options));
});

If it is a case of creating the same set of operations ServiceBusAdministrationClient offers I am more than happy to create a PR to support that.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
kaylumahcommented, Jan 10, 2022

Thanks for the help resolving this @JoshLove-msft

1reaction
jsquirecommented, Dec 30, 2021

Thank you for your feedback. Tagging and routing to the team member best able to assist.

Read more comments on GitHub >

github_iconTop Results From Across the Web

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 ...
Read more >
Dependency injection with the Azure SDK for .NET
Learn how to use dependency injection with the Azure SDK for .NET client libraries.
Read more >
service-bus-performance-improvements.md
Service Bus client objects, such as implementations of IQueueClient or IMessageSender , should be registered for dependency injection as singletons (or ...
Read more >
How to Use Managed Identities with Azure Service Bus
Azure Service Bus supports managed identities, a Microsoft Azure feature that allows apps to authenticate or authorize themselves with Azure ...
Read more >
Azure Functions Dependency Injection
First we need to make a Startup where all dependencies will be registered before they can be injected. Here I'll configure a singleton...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found