Silverlight + WCF错误“The remote server returned an error: NotFound.”的解决方法

来源:互联网 发布:wp采集软件 编辑:程序博客网 时间:2024/05/16 07:19
今天在项目中发送数据包到服务器端,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
然后考虑修改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>
摘自: http://kevinfan.blog.51cto.com/1037293/293828
原创粉丝点击