我可以同时保留对IOptions <myPoco>和myPocoObject的更改吗?

时间:2019-11-29 15:07:51

标签: c# asp.net-core

我使用asp.net core 3的StaticFiles功能。我通过appsettings.json配置所需的路径。我希望用户能够定义静态和相对路径。然后,我想在Startup.Configure()中创建目录(如果它不存在),并用永久静态路径替换值。

现在这是我的设置。我创建了要注入相关服务/控制器中的POCO类,因此我不依赖IOptions:

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    // Add the configuration via Ioptions<> or IoptionsMonitor<> or IoptionsSnapshot<>
    services.Configure<AppSettings>(Configuration);
    services.Configure<ShiftBatchControllerOptions>(Configuration.GetSection(nameof(ShiftBatchControllerOptions)));
    services.Configure<StaticFileConfigSection>(Configuration.GetSection(nameof(StaticFileConfigSection)));

    // Explicitly register the settings object by delegating to the IOptions object
    // See: https://andrewlock.net/adding-validation-to-strongly-typed-configuration-objects-in-asp-net-core/
    // TODO add validation for configuration, see: https://andrewlock.net/adding-validation-to-strongly-typed-configuration-objects-in-asp-net-core/
    services.AddSingleton(resolver => resolver.GetRequiredService<IOptions<AppSettings>>().Value);
    services.AddSingleton(resolver => resolver.GetRequiredService<IOptions<ShiftBatchControllerOptions>>().Value);
    services.AddSingleton(resolver => resolver.GetRequiredService<IOptions<StaticFileConfigSection>>().Value);

    // Tried the following - did not work.
    //var y = new StaticFileConfigSection();
    //Configuration.GetSection(nameof(StaticFileConfigSection)).Bind(y); // TODO
    //services.AddSingleton(y);

然后,我尝试使用完整的静态路径字符串重写路径属性:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IOptionsMonitor<AppSettings> appSettingsAccessor, ILogger<Startup> logger, StaticFileConfigSection staticFileConfig)
        {
            //var appSettings = appSettingsAccessor?.Value ?? throw new ArgumentNullException(nameof(appSettingsAccessor));

            app.UseOpenApi(); // serve OpenAPI/Swagger documents
            app.UseSwaggerUi3(); // serve Swagger UI 

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();

                // See: https://docs.microsoft.com/nl-nl/aspnet/core/client-side/using-browserlink?view=aspnetcore-3.0
                app.UseBrowserLink();
            }

            // For static files in ../StaticFiles
            var staticFilesDirectoryInfo = Directory.CreateDirectory(Path.GetFullPath(staticFileConfig.StaticFileDirFullPath, PathHelpers.CurrentOutPutDirectory));

            // Rewrite the full path to the config - for easy of access.
            // NOTE that the object gets updated and this updated object will get injected in the controller.
            staticFileConfig.StaticFileDirFullPath = staticFilesDirectoryInfo.FullName;
            appSettingsAccessor.CurrentValue.StaticFileConfigSection.StaticFileDirFullPath = staticFilesDirectoryInfo.FullName;

            // For static files in wwwroot
            app.UseStaticFiles(); 

            app.UseFileServer(new FileServerOptions
            {
                FileProvider = new PhysicalFileProvider(staticFilesDirectoryInfo.FullName),
                RequestPath = staticFileConfig.StaticFileRequestPath,
                EnableDirectoryBrowsing = true
            });

请注意以下几点:

  • 如果我设置POCO对象(StaticFileConfigSection类)的值,然后在控制器中访问它,则在Configure中所做的更改将成功持久化。
  • 访问IOptions,IOptionsMonitor或IOptionsSnapShot没有更改:

enter image description here

我该如何做到: a)我可以使用普通的POCO对象和IOptions <> b)编辑配置(以后处理方式),并使更改在控制器/其他服务中可见吗?

0 个答案:

没有答案