WCF常见问题及解决方案

1.在WCF服务端使用HttpContext.Current为空的解决方案:

   1)在服务端WCF的类上加描述[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

   2)在服务端WEBCONFIG的<system.serviceModel>节点里加<serviceHostingEnvironment  aspNetCompatibilityEnabled="true"/>

2.在WCF服务端使用HttpContext.Current.Server.MapPath的替代解决方案:

  System.Web.Hosting.HostingEnvironment.MapPath("~/Upload" + fileFolder);

3.解决大文件byte[]传输的问题:

  服务端,Web.config文件里,Bindings节是空的,而Service也没有指定bindingConfiguration属性,那么它们采用的就是默认的65535的大小。

  问题找到,解决就比较容易了:

  在Bindings节添加新的Binding设置(位于system.serviceModel节点),指定最大接受数据:

  <bindings>
      <basicHttpBinding>
          <binding name="LargeData" 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="LargeData" 
         contract="Server.Service.WcfService" />   <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />   </service>

  这样就可以从客户端发送足够大的数据了。

  P.S.:

  .net默认只能传4M的文件,所以尽管设定了Wcf两端的配置,还是超不出.net的限定,所以如果要传输大文件,还需要在System.Web节下加上

    <httpRuntimemaxRequestLength="102400" />

  这里的单位是KB,这样就可以传100M的文件了。当然,这么大的文件,最好还是分段传输比较好。

4.出现下列错误的解决方案:

  ① 无法处理消息,这很可能是因为操作“http://tempuri.org/”不正确,或因为消息包含无效或过期的安全上下文令牌,或因为绑定之间出现不匹配。

     解决方法:只需在客户端和服务端的binding节点内修改如下节点: <securitymode="None"></security>

  ② 超时问题

     解决方法:客户端的<binding name="WSHttpBinding_IService" 节点下 sendTimeout="00:05:00" 为超时的时间,调整即可。

 

 

posted @ 2011-09-07 09:38  潇客的技术博客  阅读(3058)  评论(1编辑  收藏  举报