SliverLight捕捉WCF服务异常,
1 Contract
[ServiceContract]
public interface IWCFService
{
[OperationContract]
int DoWork(int a,int b);
[OperationContract]
int DoWork2(int a, int b);
[OperationContract]
int DoDevide(int a, int b, ref string errorMessage);
[OperationContract]
int Devide(int a, int b, ref Exception error);
}
2 Service
public class WCFService : IWCFService
{
public int DoDevide(int a, int b, ref string errorMessage)
{
if (b == 0)
{
errorMessage = "Devided by Zero";
return 0;
}
errorMessage = "11111";
return a / b;
}
public int Devide(int a, int b, ref Exception error)
{
try
{
error = null;
return a / b;
}
catch (Exception ex)
{
error = ex;
return 0;
}
}
}
3 跨域访问策略服务接口
[ServiceContract]
public interface IDomainService
{
[OperationContract]
[WebGet(UriTemplate = "ClientAccessPolicy.xml")]
Message ProvidePolicyFile();
}
4
public System.ServiceModel.Channels.Message ProvidePolicyFile()
{
FileStream filestream = File.Open(@"ClientAccessPolicy.xml", FileMode.Open);
// Either specify ClientAcessPolicy.xml file path properly
// or put that in /Bin folder of the console application
XmlReader reader = XmlReader.Create(filestream);
System.ServiceModel.Channels.Message result = Message.CreateMessage(MessageVersion.None, "", reader);
return result;
}
5 clientaccesspolicy.xml
<?xml version="1.0" encoding="utf-8"?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*">
<domain uri="*"/>
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
6 App.Config
<configuration>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<behaviors>
<serviceBehaviors>
<behavior name="WCFBase.WCFServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="DomainServiceBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<!--
<bindings>
<basicHttpBinding>
<binding name="NoneSecurity"
maxBufferPoolSize="12000000" maxReceivedMessageSize="12000000" useDefaultWebProxy="false">
<readerQuotas maxStringContentLength="12000000" maxArrayLength="12000000"/>
<security mode="None"/>
</binding>
</basicHttpBinding>
</bindings>
-->
<services>
<service behaviorConfiguration="WCFBase.WCFServiceBehavior" name="WCFBase.WCFService">
<endpoint address="" binding="basicHttpBinding" contract="WCFBase.IWCFService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:9090/WCFService/" />
</baseAddresses>
</host>
</service>
<service name="WCFBase.DomainService">
<endpoint address="" behaviorConfiguration="DomainServiceBehavior"
binding="webHttpBinding" contract="WCFBase.IDomainService" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:9090/" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
7 Host
static void Main(string[] args)
{
ServiceHost host = new ServiceHost(typeof(WCFService));
host.Open();
ServiceHost crossDomainserviceHost = new ServiceHost(typeof(DomainService));
crossDomainserviceHost.Open();
Console.WriteLine("I'am Here.");
Console.ReadKey();
host.Close();
}
8 添加服务引用
9 实现
private void button1_Click(object sender, RoutedEventArgs e)
{
ServiceReference2.WCFServiceClient client = new ServiceReference2.WCFServiceClient();
client.DoDevideCompleted += new EventHandler<ServiceReference2.DoDevideCompletedEventArgs>(client_DoDevideCompleted);
string errorMessage = String.Empty;
client.DoDevideAsync(5, 0,errorMessage);
}
void client_DoDevideCompleted(object sender, ServiceReference2.DoDevideCompletedEventArgs e)
{
MessageBox.Show(e.errorMessage);
}
private void button2_Click(object sender, RoutedEventArgs e)
{
ServiceReference1.WCFServiceClient client = new ServiceReference1.WCFServiceClient();
client.DevideCompleted += new EventHandler<ServiceReference1.DevideCompletedEventArgs>(client_DevideCompleted);
Exception ex = new Exception();
client.DevideAsync(5, 0, ex);
}
void client_DevideCompleted(object sender, ServiceReference1.DevideCompletedEventArgs e)
{
MessageBox.Show(e.error.Message);
}