項目中需要一個上傳文件的操作,於是想到一個解決方案:將客服端文件以二進制數組的形式,用WCF傳到服務器,在服務器端在寫成圖片。試過發現此方案可行,但是當文件過大時問題就出現了。
錯誤:“遠程服務器返回了錯誤: NotFound”
解決:
1. 遠程服務器返回了錯誤: NotFound,該錯誤一般為WCF調用不成功,SL的錯誤類型是CommunicationException,但是具體的錯誤類型及信息不完整,通過以下方法,可以使錯誤信息更加完成。
2. 在WCF服務的類中加上
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
3. 然后在調用此服務的silverlight的初始化方法中加上:
bool registerResult = WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
bool httpsResult = WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp);
參考博客:http://blog.csdn.net/luminji/article/details/5279695
查“遠程服務器返回了錯誤: NotFound”錯誤時,很多文章直接就指出了,引起錯誤原因:傳輸的數據量過大,需要配置服務端和客服端的Config。我還是按照以上方案操作了下,調試運行出現下面錯誤,確定了錯誤的真正原因。
錯誤:“遠程服務器返回了意外響應 400 Bad Request”
解決:
1. WCF服務文件的名稱UserRightService.svc、IUserRightService.cs和UserRightService.cs(下面的專業名稱可能不太准確)。
UserRightService.svc宿主,里面只關聯接口的空間
IUserRightService.cs接口文件
UserRightService.cs具體方法實現文件。
2. 下面配置服務端的Config,Config在WCF所屬的項目下面:
說明:
1. 2147483647最大字節數,這個是int32的最大字節數,大小是2047M,肯定能適合正常的數據傳輸。估計不用全部設置成這么大都夠用。
2. metadataBehavior 對應的是“<behavior name="metadataBehavior">”的name,名稱無所謂,只要與<service behaviorConfiguration="metadataBehavior">對應就好。
3. NewBinding0 對應的是“<binding name="NewBinding0">”的name,名稱無所謂,只要與<service>節點下面的<endpoint bindingConfiguration="NewBinding0">對應就好。
4. <bindings>節點下面的綁定類型有BasicHttpBinding和WsHttpBinding兩種類型。 要與<endpoint binding="basicHttpBinding">對應。
BasicHttpBinding和WsHttpBinding的異同點,可以參看http://www.cnblogs.com/virusswb/archive/2010/02/21/1670225.html。
5. <service name="Microinfo.Map.Services.UserRightService">的name是命名空間(Microinfo.Map.Services)+WCF實現類的類名(UserRightService)。
6. <endpoint contract="Microinfo.Map.Services.IUserRightService"/>的contract是命名空間(Microinfo.Map.Services)+WCF接口(IUserRightService)。
服務端Config配置
<configuration>
<system.serviceModel>
<services>
<service behaviorConfiguration="metadataBehavior" name="Microinfo.Map.Services.UserRightService">
<endpoint binding="basicHttpBinding" bindingConfiguration="NewBinding0" contract="Microinfo.Map.Services.IUserRightService"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="metadataBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
<bindings>
<basicHttpBinding>
<binding name="NewBinding0" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
</binding>
</basicHttpBinding>
</bindings>
</system.serviceModel>
</configuration>
3.下面是客服端Config的設置,這個文件是在引用WCF服務的項目中自動產生的,項目根目錄下,原博文說文件名App.Config,但是我的項目中文件名叫ServiceReferences.ClientConfig。一些需要注意的可以參看上面服務端Config設置的說明。
客服端Config配置
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IUserRightService" maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647">
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:2400/WCF/UserRightService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IUserRightService"
contract="UserRightServiceReference.IUserRightService" name="BasicHttpBinding_IUserRightService" />
</client>
</system.serviceModel>
</configuration>
參考博客:http://blog.csdn.net/qyr20/article/details/6585606。
如果配置了服務端和客服端以后還是報錯,就需要在實例化WCF服務時候再加代碼設置:
View Code
AIDClient bll = new AIDClient();
(bll.Endpoint.Binding as NetTcpBinding).MaxReceivedMessageSize = int.MaxValue;
(bll.Endpoint.Binding as NetTcpBinding).MaxBufferSize =int.MaxValue;
