[英]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.