【问题标题】:Can't retrieve AppSettings in asp.net mvc 6 beta 8无法在 asp.net mvc 6 beta 8 中检索 AppSettings
【发布时间】:2015-10-27 14:18:19
【问题描述】:

我正在尝试创建一个新的 ASP.Net MV5 项目(目前为 beta8),用于学习目的。

我对获取应用设置的简单案例感到困惑。

我在 appsettings.json 文件中添加了一些配置:

{
  "Data": {
    "DefaultConnection": {
      "ConnectionString": "Server=(localdb)\\mssqllocaldb;Database=aspnet5-someproject-e84e86e2-0fec-4132-9a91-2f6c4b4c61a3;Trusted_Connection=True;MultipleActiveResultSets=true"
    }
  },
  "AppSettings": {
    "CloudStorageContainerReference": "someproject",
    "StorageConnectionString": "UseDevelopmentStorage=true"
  }
}

我还创建了一个强类型类:

public class AppSettings
{
    public string CloudStorageContainerReference { get; set; }
    public string StorageConnectionString { get; set; }
}

并更新了我的启动文件:

   public void ConfigureServices(IServiceCollection services)
    {
        // Add Entity Framework 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();

        // Add settings
        services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));

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

        // Register application services.
        services.AddTransient<IEmailSender, AuthMessageSender>();
        services.AddTransient<ISmsSender, AuthMessageSender>();
    }

此时,如果我破解代码,我可以看到Configuration.GetSection("AppSettings").Value 为空。

此外,我想在我的控制器中注入这些设置:

public class TransfertController : Controller
{
    private IOptions<AppSettings> AppSettings;

    public TransfertController(IOptions<AppSettings> appSettings)
    {
        if (AppSettings == null) throw new ArgumentNullException(nameof(appSettings));

        AppSettings = appSettings;
    }

}

但它会抛出一个NullReferenceException,因为我的appSettings 参数为空。

缺少什么?

[编辑]:作为旁注,这一行:

Configuration["AppSettings:CloudStorageContainerReference"]

实际上返回正确的单个值。

【问题讨论】:

    标签: asp.net dependency-injection asp.net-core-mvc


    【解决方案1】:

    我会说这只是您原始代码中的一个错字:

    public TransfertController(IOptions<AppSettings> appSettings)
    {
        if (AppSettings == null) throw new ArgumentNullException(nameof(settings));
    
        AppSettings = appSettings;
    }
    

    在您分配之前,您正在检查您正在分配的同一属性AppSettings。您希望守卫检查方法输入参数,而不是 if (appSettings == null) throw new ... 中的属性(注意小写 appSettings

    【讨论】:

      【解决方案2】:

      不知道为什么,但我通过将我的班级成员“AppSettings”重命名为“Settings”并解开 IOptions 解决了这个问题:

      public class TransfertController : Controller
      {
          private readonly AppSettings Settings;
      
          public TransfertController(IOptions<AppSettings> settings)
          {
              if (settings == null) throw new ArgumentNullException(nameof(settings));
      
              Settings = settings.Value;
          }
      }
      

      现在正在工作...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-29
        • 2016-01-24
        • 2016-12-27
        • 2016-01-14
        • 2015-09-22
        • 1970-01-01
        相关资源
        最近更新 更多