1. 程式人生 > >VS2010 WCF 遠端伺服器返回了意外響應: (400) Bad Request等問題解決方案

VS2010 WCF 遠端伺服器返回了意外響應: (400) Bad Request等問題解決方案

1.wcf相關專案名稱Service1.svcIDataChangeTest.csDataChangeTest.cs(下面的專業名稱可能不太準確)。

Service1.svc宿主,裡面只關聯介面的空間

IdataChangeTest.cs裡邊是介面,如下:

……

      /// <summary>
      /// 通過特定表的特定欄位取得Datatable
      /// </summary>
      /// <param name="tableName">表名</param>
      /// <param name="fieldname">欄位名</param>
      /// <param name="fieldval">對應的值</param>
      /// <param name="accWord">驗證編碼</param>
      /// <returns></returns>
    [OperationContract]
    DataTable GetTableDataByField(string tableName, string fieldname, string fieldval, string accWord);

……

DataChangeTest.cs為實際的方法,如下:

……

2.下面是wcfconfig設定:

<configuration>
       <system.serviceModel>
               <bindings>
                       <wsHttpBinding>
<!-- 2147483647最大位元組數,這個是int32的最大位元組數,大小是2047M,肯定能適合正常的資料傳輸。估計不用全部設定成這麼大都夠用。-->
                               <binding name="NewBinding0" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text">
                                       <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
                               </binding>
                       </wsHttpBinding>
               </bindings>
               <behaviors>
                       <serviceBehaviors>
                               <behavior name="metadataBehavior">
                                       <serviceMetadata httpGetEnabled="true"/>
         <serviceDebug includeExceptionDetailInFaults="false"/>
                               </behavior>
                       </serviceBehaviors>
               </behaviors>
<!—metadataBehavior對應的是“<behavior name="metadataBehavior">”的name,名稱無所謂,只要對應就好。Services.DataChangeTest是呼叫方法DataChangeTest.cs窗體對應的空間名稱和窗體類名稱;Contracts.IdataChangeTest是呼叫介面IdataChangeTest.cs窗體對應的空間名稱和窗體類名稱;NewBinding0對應上面“<binding name="”的name-->
               <services>
                       <service behaviorConfiguration="metadataBehavior" name="Services.DataChangeTest">
                               <endpoint binding="wsHttpBinding" bindingConfiguration="NewBinding0" contract="Contracts.IDataChangeTest"/>
                       </service>
<!—下面兩處紅色的部分會影響釋出之後scf的呼叫,如果沒有,釋出之後無法引用。-->
               </services>
   <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
       </system.serviceModel>
 <system.webServer>
   <modules runAllManagedModulesForAllRequests="true"/>
 </system.webServer>
 <system.web>
               <compilation debug="true"/>
 </system.web>
</configuration>

3.下面是app.config裡面的設定,這個基本上是引用Service Reference時自動產生的,我只是把對應的size改的大了些。

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

<configuration>

<system.serviceModel>

<bindings>

<wsHttpBinding>

<binding name="WSHttpBinding_IDataChangeTest" closeTimeout="00:01:00"

openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"

bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"

maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"

messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"

allowCookies="false">

<readerQuotas maxDepth="32" maxStringContentLength="2147483647"

maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />

<reliableSession ordered="true" inactivityTimeout="00:10:00"

enabled="false" />

<security mode="Message">

<transport clientCredentialType="Windows" proxyCredentialType="None"

realm="" />

<message clientCredentialType="Windows" negotiateServiceCredential="true" />

</security>

</binding>

</wsHttpBinding>

</bindings>

WSHttpBinding_IDataChangeTest這個是上面bingname,這個可能是由ServiceReference1.IdataChangeTest的來的,估計可以隨便寫,個人猜測

ServiceReference1.IdataChangeTest這個是對應引用的ServiceReference1,雙擊他,左面會顯示出來,如下面的圖片-->

<client>

<endpoint address="http://192.168.254.65:82/Service1.svc" binding="wsHttpBinding"

bindingConfiguration="WSHttpBinding_IDataChangeTest" contract="ServiceReference1.IDataChangeTest"

name="WSHttpBinding_IDataChangeTest">

<identity>

<servicePrincipalName value="host/Hqyr-PC" />

</identity>

</endpoint>

</client>

</system.serviceModel>

</configuration>


 
  上面只是解決的具體方法,相信我說的已經很詳細了。之前查過很長時間,一直未找到解決的方案。現在解決,我在想不是之前查到的那些文章沒有說明白,應該是自己沒有靜下心來好好的看吧。很多時候遇到問題,我們總是在想:找到個一下子能解決的方法,卻忽略了很多細節。也許我更關注的是結果,而不是過程。雖然受時間的壓迫這個因素會讓我們傾向於這樣想!我想細節能影響一件事情的成敗,更決定了一個人的層次。
  在努力一點,細心一點,你會更成功!跟自己說,跟像我一樣急功近利的人說。