【问题标题】:How do you add ConfigurationRoot to Dependency Injection in UWP program?如何在 UWP 程序中将 ConfigurationRoot 添加到依赖注入?
【发布时间】:2019-04-02 12:52:56
【问题描述】:

在我的 UWP 应用程序中,我可以使用 Microsoft.Extensions.Configuration 包来读取 JSON 格式的 appSettings 文件。

    IConfiguration configuration = new ConfigurationBuilder()
        .AddJsonFile("appsettings.json", true, true)
        .AddJsonFile($"appsettings.{App.UWPENVIRONMENT}.json", true, true)
        .Build();
    string clientId = configuration["security:clientId"];
    string authority = configuration["security:authority"];

我现在似乎无法做的是使用 System.Composition 依赖注入使配置对我的 UWP 应用程序的其余部分可用。由于配置对象的构建方式(使用 Build 方法),我无法弄清楚如何将此配置嵌入到某种构造函数中。

想到的一个选择是我可以将接口与对象关联起来,如下所示:

        conventions.ForObject(configuration)
            .Shared()
            .Export(ecb => ecb.AsContractType<IConfiguration>());

但 DI 包似乎不支持这种思维方式。我该如何解决这个问题:

  1. 找到构建类型的方法。
  2. 将接口与具体实例相关联。
  3. 其他

【问题讨论】:

    标签: .net dependency-injection uwp configuration


    【解决方案1】:

    这是创建类型化 ConfigurationRoot 的代码:

        /// <summary>
        /// The configuration for this application.
        /// </summary>
        public class ApplicationConfiguration : ConfigurationRoot
        {
            /// <summary>
            /// The client id of the service application to which we want to connect.
            /// </summary>
    #if PRODUCTION
            private const string UWPENVIRONMENT = "Production";
    #elif STAGING
            private const string UWPENVIRONMENT = "Staging";
    #else
            private const string UWPENVIRONMENT = "Development";
    #endif
    
            /// <summary>
            /// Initializes a new instance of the <see cref="ApplicationConfiguration"/> class.
            /// </summary>
            public ApplicationConfiguration()
                : base(ApplicationConfiguration.GetProviders())
            {
            }
    
            /// <summary>
            /// Gets the configuration providers.
            /// </summary>
            /// <returns>A list of the configuration providers.</returns>
            private static List<IConfigurationProvider> GetProviders()
            {
                // Build a configuration that reads from the appsettings.json files and extract the providers.
                IConfigurationRoot configurationRoot = new ConfigurationBuilder()
                    .AddJsonFile("appsettings.json", true, true)
                    .AddJsonFile($"appsettings.{ApplicationConfiguration.UWPENVIRONMENT}.json", true, true)
                    .Build();
                return configurationRoot.Providers.ToList();
            }
        }
    

    并将其添加到 DI:

        conventions.ForType<ApplicationConfiguration>().Shared().Export(ecb => ecb.AsContractType<IConfiguration>());
    

    您的客户端现在将使用与 .NET Core Web 服务大致相同的方式使用配置文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-19
      • 2021-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多