静态类的访问配置/设置

编程入门 行业动态 更新时间:2024-10-26 10:39:56
本文介绍了静态类的访问配置/设置-ASP Core的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有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.

更多推荐

静态类的访问配置/设置

本文发布于:2023-11-08 11:42:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569273.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:静态

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!