今天在项目中发送数据包到服务器端,WCF返回错误信息:The remote server returned an error: NotFound. 经过排查,通过以下步骤进行了解决。
Silverlight企业级项目中,经常要发送大量数据包到服务器端,而WCF服务本身对数据包进行了限制,最大不能超过65535,而我在项目中尝试发送XML大数据包到服务器端,WCF返回以上错误。
起初考试在客户端尝试重新定义BasicHttpBinding对象
Dim
binding
As
BasicHttpBinding
=
New
BasicHttpBinding()
binding.MaxBufferSize = 2147483647
binding.MaxReceivedMessageSize = 2147483647
binding.MaxBufferSize = 2147483647
binding.MaxReceivedMessageSize = 2147483647
然后考虑修改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
希望能够帮助到大家。
本文转自冷秋寒 51CTO博客,原文链接:http://blog.51cto.com/kevinfan/293828 ,如需转载请自行联系原作者