我正在使用ASP.NET Core MVC应用程序,我的连接字符串存在问题.
我在生产服务器上ASPNETCORE_ENVIRONMENT设置了一个变量Production,我的生产服务器是运行IIS的Windows Server 2012R2.我还在生产服务器上安装了DotNetCore.1.0.4_1.1.1-WindowsHosting.exe.
在开发过程中,我UserSecrets用来保存我的连接字符串.这工作正常.
对于生产,我希望我的生产服务器上的环境变量中的连接字符串,这是我遇到问题的地方.我怀疑它可能是我构建环境变量的方式.
当我尝试在生产中访问数据库时,我得到一个错误,表明它无法解析连接字符串.
An exception occurred in the database while iterating the results of a query.
System.ArgumentException: Keyword not supported: '"server'.
at System.Data.Common.DbConnectionOptions.ParseInternal(Dictionary`2
parsetable, String connectionString, Boolean buildChain, Dictionary`2 synonyms)
at System.Data.Common.DbConnectionOptions..ctor(String connectionString, Dictionary`2 synonyms)
at System.Data.SqlClient.SqlConnectionString..ctor(String connectionString)
Run Code Online (Sandbox Code Playgroud)
如果我将连接字符串放入appSettings.json,生产服务器就可以正常工作.
所以,这是我的appSettings.json文件示例,显示了在生产中工作的连接字符串;
{
"ConnectionStrings": {
"TestDb": "Server=TestServer;Database=TestDb;Persist Security Info=True;User ID=TestUser;Password=testpassword;MultipleActiveResultSets=true"
},
...
...
...
}
}
Run Code Online (Sandbox Code Playgroud)
如果我将此appSettings.json文件部署到生产中,它可以正常工作.
在我的ASP.Net Core应用程序中,在Startup.cs文件中,我有以下内容:
public Startup(IHostingEnvironment env)
{
var …Run Code Online (Sandbox Code Playgroud) app.json我的里面有一个文件Expo文件。在此文件中,我有两个 API 密钥(下面标记为 API_KEY),我想通过环境变量隐藏它们。
如何使用环境变量而不是对 API 密钥进行硬编码?
应用程序.json
{
"expo": {
"name": "Closeout",
"slug": "Closeout",
"version": "1.0.0",
"orientation": "portrait",
"privacy": "hidden",
"notification": {
"icon": "./assets/images/notification-icon.png",
"color": "#000000",
"iosDisplayInForeground": true
},
"updates": {
"fallbackToCacheTimeout": 0
},
"assetBundlePatterns": ["**/*"],
"ios": {
"icon": "./assets/images/icon.png",
"buildNumber": "2",
"config": {
"googleMapsApiKey": "API_KEY"
}
},
"android": {
"adaptiveIcon": {
"foregroundImage": "./assets/images/icon.png",
"backgroundColor": "#000"
},
"versionCode": 5,
"useNextNotificationsApi": true,
"config": {
"googleMaps": {
"apiKey": "API_KEY"
}
},
"googleServicesFile": "./google-services.json"
}
}
}
Run Code Online (Sandbox Code Playgroud)