【发布时间】:2022-01-27 16:51:21
【问题描述】:
如果有人能指出我正确的方向,我将不胜感激。很抱歉,这篇文章很长,所以我很抱歉。
在我的旧 ASP.NET webforms 应用程序中,我有以下项目结构: 业务层 数据访问层 实用程序层 网页界面
WebUI 将具有以下项目依赖项 业务层 实用工具层
BusinessLayer 将具有以下项目依赖项 数据访问层 实用工具层
WebUI 会有一个包含数据库设置等的 web.config 文件。
现在在我的新 Blazor WASM 应用程序中,我有以下项目结构: API层 业务层 数据访问层 实用程序层 服务器.UI Wasm.UI
APILayer 将具有以下项目依赖项 BusinessLayer 并直接调用方法
Wasm.UI 将具有以下项目依赖项 UtilitiesLayer 并会调用 APILayer 来获取数据
Server.UI 将具有以下项目依赖项 UtilitiesLayer 并会调用 APILayer 来获取数据
BusinessLayer 将具有以下项目依赖项 数据访问层 实用工具层
现在我的问题是我应该将包含数据库设置的 appsettings.json 放在哪里。目前我在 UtilitiesLayer 中有一个名为 WebsiteSettings 的类,例如
public static string ThisDatabaseConnectionString { get; set; }
并在 MainLayout.razor 中设置我第一次运行应用程序时的设置
@inject IConfiguration configuration
UtilitiesLayer.WebsiteSettings.ThisDatabaseConnectionString = configuration.GetSection("ConnectionStrings").GetSection("ThisDatabaseConnectionString").Value;
"ConnectionStrings": {
"ThisDatabaseConnectionString": "Server=127.0.0.1;Port=3306;Database=thisDB;Uid=root;Pwd=password;"
},
如果我将项目依赖项直接放在 BusinessLayer 上,这一切正常,但因为我通过 APILayer 中的 HttpClient 调用 BusinessLayer,如下所示:
sUrl = "https://localhost:7263/DBAPIs/GetAllUser";
using (var http = new HttpClient())
{
oDic = await http.GetFromJsonAsync<List<Users>>(sUrl);
}
在业务层中
DataTable dt = MySQLHelper.ExecuteDataTable(UtilitiesLayer.WebsiteSettings.ThisDatabaseConnectionString, sqlCommand);
UtilitiesLayer.WebsiteSettings.ThisDatabaseConnectionString 始终为空。
那么我在哪里可以放置我可以在所有项目中始终访问 UtilitiesLayer.WebsiteSettings.ThisDatabaseConnectionString 的设置文件?
【问题讨论】:
标签: blazor