我有3个解决方案。 Project.Web,Project.Core(业务)和Project.Layer(模型)。
I have 3 solutions. Project.Web, Project.Core (Business), and Project.Layer(Models).
在Project.Core中,我有一个可以这样调用的静态文件 Business.GetAllData(); 从Project.Web.Controller。
In Project.Core, I have a static file that I can call like this Business.GetAllData(); from Project.Web.Controller.
这将调用DAL / EF文件并获取数据( BusinessDal.GetData() )。
This calls DAL/EF files and gets data(BusinessDal.GetData()).
using (DBContext db = new DBContext()) { return db.GetAllData().ToPOCO(); }在我的配置/DbContext.cs中,我有以下内容:
On my configuration/DbContext.cs, I have this:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { #if DEBUG optionsBuilder.UseSqlServer(@"connstring"); #else optionsBuilder.UseSqlServer(@"connstring"); #endif // How do I access configuration here? Configuration["ConnectionString"] }我要做的是读取设置从我的appsettings.json中。我确保在启动时正确加载了设置。cs
What I'm trying to do is read settings from my appsettings.json. I made sure settings are loaded properly on 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(); }但是现在呢?... MS文档展示了如何从控制器读取它。那部分工作正常,我可以在Controller上读取我的设置。但是,我不确定如何将其传递给另一个项目,并且仍然能够从静态类中调用它。
But now what?... MS Document shows how to read it from controllers. 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.
我创建另一个静态类 AppSettingsProvider.cs
public static class AppSettingsProvider { public static string DbConnectionString { get; set; } public static bool IsDevelopment { get; set; } }然后我将它们设置在Startup.cs中。
Then I set them on my 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"]; }然后我可以从DbContext.cs中调用它
Then I can call it from my DbContext.cs
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { string connString = AppSettingsProvider.ConnectionString; }我尝试将依赖项注入方法插入DbContext(通过构造函数)。但是,这对我不起作用,因为我是从静态文件中调用DbContext的,所以DbContextOptions丢失了。
P.S. I tried the dependency injection method into DbContext (by having contructors). However, that did not work for me because I was calling DbContext from a static file, so the DbContextOptions was getting lost.
更多推荐
静态类的访问配置/设置
发布评论