Passing application#39;s connection string down to a Repository Class Library in ASP.NET 5 using the IConfigurationRoot(使用 IConfigurationRoot 将应用程序的连接字符串传递到 ASP.NET 5 中的存储库类库)
问题描述
我有一个 ASP.NET 5 MVC Web 应用程序,在 Startup.cs 中我看到公共属性
I have an ASP.NET 5 MVC Web Application and in Startup.cs I see that the public property
IConfigurationRoot Configuration
被设置为builder.Build();
在整个 MVC Web 应用程序中,我可以轻松完成
Throughout the MVC Web Application I can simply do
Startup.Configuration["Data:DefaultConnection:ConnectionString"]
从 appsettings.json
文件中获取 conn 字符串.
to get the conn string from the appsettings.json
file.
如何使用构造函数注入将 ASP.NET 5 MVC appsettings.json
中指定的连接字符串传递给我的存储库类库?
How can I get the connection string specified in the ASP.NET 5 MVC appsettings.json
passed down to my Repository Class Library using constructor injection?
更新:
这是所有其他存储库继承自的基本存储库(如您所见,我现在在这里有一个硬编码的连接字符串):
UPDATE:
Here is the base repository that all other repositories inherit from (as you can see I have a hardcoded connection string in here for now):
public class BaseRepo
{
public static string ConnectionString = "Server=MYSERVER;Database=MYDATABASE;Trusted_Connection=True;";
public static SqlConnection GetOpenConnection()
{
var cs = ConnectionString;
var connection = new SqlConnection(cs);
connection.Open();
return connection;
}
}
在我的 appsettings.json 文件中的 asp.net 5 Web 应用程序中,我有以下内容,这相当于将连接字符串添加到 .net 4.5 webapp 中的 web.config:
In my asp.net 5 web application in my appsettings.json file I have the following which is equivalent to adding a connection string to a web.config in a .net 4.5 webapp:
"Data": {
"DefaultConnection": {
"ConnectionString": "Server=MYSERVER;Database=MYDATABASE;Trusted_Connection=True;"
}
}
此外,在我的 asp.net 5 Web 应用程序中,我的 Startup.cs 中有以下默认代码,它将站点配置加载到 IConfigurationRoot 类型的公共属性中:
Additionally in my asp.net 5 web application I have the following default code in my Startup.cs which loads the sites configuration into a public property of type IConfigurationRoot:
public IConfigurationRoot Configuration { get; set; }
// Class Constructor
public Startup(IHostingEnvironment env)
{
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
if (env.IsDevelopment())
{
// For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
builder.AddUserSecrets();
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
现在在我的 asp.net Web 应用程序中,如果我想访问任何 appsettings,我可以简单地执行以下操作:Startup.Configuration["Data:DefaultConnection:ConnectionString"]
Now in my asp.net web application if I would like to access any of the appsettings I can simple do the following: Startup.Configuration["Data:DefaultConnection:ConnectionString"]
但不幸的是,我不能从我的类库中做到这一点..
But unfortunately I can't do this from my class library..
如果有人想尝试解决这个问题,请按照以下步骤进行重现:
If someone wants to try and figure this out here are the steps to reproduce:
- 创建一个新的 ASP.NET 5 MVC Web 应用.
- 将另一个类库(包)类型的项目添加到项目中.
- 想办法将 appsettings 从 ASP.NET 5 MVC 应用程序传递到类库
更新后还是看不懂.这是我的代码:
public class BaseRepo
{
private readonly IConfigurationRoot config;
public BaseRepo(IConfigurationRoot config)
{
this.config = config;
}
}
此类声明不起作用,因为 BaseRepo 现在需要构造函数参数.
public class CustomerRepo : BaseRepository, ICustomerRepo
{
public Customer Find(int id)
{
using (var connection = GetOpenConnection())
{
...
}
}
}
推荐答案
在你的 Startup.cs 文件中添加以下方法
on your Startup.cs file add the following method
public void ConfigureServices(IServiceCollection services) {
services.AddSingleton(_ => Configuration);
}
然后像这样更新你的 BaseRepo 类
then update your BaseRepo class like this
public class BaseRepo {
private readonly IConfiguration config;
public BaseRepo(IConfiguration config) {
this.config = config;
}
public SqlConnection GetOpenConnection() {
string cs = config["Data:DefaultConnection:ConnectionString"];
SqlConnection connection = new SqlConnection(cs);
connection.Open();
return connection;
}
}
这篇关于使用 IConfigurationRoot 将应用程序的连接字符串传递到 ASP.NET 5 中的存储库类库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 IConfigurationRoot 将应用程序的连接字符串传递到 ASP.NET 5 中的存储库类库
基础教程推荐
- MS Visual Studio .NET 的替代品 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01