0

我正在尝试做的事情:我已经使用 .net core 3.1 mvc Web 应用程序在 Azure App Configuration 中设置了带有哨兵键的 Azure App Configuration,目标是当我更新不同键的值及其哨兵时键更新的值应该反映在我的 mvc 应用程序中,而无需刷新页面。

我的问题是:当我在 Program.cs 类的选项依赖注入中使用 RefreshAll: true 执行此操作时,我可以在刷新页面后查看我的应用程序中的更改,但我想在更新后立即查看更改我的应用配置服务中的键值(不刷新页面)

我参考的文档: https ://docs.microsoft.com/en-us/azure/azure-app-configuration/enable-dynamic-configuration-aspnet-core?tabs=core3x#reload-data-from-app-configuration我只使用上面的链接创建了应用程序。 我的环境:使用从 Visual Studio Enterprise 2019 运行的 dot net core 3.1

我的代码:Programe.cs --

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

添加了一个 Setting.cs 类:

namespace TestAppConfig
{
    public class Settings
    {
        public string BackgroundColor { get; set; }
        public long FontSize { get; set; }
        public string FontColor { get; set; }
        public string Message { get; set; }
    }
}

在 Startup.cs 中修改了 ConfigureService 方法:

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<Settings>(Configuration.GetSection("TestApp:Settings"));
    services.AddControllersWithViews();
}

还将配置方法更新为:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/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();
        }

        // Add the following line:
        app.UseAzureAppConfiguration();

        app.UseHttpsRedirection();

        app.UseStaticFiles();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });
}

我的 HomeController 类:

public class HomeController : Controller
{
    private readonly Settings _settings;
    private readonly ILogger<HomeController> _logger;

    public HomeController(ILogger<HomeController> logger, IOptionsSnapshot<Settings> settings)
    {
        _logger = logger;
        _settings = settings.Value;
    }

    public IActionResult Index()
    {
        ViewData["BackgroundColor"] = _settings.BackgroundColor;
        ViewData["FontSize"] = _settings.FontSize;
        ViewData["FontColor"] = _settings.FontColor;
        ViewData["Message"] = _settings.Message;

        return View();
    }

    // ...
}

索引.cshtml:

<!DOCTYPE html>
<html lang="en">
<style>
    body {
        background-color: @ViewData["BackgroundColor"]
    }
    h1 {
        color: @ViewData["FontColor"];
        font-size: @ViewData["FontSize"]px;
    }
</style>
<head>
    <title>Index View</title>
</head>
<body>
    <h1>@ViewData["Message"]</h1>
</body>
</html>

让我知道这是否可以实现以及如何实现。提前致谢

4

1 回答 1

1

在现实世界中,当配置更改时,您的 Web 应用程序的新用户将看到更改。如果您想在不刷新页面的情况下查看更改,则必须使用 javascript 或 AJAX 自动刷新页面,例如在计时器上。当服务器端发生任何更改时,无论是配置还是其他内容,这都没有什么不同。

于 2020-09-24T21:51:19.957 回答