Comments (6)
You're saying there's nothing wrong with the way I'm currently doing it though (i.e. with builder.AddClient<ServiceBusClient, ServiceBusClientOptions>)?
Correct. There's nothing wrong with what you're currently doing, and it seems like the best approach for your specific scenario of not knowing the configuration that you want upfront. What you're doing will register the client and ensure proper cleanup.
from azure-sdk-for-net.
Thank you for your feedback. Tagging and routing to the team member best able to assist.
from azure-sdk-for-net.
Hi @cjablonski76. You are correct in that there is no overload that injects an IServiceProvider
and the factory approach that you're using is the recommended way to do so. The resulting factory is invoked on-demand and the resulting client instance is cached and cleaned up.
If your goal is to provide configuration to the client or overload its options, there are other more direct ways to do so.
For example, to create a client based on configuration, the recommended approach is:
var appBuilder = WebApplication.CreateBuilder(args);
var serviceCollection = appBuilder.Services;
serviceCollection.AddAzureClients(builder =>
{
builder.AddServiceBusClient(appBuilder.Configuration.GetSection("ServiceBus");
}
Overriding client options can be done in the following way, if you'd like to avoid the factory:
var appBuilder = WebApplication.CreateBuilder(args);
var serviceCollection = appBuilder.Services;
serviceCollection.AddAzureClients(builder =>
{
builder
.AddServiceBusClientWithNamespace("<your_namespace>.servicebus.windows.net");
.ConfigureOptions(options =>
{
var myOptions = new MyOptions();
appBuilder.Configuration["MyOptions"].Bind(myOptions);
options.TransportType = myOptions.ServiceBusTransportType;
});
}
More discussion and examples can be found in Dependency injection with the Azure SDK for .NET.
from azure-sdk-for-net.
Hi @cjablonski76. Thank you for opening this issue and giving us the opportunity to assist. We believe that this has been addressed. If you feel that further discussion is needed, please add a comment with the text "/unresolve" to remove the "issue-addressed" label and continue the conversation.
from azure-sdk-for-net.
@jsquire, the more in depth problem I have is that the configuration I'm trying to load has two possible service bus connections configured. So something like:
{
"PrimaryConnection" : {
"Namespace": "testing-primary-centralus.servicebus.windows.net"
},
"FailoverConnection": {
"Namespace": "testing-failover-centralus.servicebus.windows.net"
}
}
And deciding which service bus connection to use depends on some other DI dependency, so I end up doing something like:
serviceCollection
.AddOptions<ServiceBusOptions>()
.Configure<IConnectionPicker>((serviceBusOptions, connPicker) =>
{
var connectionToUse = connPicker.GetConnectionNameBasedOnPodLocation(); // return "Primary" or "Secondary"
configurationSection.GetSection(connectionToUse).Bind(serviceBusOptions);
});
serviceCollection.AddAzureClients(builder =>
{
builder.AddClient<ServiceBusClient, ServiceBusClientOptions>((options, provider) =>
{
var serviceBusOptions =
provider.GetRequiredService<IOptions<ServiceBusOptions>>().Value;
return new ServiceBusClient(serviceBusOptions.Namespace, new DefaultAzureCredential(), options);
}
}
So it would have been convenient if to have access to the IServiceProvider
in some AddServiceBus*
methods so that I could stick to those methods without re-designing my above flow.
I think to accomplish what I'm talking about while using the AddServiceBus*
methods, I would need to register both ServiceBusClient
instances and name them appropriately, then I would push my IConnectionPicker
down into the class that depends on ServiceBusClient
and grab the correct instance with IAzureClientFactory<ServiceBusClient>.CreateClient(connPicker.GetConnectionNameBasedOnPodLocation());
You're saying there's nothing wrong with the way I'm currently doing it though (i.e. with builder.AddClient<ServiceBusClient, ServiceBusClientOptions>
)?
from azure-sdk-for-net.
Awesome, thanks for the help!
from azure-sdk-for-net.
Related Issues (20)
- Scopes not being included by default in the OpenTelemetry distro may be insufficiently documented HOT 2
- ManagedIdentityCredential authentication failed: Response from Managed Identity was successful, but the operation timed out prior to completion[QUERY] HOT 2
- [BUG] One of BlobContainerClient constructor overloads does not set clientSideEncryptionOptions HOT 1
- [BUG] Can't get token to connect to Azure Storage in GovCloud from Visual Studio HOT 2
- Azure AI Open AI Assistant Modify Assistant cannot remove last file [BUG] HOT 2
- Should not check response body when a DELETE LRO return 204 HOT 9
- REST API version? HOT 1
- https://www.facebook.com/nickryanpedralba?mibextid=LQQJ4d HOT 2
- [BUG] Azure.Monitor.OpenTelemetry.Exporter EventId duplicate key exception causes LogRecord to be dropped HOT 2
- [BUG] Azure.Monitor.OpenTelemetry.AspNetCore package HOT 3
- Add extension method TryGetSystemEvent to Azure.Messaging.EventGrid.SystemEvents prior to GA
- Invalid model. The model argument should be left blank error HOT 2
- [QUERY] Which exception for Azure.AI.OpenAI 2.0.0-beta, System.ClientModel.ClientResultException or Azure.RequestFailedException? HOT 2
- [BUG] Azure.Search.Documents returns 'Parital Content' Error for semantic hybrid search HOT 2
- [BUG] Synonym Maps >> Index, "modelName" Error HOT 6
- [BUG] Semantic configuration properties for prioritized keyword fields not accepting string subfields of type Collection(Edm.ComplextType) HOT 8
- [BUG] 500 Error when GetToken called in Azure App Service, but not when run locally in VS 2022 HOT 6
- [FEATURE REQ] Set client LocationIP field by default when using Azure.Monitor.OpenTelemetry.AspNetCore HOT 1
- [BUG] Azure Function with UserAssigned ManagedIdentity has a 16% chance to result in Azure.Identity.CredentialUnavailableException HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from azure-sdk-for-net.