WCF傳輸大資料的設定

來源:互聯網
上載者:User

在從用戶端向WCF服務端傳送較大資料(>65535B)的時候,發現程式直接從Reference的BeginInvoke跳到EndInvoke,沒有進入服務端的Service實際邏輯中,懷疑是由於資料過大超出限定導致的。

報錯資訊:遠程伺服器返回了意外響應: (400) Bad Request。

問題是我實際發送的資料是剛剛從WCF服務端接收過來的,一來一去,資料量差別並不大。

然後發現,在用戶端和服務端實際使用的是不同的配置,對於用戶端,在添加ServiceReference時自動產生的ServiceReferences.ClientConfig檔案中system.serviceModel節下有這樣的設定:

<bindings>    <basicHttpBinding>        <binding name="BasicHttpBinding_WcfService" maxBufferSize="2147483647"            maxReceivedMessageSize="2147483647">            <security mode="None" />        </binding>    </basicHttpBinding></bindings>

然後在Client節裡應用Binding Configuration:

<client>            <endpoint address="http://localhost:22000/Service/WcfService.svc"                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_WcfService"                contract="WcfServiceReference.WcfService" name="BasicHttpBinding_WcfService" />

</client>

在Binding裡指定了最大緩衝位元組數和最大接受位元組數,相當於2G的大小!除非傳一整套連續劇,一般是夠用了。

而在服務端,Web.config檔案裡,Bindings節是空的,而Service也沒有指定bindingConfiguration屬性,那麼它們採用的就是預設的65535的大小。

問題找到,解決就比較容易了:

在Bindings節添加新的Binding設定,指定最大接受資料:

<bindings>    <basicHttpBinding>        <binding name="LargeDataTransferServicesBinding" maxReceivedMessageSize="2147483647"  messageEncoding="Text" transferMode="Streamed" sendTimeout="00:10:00" />    </basicHttpBinding></bindings>

之後給相應的Service指定bindingConfiguration屬性:

<service behaviorConfiguration="Server.Service.WcfServiceBehavior"  name="Server.Service.WcfService">  <endpoint address="" binding="basicHttpBinding" bindingConfiguration="LargeDataTransferServicesBinding" contract="Server.Service.WcfService" />  <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /></service>

這樣就可以從用戶端發送足夠大的資料了。

P.S.:

.net預設只能傳4M的檔案,所以儘管設定了Wcf兩端的配置,還是超不出.net的限定,所以如果要傳輸大檔案,還需要在System.Web節下加上

    <httpRuntimemaxRequestLength="102400" />

這裡的單位是KB,這樣就可以傳100M的檔案了。當然,這麼大的檔案,最好還是分段傳輸比較好。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.