今天在項目中發送資料包到伺服器端,WCF傳回錯誤資訊:The remote server returned an error: NotFound. 經過排查,通過以下步驟進行了解決。
Silverlight企業級項目中,經常要發送大量資料包到伺服器端,而WCF服務本身對資料包進行了限制,最大不能超過65535,而我在項目中嘗試發送XML大資料包到伺服器端,WCF傳回以上錯誤。
起初考試在用戶端嘗試重新定義BasicHttpBinding對象
1 Dim binding As BasicHttpBinding = New BasicHttpBinding()
2
3 binding.MaxBufferSize = 2147483647
4 binding.MaxReceivedMessageSize = 2147483647
5
然後考慮修改ServiceReferences.ClientConfig檔案,增加Buffer 尺寸。
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IDataService"
maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647">
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
但是,WCF仍舊報錯。
最後,嘗試修改伺服器端,在Web.config中添加自定義BasicHttpBinding對象,
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IDataService"
maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647"
maxBufferSize="2147483647">
<readerQuotas
maxArrayLength="2147483647"
maxBytesPerRead="2147483647"
maxDepth="2147483647"
maxNameTableCharCount="2147483647"
maxStringContentLength="2147483647" />
</binding>
</basicHttpBinding>
</bindings>
另外,在ServiceBehaviors中添加maxItemsInObjectGraph屬性
<behaviors>
<serviceBehaviors>
<behavior name="TeacherLogic.Net.Web.DataServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</serviceBehaviors>
</behaviors>
添加自定義Binding對象後,在endpoint中引用,就解決了“NotFound”問題了。
<endpoint address="" binding="basicHttpBinding" contract="Myproject.IDataService" bindingConfiguration="BasicHttpBinding_IDataService"/>
在Silverlight官方論壇還有一種說法,因為404 Notfound錯誤的,還可能因為跨域問題,這個問題比較容易解決,隻要在網站根目錄下建立一個clientaccesspolicy.xml檔案即可。
<?xml version="1.0" encoding="utf-8" ?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*">
<domain uri="http://*"/>
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy