Azure / azure-sdk-for-net

This repository is for active development of the Azure SDK for .NET. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/dotnet/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-net.
MIT License
5.36k stars 4.77k forks source link

Microsoft.Extensions.Azure 1.7.1: Code doesn't compile due to potentially outdated documentation #39935

Open sgabler opened 10 months ago

sgabler commented 10 months ago

The documentation seems outdated. I pasted the code from the docs, but it doesn't compile for me.

image

Here is my Program.cs

using Azure.Core;
using Azure.Identity;
using Microsoft.Extensions.Azure;

var host = Host.CreateDefaultBuilder(args)
    .ConfigureAppConfiguration((hostingContext, config) =>
    {
        config.Sources.Clear();

        config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
        config.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true);
        config.AddJsonFile("appsettings.k8s.json", optional: true, reloadOnChange: false);
    })
    .ConfigureServices((hostContext, services) =>
    {
        // Registering policy to use in ConfigureDefaults later
        services.AddSingleton<DependencyInjectionEnabledPolicy>();

        services.AddAzureClients(builder => {
            // Register blob service client and initialize it using the KeyVault section of configuration
            builder.AddSecretClient(Configuration.GetSection("KeyVault"))
                // Set the name for this client registration
                .WithName("NamedBlobClient")
                // Set the credential for this client registration
                .WithCredential(new ClientSecretCredential("<tenant_id>", "<client_id>", "<client_secret>"))
                // Configure the client options
                .ConfigureOptions(options => options.Retry.MaxRetries = 10);

            // Adds a secret client using the provided endpoint and default credential set later
            builder.AddSecretClient(new Uri("http://my.keyvault.com"));

            // Configures environment credential to be used by default for all clients that require TokenCredential
            // and doesn't override it on per registration level
            builder.UseCredential(new EnvironmentCredential());

            // This would use configuration for auth and client settings
            builder.ConfigureDefaults(hostContext.Configuration.GetSection("Default"));

            // Configure global retry mode
            builder.ConfigureDefaults(options => options.Retry.Mode = RetryMode.Exponential);

            // Advanced configure global defaults
            builder.ConfigureDefaults((options, provider) => options.AddPolicy(provider.GetService<DependencyInjectionEnabledPolicy>(), HttpPipelinePosition.PerCall));

            // Register blob service client and initialize it using the Storage section of configuration
            builder.AddBlobServiceClient(Configuration.GetSection("Storage"))
                .WithVersion(BlobClientOptions.ServiceVersion.V2019_02_02);
        });
    })
    .Build();

await host.RunAsync();

My csproj file:

<Project Sdk="Microsoft.NET.Sdk.Worker">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
    <!-- Code Analysis (https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/overview) - BEGIN -->
    <EnableNETAnalyzers>true</EnableNETAnalyzers>
    <AnalysisMode>Default</AnalysisMode>
    <AnalysisLevel>latest</AnalysisLevel>
    <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
    <!-- Code Analysis - END -->
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="IDisposableAnalyzers" Version="4.0.7">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.Extensions.Azure" Version="1.7.1" />
    <PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
  </ItemGroup>

  <ItemGroup>
    <Content Include="..\.dockerignore">
      <Link>.dockerignore</Link>
    </Content>
    <Content Update="appsettings.Development.json">
      <DependentUpon>appsettings.json</DependentUpon>
    </Content>
  </ItemGroup>

</Project>

Could you please take a look at what might be wrong here?


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

jsquire commented 10 months ago

Hi @sgabler. Thank you for reaching out and we regret that you're experiencing difficulties. That section of the documentation attempts to illustrate how to perform various activities using the library which apply to different scenarios. It is not intended that you use the entire snippet as a copy/paste in an application, rather it is intended to serve as a reference for what patterns work so that you can apply them to your scenario. A full end-to-end sample is available here which allows you explore more directly.

That all said, I agree that the documentation does not make this clear; we'll look at getting that updated to clarify.

jsquire commented 10 months ago

@AlexanderSher: Would you please look into updating the README to clarify the intent of the snippet, note that it does not work copy/paste, and reference the sample?

sgabler commented 10 months ago

@jsquire thank you, that sounds like a good solution. The sample you linked helped me solve the issues.