【问题标题】:How can I retrieve AppSettings configuration back in Asp.Net MVC 6?如何在 Asp.Net MVC 6 中检索 AppSettings 配置?
【发布时间】:2015-06-03 00:29:57
【问题描述】:

假设我正在使用新的 DepencyInjection 框架在新的 ASP.Net/vNext 中配置我的类和依赖项。

如何使用,如何获取我的预定义配置设置?

    public void ConfigureServices(IServiceCollection services)
    {
        // Add Application settings to the services container.
        services.Configure<AppSettings>(Configuration.GetSubKey("AppSettings"));

        // Add EF services to the services container.
        services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
        // Add Identity services to the services container.
        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

        // Configure the options for the authentication middleware.
        // You can add options for Google, Twitter and other middleware as shown below.
        // For more information see http://go.microsoft.com/fwlink/?LinkID=532715
        services.Configure<FacebookAuthenticationOptions>(options =>
        {
            options.AppId = Configuration["Authentication:Facebook:AppId"];
            options.AppSecret = Configuration["Authentication:Facebook:AppSecret"];
        });

        services.Configure<MicrosoftAccountAuthenticationOptions>(options =>
        {
            options.ClientId = Configuration["Authentication:MicrosoftAccount:ClientId"];
            options.ClientSecret = Configuration["Authentication:MicrosoftAccount:ClientSecret"];
        });

        // Add MVC services to the services container.
        services.AddMvc();

        services.AddSingleton(a =>
        {
            //AppSettings settingsModel = ?? //GET CONFIGURATION SETTINGS FILLED 

            // TECHNICAL ARTIFICE TO RETRIEVE CURRENT SETTINGS
            //var settingsModel = new AppSettings();
            //var config = Configuration.GetSubKey("AppSettings");
            //foreach (var item in typeof(AppSettings).GetProperties().Where(b => b.CanWrite))
            {
                //item.SetValue(settingsModel, config.Get(item.Name));
            }

            return new FooService(settingsModel);
        });

        //Uncomment the following line to add Web API services which makes it easier to port Web API 2 controllers.
        //You will also need to add the Microsoft.AspNet.Mvc.WebApiCompatShim package to the 'dependencies' section of project.json.
        services.AddWebApiConventions();
    }

【问题讨论】:

  • 也许我误解了这个问题,但我没有看到问题所在。为什么不直接将Configuration.GetSubKey("AppSettings") 传递给 FooService 构造函数?
  • 因为项目中有默认创建的AppSettings类,它只是为了配置,如果我使用Configuration.GetSubKey我会得到一个IConfiguration对象,它意味着我需要手动获取配置值。

标签: c# dependency-injection asp.net-core-mvc appsettings


【解决方案1】:

您可以通过在其构造函数中注入IOptions&lt;AppSettings&gt; DI 服务来获取 FooService 中的 AppSettings。

IOptions&lt;&gt; 接口是选项模型的一部分,用于在您的应用程序中访问 POCO 样式设置(例如:您的 AppSettings)。 在上面的示例中,像 services.Configure&lt;AppSettings&gt;(services.Configure&lt;FacebookAuthenticationOptions&gt;(options =&gt; 这样的调用实际上注册了 DI 服务,而这些 DI 服务又被称为 OptionsManager 的 DI 服务在解析 IOptions&lt;&gt; 的请求时使用。

例子:

public class FooService
{
    private readonly AppSettings _settings;

    public FooService(IOptions<AppSettings> options)
    {
        _settings = options.Options;
    }

    ....
    ....
}

【讨论】:

  • 对于 1.0.0-rc1options.Options 更改为 options.Value
  • 如何在我的代码中使用这个服务? FooService fooService = new FooService()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-27
  • 2022-08-03
相关资源
最近更新 更多