我希望我的Silverlight客户端能够显示在WCF调用期间在服务器上发生的异常。
鉴于我目前的代码创建WCF频道(在客户端上):
// create the binding elements
BinaryMessageEncodingBindingElement binaryMessageEncoding = new BinaryMessageEncodingBindingElement();
HttpTransportBindingElement httpTransport = new HttpTransportBindingElement() { MaxBufferSize = int.MaxValue, MaxReceivedMessageSize = int.MaxValue };
// add the binding elements into a Custom Binding
CustomBinding customBinding = new CustomBinding(binaryMessageEncoding, httpTransport);
// create the Endpoint URL
EndpointAddress endpointAddress = new EndpointAddress(serviceUrl);
// create an interface for the WCF service
ChannelFactory<TWcfApiEndPoint> channelFactory=new ChannelFactory<TWcfApiEndPoint>(customBinding, endpointAddress);
channelFactory.Faulted += new EventHandler(channelFactory_Faulted);
TWcfApiEndPoint client = channelFactory.CreateChannel();
return client;
当发生异常时,我只是得到一个“NotFound”异常,这显然没有用。我怎样才能获得异常信息?
我使用此代码来使用上面返回的客户端对象:
try
{
// customFieldsBroker is the client returned above
customFieldsBroker.BeginCreateCustomField(DataTypeID, newCustomField, (result) =>
{
var response = ((ICustomFieldsBroker)result.AsyncState).EndCreateCustomField(result);
}, customFieldsBroker);
}
catch (Exception ex)
{
// would like to handle exception here
}
在try {} catch {}块中包装Begin / End调用似乎甚至没有跳转到catch {}块。
如果重要,我在客户端使用Silverlight 3。
答案 0 :(得分:0)
由于浏览器沙箱中的安全限制,Silverlight无法查看服务器错误的主体(状态码500)。要使其正常工作,您需要对服务器端进行更改,以更改将错误返回给浏览器的方式。有MSDN article详细描述了它。
答案 1 :(得分:0)
你需要做两件事:
将异常作为错误异常抛出
[OperationContract的]
[FaultContract(typeof运算(ArithmeticFault))]
public int Calculate(Operation op,int a,int b)
{ // ... }
抛出新的FaultException();