返回错误的连接字符串

时间:2014-09-04 10:10:08

标签: asp.net wcf web-config

我已经编写了一个WCF服务应用程序。 生成项目时,visual studio会创建3个Web配置文件。

我已经完成了项目,到目前为止我一直在GetOpenConnection()函数中使用硬编码连接字符串,所以我现在想将连接字符串移动到web.config文件。 / p>

以下调用返回null。     ConnectionStringSettings csSettings = ConfigurationManager.ConnectionStrings [" PulseWcfConnectionString"];

当我运行以下代码时,它不会返回我的web.debug.config文件中设置的字符串。

for(int idx = 0; idx < ConfigurationManager.ConnectionStrings.Count; idx++)
  Debug.WriteLine(ConfigurationManager.ConnectionStrings[idx].ConnectionString);

它返回以下2项,第二项是空字符串。我不认识第一行,也许它是默认的一行?

data source=.\SQLEXPRESS;Integrated Security=SSPI; AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true
""

我错过了什么?

我的web.debug.config包含以下应该用于本地sql server实例

<?xml version="1.0"?>

<!-- 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">

  <connectionStrings>
    <add
      name="PulseWcfConnectionString"
      connectionString="Data Source=WIN8-CLAIRE\SQLSRVDEV2008;Initial 
    Catalog=gcll;Persist Security Info=True;Integrated Security=True"
      providerName="System.Data.SqlClient"
  />
  </connectionStrings>

</configuration>

现在我的web.release.config包含相同的内容(它明天将发布到它的目的地,所以我将更改它的详细信息)

<?xml version="1.0"?>

<!-- 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">

  <connectionStrings>
    <add
      name="PulseWcfConnectionString"
      connectionString="Data Source=WIN8-CLAIRE\SQLSRVDEV2008;Initial 
    Catalog=gcll;Persist Security Info=True;Integrated Security=True"
      providerName="System.Data.SqlClient"
  />
  </connectionStrings>

  <system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />
  </system.web>

</configuration>

的web.config

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <authentication mode="Windows" />
  </system.web>

  <system.serviceModel>
    <services>
      <service name  ="pulse.smartcentre.wcf.service.app.PulseWebService"
               behaviorConfiguration="ServiceBehavior">

        <host>
          <baseAddresses>
            <add baseAddress = "http://localhost:52478/Design_Time_Addresses/pulse.smartcentre.wcf.service.app/PulseWebService/" />
          </baseAddresses>
        </host>

        <endpoint address="" binding="wsHttpBinding"
                  bindingConfiguration="wsHttpBinding"
                  contract="pulse.smartcentre.wcf.service.app.IPulseWebService">

          <!-- 
              Upon deployment, the following identity element should be removed or replaced to reflect the 
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
              automatically.
          -->
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>

        <!-- Metadata Endpoints -->
        <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
        <!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>

      </service>
    </services>

    <!-- CNH -->
    <bindings>

      <!-- Secure binding (to use) -->
      <wsHttpBinding>
        <binding name="wsHttpBinding" closeTimeout="00:01:00"
            openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
            allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
            maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
            messageEncoding="Text" textEncoding="utf-8"
            useDefaultWebProxy="true" transactionFlow="true">
          <readerQuotas
            maxArrayLength="2147483647"
            maxBytesPerRead="2147483647"
            maxDepth="2147483647"
            maxNameTableCharCount="2147483647"
            maxStringContentLength="2147483647" />
        </binding>
      </wsHttpBinding>


    </bindings>

    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>

          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>

      <endpointBehaviors>
        <behavior name="Behaviors.EndpointBehavior">
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

  </system.serviceModel>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>


</configuration>

1 个答案:

答案 0 :(得分:1)

如果您使用转换文件,则需要添加转换属性,并指定要执行的操作,插入,删除,替换...

我用来将本地连接放在master web.config中,然后通过替换定义的连接字符串的属性,在Release配置中对其进行转换。

查看此文章:Web.config Transformation Syntax for Web Project Deployment Using Visual Studio

如果您想使用自己的方式,只需在xdt:Transform="Insert"节点中添加<add>

您可以使用此网络测试工具测试您的转化:Web.config Transformation Tester