【问题标题】:Difference between my local and production web.config我的本地和生产 web.config 之间的区别
【发布时间】:2014-11-06 12:39:18
【问题描述】:

我收到此错误消息

应用程序中的连接字符串“MyConnection” 配置文件不包含所需的 providerName 属性。”

我读过这个问题The connection string 'MyConnection' in the application's configuration file does not contain the required providerName attribute." 关于同样的问题。

在我的情况下,providerName="System.Data.SqlClient" 在我的本地 web.config 文件中是正确的,但是当我在生产服务器上发布时消失(内置发布)。我的本地服务器上没有问题。

编辑

这是我的发布配置文件

<?xml version="1.0" encoding="utf-8"?>

<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <!--
    In the example below, the "SetAttributes" transform will change the value of 
    "connectionString" to use "ReleaseSQLServer" only when the "Match" locator 
    finds an attribute "name" that has a value of "MyDB".

    <connectionStrings>
      <add name="MyDB" 
        connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" 
        xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
    </connectionStrings>
  -->
  <system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />
    <!--
      In the example below, the "Replace" transform will replace the entire 
      <customErrors> section of your web.config file.
      Note that because there is only one customErrors section under the 
      <system.web> node, there is no need to use the "xdt:Locator" attribute.

      <customErrors defaultRedirect="GenericError.htm"
        mode="RemoteOnly" xdt:Transform="Replace">
        <error statusCode="500" redirect="InternalError.htm"/>
      </customErrors>
    -->
  </system.web>
</configuration>

【问题讨论】:

  • 最有可能是由 web.config 转换引起的 msdn.microsoft.com/en-us/library/dd465326(v=vs.110).aspx 检查 web.release.config 中的内容
  • 发布时,会应用任何配置转换。检查您的解决方案中是否有类似 Web.Config.Production 的内容
  • 连接字符串被发布过程重写。您是否在“发布”对话框的“设置”部分设置了连接字符串?
  • 通常您不会用开发机器上的构建或发布过程发布的文件覆盖生产服务器上的web.config 文件。您只需根据该服务器上的设置对其进行编辑。在这种情况下,请手动编辑文件以包含您的 providerName
  • @AlexBarac :看来您需要从 VisualStudio 和 msdeploy 了解很多关于“发布”的知识。 Visual Studio 允许您根据在 vs 中选择的解决方案配置对 web.config 应用转换,从而无需对部署到服务器的文件进行任何手动编辑。

标签: c# asp.net .net


【解决方案1】:

将此用于您的发布配置文件:

<connectionStrings>
    <add name="MyDB" 
      connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" 
      xdt:Transform="SetAttributes" xdt:Locator="Match(name)" providerName="System.Data.SqlClient" />
  </connectionStrings>

您本地的 ConnectionString 正在通过发布配置文件进行更改。因此,如果您在本地配置中声明 providerName,那么如果您发布发布文件,那将毫无用处。您还需要在发布配置文件中声明 providerName。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-27
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-20
    • 1970-01-01
    • 2012-12-17
    相关资源
    最近更新 更多