Skip to content

Commit a06979c

Browse files
authored
Update Azure SDK DI doc for additional app builders (#36298)
* Update Azure SDK DI doc for additional app builder * Update section link * Terminate tab group and fix code sample * More improvements * Update line numbers * Add HostBuilder code sample * Update code sample * More edits * Update JSON sample * Code formatting to prevent horizontal scrolling * Link to client registration section * Wrap long lines of code * Shorten API xref link * Remove duplicate package install commands * Add explanation of AzureDefaults section name * React to Alex's feedback * Update verbiage * Add more clarity * Simplify Program.cs
1 parent 479a3fe commit a06979c

File tree

16 files changed

+409
-54
lines changed

16 files changed

+409
-54
lines changed

docs/azure/sdk/dependency-injection.md

Lines changed: 120 additions & 53 deletions
Large diffs are not rendered by default.

docs/azure/sdk/logging.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ Using the Azure Service Bus library as an example, complete the following steps:
154154
155155
### Logging without client registration
156156
157-
There are scenarios in which [registering an Azure SDK library's client with the DI container](dependency-injection.md#register-client) is either impossible or unnecessary:
157+
There are scenarios in which [registering an Azure SDK library's client with the DI container](dependency-injection.md#register-clients) is either impossible or unnecessary:
158158
159159
- The Azure SDK library doesn't include an `IServiceCollection` extension method to register a client in the DI container.
160160
- Your app uses Azure extension libraries that depend on other Azure SDK libraries. Examples of such Azure extension libraries include:
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project>
2+
<PropertyGroup>
3+
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
4+
</PropertyGroup>
5+
<ItemGroup>
6+
<PackageVersion Include="Azure.Identity" Version="1.9.0" />
7+
<PackageVersion Include="Azure.Security.KeyVault.Secrets" Version="4.5.0" />
8+
<PackageVersion Include="Azure.Storage.Blobs" Version="12.17.0" />
9+
<PackageVersion Include="Microsoft.Extensions.Azure" Version="1.6.3" />
10+
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="7.0.9" />
11+
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
12+
<PackageVersion Include="Swashbuckle.AspNetCore" Version="6.5.0" />
13+
</ItemGroup>
14+
</Project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net7.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Azure.Identity" />
12+
<PackageReference Include="Azure.Security.KeyVault.Secrets" />
13+
<PackageReference Include="Azure.Storage.Blobs" />
14+
<PackageReference Include="Microsoft.Extensions.Azure" />
15+
<PackageReference Include="Microsoft.Extensions.Hosting" />
16+
</ItemGroup>
17+
</Project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Azure.Identity;
2+
using Microsoft.Extensions.Azure;
3+
using Microsoft.Extensions.Hosting;
4+
5+
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
6+
7+
builder.Services.AddAzureClients(clientBuilder =>
8+
{
9+
clientBuilder.AddSecretClient(new Uri("<key_vault_url>"));
10+
clientBuilder.AddBlobServiceClient(new Uri("<storage_url>"));
11+
clientBuilder.UseCredential(new DefaultAzureCredential());
12+
});
13+
14+
using IHost host = builder.Build();
15+
await host.RunAsync();
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Worker">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<UserSecretsId>dotnet-HostBuilder-3f144e50-3cf7-463d-b3e7-33f5e9a62474</UserSecretsId>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Azure.Identity" />
12+
<PackageReference Include="Azure.Security.KeyVault.Secrets" />
13+
<PackageReference Include="Azure.Storage.Blobs" />
14+
<PackageReference Include="Microsoft.Extensions.Azure" />
15+
<PackageReference Include="Microsoft.Extensions.Hosting" />
16+
</ItemGroup>
17+
</Project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using HostBuilder;
2+
#region snippet_HostBuilder
3+
using Azure.Identity;
4+
using Microsoft.Extensions.Azure;
5+
6+
IHost host = Host.CreateDefaultBuilder(args)
7+
.ConfigureServices(services =>
8+
{
9+
services.AddHostedService<Worker>();
10+
services.AddAzureClients(clientBuilder =>
11+
{
12+
clientBuilder.AddSecretClient(new Uri("<key_vault_url>"));
13+
clientBuilder.AddBlobServiceClient(new Uri("<storage_url>"));
14+
clientBuilder.UseCredential(new DefaultAzureCredential());
15+
});
16+
})
17+
.Build();
18+
19+
await host.RunAsync();
20+
#endregion snippet_HostBuilder
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"profiles": {
3+
"HostBuilder": {
4+
"commandName": "Project",
5+
"dotnetRunMessages": true,
6+
"environmentVariables": {
7+
"DOTNET_ENVIRONMENT": "Development"
8+
}
9+
}
10+
}
11+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace HostBuilder;
2+
3+
public class Worker : BackgroundService
4+
{
5+
private readonly ILogger<Worker> _logger;
6+
7+
public Worker(ILogger<Worker> logger)
8+
{
9+
_logger = logger;
10+
}
11+
12+
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
13+
{
14+
while (!stoppingToken.IsCancellationRequested)
15+
{
16+
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
17+
await Task.Delay(1000, stoppingToken);
18+
}
19+
}
20+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.Hosting.Lifetime": "Information"
6+
}
7+
}
8+
}

0 commit comments

Comments
 (0)