简体   繁体   English

访问静态类上的配置/设置 - Asp Core

[英]Access Configuration/Settings on static class - Asp Core

I have 3 solutions. 我有3个解决方案。 Project.Web, Project.Core (Business), and Project.Layer(Models). Project.Web,Project.Core(Business)和Project.Layer(Models)。

In Project.Core, I have a static file that I can call like this Business.GetAllData(); 在Project.Core中,我有一个静态文件,我可以像这样调用Business.GetAllData(); from Project.Web.Controller. 来自Project.Web.Controller。

This calls DAL/EF files and gets data( BusinessDal.GetData() ). 这会调用DAL / EF文件并获取数据( BusinessDal.GetData() )。

        using (DBContext db = new DBContext())
        {
            return db.GetAllData().ToPOCO();
        }

On my configuration/DbContext.cs, I have this: 在我的配置/ DbContext.cs上,我有这个:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    #if DEBUG
        optionsBuilder.UseSqlServer(@"connstring");
    #else
        optionsBuilder.UseSqlServer(@"connstring");
    #endif
    // How do I access configuration here? Configuration["ConnectionString"]
}

What I'm trying to do is read settings from my appsettings.json. 我想要做的是从我的appsettings.json读取设置。 I made sure settings are loaded properly on startup.cs 我确保在startup.cs上正确加载了设置

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
        .AddEnvironmentVariables();
    Configuration = builder.Build();
}

But now what?... MS Document shows how to read it from controllers. 但现在是什么?... MS Document显示了如何从控制器读取它。 And that part works fine, I can read my settings on Controllers. 这部分工作正常,我可以在控制器上读取我的设置。 However, I am not sure how to pass it to another project and still be able to call it from a static class. 但是,我不知道如何将它传递给另一个项目,仍然可以从静态类中调用它。

I feel like this may be more work than necessary, but I'm in a rush so this is what I'm going with so far. 我觉得这可能比必要的工作更多,但我很匆忙所以这就是我到目前为止所做的事情。 Feel free to post other solutions as they become available. 随时可以发布其他解决方案。

I create another static class AppSettingsProvider.cs 我创建了另一个静态类AppSettingsProvider.cs

public static class AppSettingsProvider
{
    public static string DbConnectionString { get; set; }
    public static bool IsDevelopment { get; set; }
}

Then I set them on my Startup.cs 然后我将它们设置在我的Startup.cs上

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
        .AddEnvironmentVariables();
    Configuration = builder.Build();

    BuildAppSettingsProvider();
}
private void BuildAppSettingsProvider()
{
    AppSettingsProvider.ConnectionString = Configuration.GetConnectionString("DBContext");
    AppSettingsProvider.IsDevelopment = Configuration["IsDev"];
}

Then I can call it from my DbContext.cs 然后我可以从我的DbContext.cs中调用它

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    string connString = AppSettingsProvider.ConnectionString;
}

PS I tried the dependency injection method into DbContext (by having contructors). PS我尝试将依赖注入方法引入DbContext(通过使用构造函数)。 However, that did not work for me because I was calling DbContext from a static file, so the DbContextOptions was getting lost. 但是,这对我不起作用,因为我从静态文件调用DbContext,所以DbContextOptions丢失了。

A slightly shorter version based on the same principle as above: 基于与上述相同原理的略短版本:

public Startup(IConfiguration configuration)
{
    Configuration = configuration;
    StaticConfig = configuration;
}
public static IConfiguration StaticConfig { get; private set; }

To use in another static class: 要在另一个static类中使用:

string connString = Startup.StaticConfig.GetConnectionString("DefaultConnection");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 static class 中的 Asp.Net Core 配置 - Asp.Net Core configuration in static class ASP.NET Core中的抽象设置配置 - Abstract Settings Configuration in ASP.NET Core Asp.Net Core - 访问 IStringLocalizer 实例形式 static class - Asp.Net Core - Access IStringLocalizer instance form static class 在使用最小 api 时在服务配置期间访问 ASP.NET 核心设置 - Access ASP.NET Core settings during service configuration when using minimal api 无法使用 Asp.Net Core 3 中的命名选项读取/绑定具有强类型 class 的配置设置 - Unable to read/bind configuration settings with strongly-typed class using Named Options in Asp.Net Core 3 将ASP.NET Core配置设置用于RavenDB连接 - Using ASP.NET Core configuration settings for RavenDB connections 如何在测试类中注入配置设置(ASP.NET Core)? - How to inject configuration settings in test classes (ASP.NET Core)? 如何在 ASP .NET Core 中初始化主机之前读取配置设置? - How to read configuration settings before initializing a Host in ASP .NET Core? 在.net核心类库中创建静态设置类 - Creating a static settings class in .net core class library 如何访问 ASP.NET Core 中任何类中的配置? - How do I access Configuration in any class in ASP.NET Core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM