-
Notifications
You must be signed in to change notification settings - Fork 6k
Document Azure SDK subclient registration #36555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
scottaddie
merged 28 commits into
dotnet:main
from
alexwolfmsft:dependency-injection-subclients
Aug 11, 2023
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
26f27b9
initial commit
alexwolfmsft 1b917a9
service bus integration
alexwolfmsft b806cad
removed extra file
alexwolfmsft 4e79de1
merge
alexwolfmsft a1da61a
fixed conflicts
alexwolfmsft f51aeda
fixed csproj
alexwolfmsft e819db1
Fixed link
alexwolfmsft c13afef
fix version
alexwolfmsft 59a3c08
updates
alexwolfmsft 19966c6
updates
alexwolfmsft 7562851
updates
alexwolfmsft 1aac173
fix highlights
alexwolfmsft fe15f92
refactor
alexwolfmsft 7252794
fix package
alexwolfmsft a1512a9
fix line numbers
alexwolfmsft 74180da
fix
alexwolfmsft 51a5d9e
fix
alexwolfmsft c273496
Apply suggestions from code review
alexwolfmsft 7c4ec5d
merge
alexwolfmsft 7ec5795
Merge branch 'dependency-injection-subclients' of https://github.com/…
alexwolfmsft bf5935c
tweaks
alexwolfmsft 50d2905
Apply suggestions from code review
alexwolfmsft d228890
pr feedback
alexwolfmsft 043df3f
Merge branch 'dependency-injection-subclients' of https://github.com/…
alexwolfmsft 95a396f
Apply suggestions from code review
alexwolfmsft 9e2f93b
removed namespaceuri
alexwolfmsft 11c330c
Apply suggestions from code review
alexwolfmsft 8e9b8a6
Apply suggestions from code review
alexwolfmsft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 44 additions & 8 deletions
52
docs/azure/sdk/snippets/dependency-injection/HostApplicationBuilder/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,51 @@ | ||
using Azure.Identity; | ||
using Azure.Messaging.ServiceBus; | ||
using Azure.Messaging.ServiceBus.Administration; | ||
using Microsoft.Extensions.Azure; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args); | ||
List<string> queueNames = await GetQueueNames(); | ||
|
||
builder.Services.AddAzureClients(clientBuilder => | ||
{ | ||
clientBuilder.AddSecretClient(new Uri("<key_vault_url>")); | ||
clientBuilder.AddBlobServiceClient(new Uri("<storage_url>")); | ||
clientBuilder.UseCredential(new DefaultAzureCredential()); | ||
}); | ||
IHost host = Host.CreateDefaultBuilder(args) | ||
.ConfigureServices(services => | ||
{ | ||
services.AddAzureClients(clientBuilder => | ||
{ | ||
// Register clients for each service | ||
clientBuilder.AddSecretClient(new Uri("<key_vault_url>")); | ||
clientBuilder.AddBlobServiceClient(new Uri("<storage_url>")); | ||
clientBuilder.AddServiceBusClientWithNamespace("<your_namespace>.servicebus.windows.net"); | ||
clientBuilder.UseCredential(new DefaultAzureCredential()); | ||
|
||
// Register subclients for Service Bus | ||
foreach (string queueName in queueNames) | ||
{ | ||
clientBuilder.AddClient<ServiceBusSender, ServiceBusClientOptions>((_, _, provider) => | ||
provider.GetService(typeof(ServiceBusClient)) switch | ||
{ | ||
ServiceBusClient client => client.CreateSender(queueName), | ||
_ => throw new InvalidOperationException("Unable to create ServiceBusClient") | ||
}).WithName(queueName); | ||
} | ||
}); | ||
}).Build(); | ||
|
||
using IHost host = builder.Build(); | ||
await host.RunAsync(); | ||
|
||
async Task<List<string>> GetQueueNames() | ||
{ | ||
// Query the available queues for the Service Bus namespace. | ||
var adminClient = new ServiceBusAdministrationClient | ||
("<your_namespace>.servicebus.windows.net", new DefaultAzureCredential()); | ||
var queueNames = new List<string>(); | ||
|
||
// Because the result is async, the queue names need to be captured | ||
// to a standard list to avoid async calls when registering. Failure to | ||
// do so results in an error with the services collection. | ||
await foreach (QueueProperties queue in adminClient.GetQueuesAsync()) | ||
{ | ||
queueNames.Add(queue.Name); | ||
} | ||
|
||
return queueNames; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 34 additions & 5 deletions
39
docs/azure/sdk/snippets/dependency-injection/HostBuilder/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,49 @@ | ||
using HostBuilder; | ||
#region snippet_HostBuilder | ||
#region snippet_HostBuilder | ||
using Azure.Identity; | ||
using Azure.Messaging.ServiceBus; | ||
using Azure.Messaging.ServiceBus.Administration; | ||
using Microsoft.Extensions.Azure; | ||
|
||
List<string> queueNames = await GetQueueNames(); | ||
|
||
IHost host = Host.CreateDefaultBuilder(args) | ||
.ConfigureServices(services => | ||
{ | ||
services.AddHostedService<Worker>(); | ||
services.AddAzureClients(clientBuilder => | ||
{ | ||
// Register clients for each service | ||
clientBuilder.AddSecretClient(new Uri("<key_vault_url>")); | ||
clientBuilder.AddBlobServiceClient(new Uri("<storage_url>")); | ||
clientBuilder.AddServiceBusClientWithNamespace("<your_namespace>.servicebus.windows.net"); | ||
clientBuilder.UseCredential(new DefaultAzureCredential()); | ||
|
||
// Register a subclient for each Service Bus Queue | ||
foreach (string queue in queueNames) | ||
{ | ||
clientBuilder.AddClient<ServiceBusSender, ServiceBusClientOptions>((_, _, provider) => | ||
provider.GetService<ServiceBusClient>().CreateSender(queue) | ||
).WithName(queue); | ||
} | ||
}); | ||
}) | ||
.Build(); | ||
}).Build(); | ||
|
||
await host.RunAsync(); | ||
|
||
async Task<List<string>> GetQueueNames() | ||
{ | ||
// Query the available queues for the Service Bus namespace. | ||
var adminClient = new ServiceBusAdministrationClient | ||
("<your_namespace>.servicebus.windows.net", new DefaultAzureCredential()); | ||
var queueNames = new List<string>(); | ||
|
||
// Because the result is async, the queue names need to be captured | ||
// to a standard list to avoid async calls when registering. Failure to | ||
// do so results in an error with the services collection. | ||
await foreach (QueueProperties queue in adminClient.GetQueuesAsync()) | ||
{ | ||
queueNames.Add(queue.Name); | ||
} | ||
|
||
return queueNames; | ||
} | ||
#endregion snippet_HostBuilder |
35 changes: 34 additions & 1 deletion
35
docs/azure/sdk/snippets/dependency-injection/WebApplicationBuilder/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.