c# - IApplicationBuilder 不包含 UseAzureAppConfiguration 的定义

标签 c# azure app-config azure-app-configuration

我一直在尝试在我的 Blazor 应用程序中实现功能管理,如下 thisthat但由于某种原因,我的程序拒绝接受“UseAzureAppConfiguration”,即使我应该有正确的使用和包。

这是我的 Startup.cs:

using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Identity.Web;
using Microsoft.Identity.Web.UI;
using Microsoft.FeatureManagement;

namespace OreNoAppu
{
    public class Startup
    {
        public IConfiguration Configuration { get; }

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
                .AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAd"));
            services.AddControllersWithViews().AddMicrosoftIdentityUI();

            // By default, all incoming requests will be authorized according to the default policy
            services.AddAuthorization(options => options.FallbackPolicy = options.DefaultPolicy);

            services.AddRazorPages();
            services.AddServerSideBlazor().AddMicrosoftIdentityConsentHandler();
            services.AddFeatureManagement();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days.You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();
            app.UseAzureAppConfiguration();     // "Does not contain definition" error.

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapBlazorHub();
                endpoints.MapFallbackToPage("/_Host");
            });
        }
    }
}

这些是我的要点:

  <ItemGroup>
    <PackageReference Include="Azure.Identity" Version="1.4.1" />
    <PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.2.0" />
    <PackageReference Include="BlazorPro.Spinkit" Version="1.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.4" NoWarn="NU1605" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.4" NoWarn="NU1605" />
    <PackageReference Include="Microsoft.Extensions.Configuration.AzureAppConfiguration" Version="4.5.0" />
    <PackageReference Include="Microsoft.FeatureManagement.AspNetCore" Version="2.4.0" />
    <PackageReference Include="Microsoft.Identity.Web" Version="1.1.0" />
    <PackageReference Include="Microsoft.Identity.Web.UI" Version="1.1.0" />
  </ItemGroup>

有人知道为什么找不到该方法吗?

最佳答案

Azure 应用程序配置有助于管理应用程序设置并集中控制其访问。它建立在键值对的简单概念之上,并且该服务提供可管理性、可用性和易用性。

要解决您面临的问题,请检查您是否使用了正确的库,并检查您是否拥有 Microsoft 提供的以下 NuGet 包来使用 Azure 应用程序配置资源以及​​使用功能标志的能力在项目中

Microsoft.Azure.AppConfiguration.AspNetCore Microsoft.Extensions.Configuration.AzureAppConfiguration Microsoft.FeatureManagement

由于您通过使用 Startup.cs 中的语句添加了 FeatureManagement 以及 ConfigureServices(...) 内部的功能,因此添加了使用它。检查是否已修改 Program.cs 并将默认主机生成器的创建替换为使用 Azure 应用程序配置的生成器,如下所示。

public  static  IHostBuilder CreateHostBuilder(string[] args) =>
  Host.CreateDefaultBuilder(args)
    .ConfigureWebHostDefaults(webBuilder =>
     webBuilder.ConfigureAppConfiguration((hostingContext, config) =>
     {
        var  settings = config.Build(); 
        config.AddAzureAppConfiguration(options => { 
            options.Connect(Environment.GetEnvironmentVariable("ConnectionString")) 
                    .ConfigureRefresh(refresh => { 
                            refresh.Register("Settings:Sentinel", refreshAll: true).SetCacheExpiration(new  TimeSpan(0, 1, 0)); 
                         }) 
                    .UseFeatureFlags(); 
        }); 
      }) 
      .UseStartup<Startup>());

检查这个How to Add Azure App Configuration and Feature Flags into Blazor in .Net 5文档中包含示例的详分割步说明。

有关更多信息,请参阅以下文档:

关于c# - IApplicationBuilder 不包含 UseAzureAppConfiguration 的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69609041/

相关文章:

c# - 2 列布局循环通过 MVC 模型 c#

c# - 为什么我的 XML 阅读器读取所有其他元素?

c# - 如果作为托管身份运行,您可以创建/删除/更新存储的访问策略吗?

wcf - 使用 'add service reference' 生成的 App.config 中包含什么?

c# - 在 C# 中通过主机名自动加载配置

c# - 如何在 C# 中连接到 .NET Core 中的 Unix 域套接字

c# - EF6 ToListAsync 卡住 Winforms

azure - 配置 Azure 事件网格以进行循环分发

node.js - 从azure网站上的子文件夹托管的nodejs应用程序

c# - 应用程序目录中所有用户的可写 .config 文件?