Visual Studio 在线构建 - web.config 连接字符串 configSource

Visual Studio Online Build - web.config connection strings configSource

我的项目 Web.config 使用以下结构在单独的文件中定义了连接字符串:

<connectionStrings configSource="ConnectionStrings.config">
</connectionStrings>

这在协作项目或部署项目时非常方便。但是,我无法让 VSO Build 正常工作,因为它向我显示以下错误:

C:\Program Files (x86)\MSBuild.0\bin\Microsoft.Common.CurrentVersion.targets (4105, 5) Could not copy the file "C:\a\s\MyProject\ConnectionStrings.config" because it was not found.

连接字符串必须已经在web.config中定义,并且连接字符串名称必须与您在Azure中设置的连接字符串名称相同。然后 Azure 可以更新连接字符串。参考Azure的描述:

Connection strings work in a similar fashion, with a small additional requirement. Remember from earlier that there is a connection string called “example-config_db” that has been associated with the website. If the website’s web.config file references the same connection string in the configuration section, then Windows Azure Web Sites will automatically update the connection string at runtime using the value shown in the portal.

However, if Windows Azure Web Sites cannot find a connection string with a matching name from the web.config, then the connection string entered in the portal will only be available as an environment variable (as shown earlier).

Remember though that for Windows Azure Web Sites to override a connection string and materialize it in the .NET Framework’s connection string configuration collection, the connection string must already be defined in the web.config. For this example website, the web.config has been updated as shown below:

有关详细信息,请参阅此 link:https://azure.microsoft.com/en-us/blog/windows-azure-web-sites-how-application-strings-and-connection-strings-work/

我知道您已经将您的问题标记为已回答,但我认为我应该 post 我的解决方案在这里供遇到相同问题的任何人使用。 我使用如下虚拟连接字符串值在其他环境中为 connectionStrings 部分创建了一个转换:

  <connectionStrings xdt:Transform="Replace">
     <add name="Your_ConnectionString_Name" connectionString="dummy_value" 
               providerName="System.Data.SqlClient" />
  </connectionStrings>

以便我可以将 configSource 属性保留在我的本地计算机中,并且当我部署到其他环境时,我有一些供 Azure 替换的虚拟连接字符串。