2014-09-04 137 views
0

我寫了一個WCF服務應用程序。 當項目生成時,Visual Studio創建3個Web配置文件。錯誤的連接字符串返回

我已經完成了項目,至今我一直在使用我的GetOpenConnection()函數中的硬編碼連接字符串,所以我現在想要將連接字符串移動到web.config文件中。

以下調用返回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> 

回答

0

如果你使用轉換文件,你需要添加轉換屬性,並指定你想要做什麼,插入,刪除,替換...

我用來把本地連接放在master web.config然後通過替換定義的連接字符串的屬性,在Release配置中對其進行轉換。

檢查這篇文章:Web.config Transformation Syntax for Web Project Deployment Using Visual Studio

如果你想用你的方式,只需添加xdt:Transform="Insert"<add>節點。

您可以使用此Web測試器測試您的轉換:Web.config Transformation Tester