解析Silverlight调用WCF/Rest异常的解决方法。下面我们来一步步讲解。
问题描述
在使用Silverlight调用WCF/Rest服务时,可能会遇到各种异常错误,比如:
- System.ServiceModel.CommunicationException
- System.ServiceModel.FaultException
- System.Net.WebException等等。
那么,如何解决这些异常呢?接下来,将详细讲解这个问题。
解决方法
针对这个问题,我们可以有以下方式解决:
1. 在服务端增加配置
在服务端的web.config中添加以下配置:
<system.serviceModel>
<services>
<service name="MyService">
<endpoint binding="customBinding" bindingConfiguration="crossDomain" contract="IMyService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/MyService/" />
</baseAddresses>
</host>
</service>
</services>
<bindings>
<customBinding>
<binding name="crossDomain">
<textMessageEncoding messageVersion="Soap11" />
<httpTransport allowCookies="false" authenticationScheme="Anonymous" bypassProxyOnLocal="true" hostNameComparisonMode="WeakWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" useDefaultWebProxy="true"/>
</binding>
</customBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
2. 在客户端增加配置
在客户端的App.config中添加以下配置:
<system.serviceModel>
<bindings>
<customBinding>
<binding name="CustomBinding_IMyService">
<textMessageEncoding messageVersion="Soap11" />
<httpTransport maxReceivedMessageSize="2147483647" allowCookies="true" authenticationScheme="Anonymous" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" proxyAuthenticationScheme="Anonymous" realm="" useDefaultWebProxy="false" />
</binding>
</customBinding>
</bindings>
<client>
<endpoint address="http://localhost:8000/MyService/" binding="customBinding" bindingConfiguration="CustomBinding_IMyService" contract="IMyService" name="CustomBinding_IMyService" />
</client>
</system.serviceModel>
示例一:使用Rest服务获取数据
在服务端,我们可以创建TestService.svc文件,内容如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace TestService
{
[ServiceContract]
public interface ITestService
{
[OperationContract]
[WebGet(UriTemplate = "/GetData/{value}", ResponseFormat = WebMessageFormat.Json)]
string GetData(string value);
}
public class TestService : ITestService
{
public string GetData(string value)
{
return "You entered: " + value;
}
}
}
在客户端,我们可以使用以下代码调用服务:
private void CallRestService()
{
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(OnDownloadStringCompleted);
client.DownloadStringAsync(new Uri("http://localhost:8000/TestService/GetData/123"));
}
private void OnDownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
MessageBox.Show(e.Result);
}
else
{
MessageBox.Show(e.Error.Message);
}
}
示例二:使用WCF服务获取数据
在服务端,我们可以创建TestService.svc文件,内容如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace TestService
{
[ServiceContract]
public interface ITestService
{
[OperationContract]
string GetData(string value);
}
public class TestService : ITestService
{
public string GetData(string value)
{
return "You entered: " + value;
}
}
}
在客户端,我们可以使用以下代码调用服务:
private void CallWcfService()
{
TestServiceClient client = new TestServiceClient();
client.GetDataCompleted += new EventHandler<GetDataCompletedEventArgs>(OnGetDataCompleted);
client.GetDataAsync("123");
}
private void OnGetDataCompleted(object sender, GetDataCompletedEventArgs e)
{
if (e.Error == null)
{
MessageBox.Show(e.Result);
}
else
{
MessageBox.Show(e.Error.Message);
}
}
通过以上方式,我们就可以解决Silverlight调用WCF/Rest异常的问题了。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:解析Silverlight调用WCF/Rest异常的解决方法 - Python技术站