【问题标题】:Two connection strings for local and hosting server本地和托管服务器的两个连接字符串
【发布时间】:2019-07-17 23:16:45
【问题描述】:

我想问有没有办法告诉 ASP.NET Core 2 选择不同的连接字符串。

每次我将我的网站发布到托管服务器时,不断更改appsettings.json 文件中的连接字符串非常烦人..

我正在使用此代码来获取连接字符串。

 services.AddDbContext<AppIdentityDbContext>(options => 
     options.UseSqlServer(Configuration["Data:WebDataBase:ConnectionString"]));

也许有一种简单的方法,但我正在考虑在我的 Startup.cs 中使用 if 语句:

if (local) {
    services.AddDbContext<AppIdentityDbContext>(options => 
        options.UseSqlServer(Configuration["Data:WebDataBase1:ConnectionString"]));
}
else {
    services.AddDbContext<AppIdentityDbContext>(options => 
        options.UseSqlServer(Configuration["Data:WebDataBase2:ConnectionString"]));
}

但是,无论服务器是我的本地计算机还是实时托管服务器,我如何设置这个 local 变量?

"Data": {
  "WebDataBase1": {
    "ConnectionString": "Data Source=DatasoruceName;Initial Catalog=DBname;Trusted_Connection=True;Integrated Security=True;"
  },

  "WebDataBase2": {
    "ConnectionString": "Data Source=DatasoruceName;Initial Catalog=DatabaseName;Trusted_Connection=True;Integrated Security=True;"
  }
}

【问题讨论】:

  • 您似乎正在尝试分离环境。这是可用的方法之一:docs.microsoft.com/en-us/aspnet/core/fundamentals/…
  • 另外,如果你想保护你的连接字符串,也可以看看这里:docs.microsoft.com/en-us/aspnet/core/security/…
  • 完全是 Baranyi,我在想也许我应该在我的 ConfigureServices 中使用 IHostingEnvironment env 并检查 env.IsDevelopment() 与否......但我不确定这会起作用还是有更好的
  • 它不起作用... ConfigureServices 方法必须是无参数的,或者只接受一个 IServiceCollection 类型的参数。

标签: c# .net asp.net-core asp.net-core-2.0 asp.net-core-configuration


【解决方案1】:

不应在代码中指定特定于环境的配置。 ASP.NET Core 有一种机制,允许您根据运行的环境更换配置。

在开发应用程序时,您通常在Development 环境中运行。当您为生产部署应用程序时,默认使用Production 环境。但是,如果您有其他环境,您也完全可以为这些环境编造新名称并为它们进行特定配置。这一切都在Environments chapter of the documentation中进行了解释

环境允许您创建多个appsettings.json 文件。 ASP.NET Core 默认带有两个文件:appsettings.jsonappsettings.Development.json

前者应该包含环境非特定配置;适用于所有环境的东西。后一个文件包含特定于开发的配置,例如调整日志记录级别,以便您在开发期间获得更多信息。您还可以使用这些文件在appsettings.json 中指定默认连接字符串,并在appsettings.Development.json 中覆盖该默认连接字符串以进行开发。

此外,配置文件遵循一个非常简单的模式:appsettings.&lt;Environment&gt;.json。因此,如果您在Production 环境中运行,则会加载一个名为appsettings.Production.json 的文件(如果存在)。此机制允许您以不同方式配置所有环境,而无需在代码中进行检测。

另外,在开发过程中还有用户机密的概念。这些用于特定于开发的配置,这些配置仅适用于您自己,但不适用于您团队的其他成员。这基本上是每台机器的配置,允许您覆盖appsettings.jsonappsettings.Development.json。这在user secrets chapter 中有描述。

最好的做法是完全避免在配置文件中使用连接字符串,因为您希望避免碰巧有权访问您的配置文件(例如通过您的源代码)的人知道您的数据库的密码。在这种情况下,您可以使用其他机制,例如进程本地的environment variables


在您的情况下,您只是使用集成安全性,因此依赖当前用户的凭据,这不是什么大问题。您唯一泄漏的是数据库名称。 – 因此,您绝对可以从将连接字符串放入 appsettings 文件开始。

例如,这就是您在 Startup 的 ConfigureServices 中配置数据库上下文的方式:

services.AddDbContext<AppIdentityDbContext>(options => 
    options.UseSqlServer(Configuration.GetConnectionString("WebDataBase"));

您的appsettings.Development.json 如下所示:

{
  "ConnectionStrings": {
    "WebDataBase": "Data Source=DatasourceName;Initial Catalog=DBname;Trusted_Connection=True;Integrated Security=True;"
  }
}

你的appsettings.Production.json 看起来像这样:

{
  "ConnectionStrings": {
    "WebDataBase": "Data Source=DatasourceName;Initial Catalog=DatabaseName;Trusted_Connection=True;Integrated Security=True;"
  }
}

【讨论】:

  • 感谢Poke的帮助。我只是跟着 Pro ASP.Net Core2 书籍连接字符串示例。你说的对。我会改变它..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-20
  • 1970-01-01
相关资源
最近更新 更多