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" 為超時的時間,調整即可。
?
?
轉載于:https://www.cnblogs.com/shawker/archive/2011/09/07/2146189.html
總結
以上是生活随笔為你收集整理的WCF常见问题及解决方案的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++标准转换运算符static_cas
- 下一篇: 使用MEF构建可扩展的Silverlig