在Silverlight客户端中使用WCF时,如何处理Exception?

     对于silverlight3.0 我们可以用WCF来进行通信,但是如果service发生错误,它将响应Http500错误使我们不能访问silverlight客户端,所以我们不得不 在service端进行try {} catch...,添加额外属性(Property) 如: public string exception {get;set;}操作。

    下面的方法介绍不使用try ...直接把错误的消息返回到silverlight客户端,  配置WCF Falut来用于silverlight 客户端

     1.定义WCF endpoint behavior

         

public   class  SilverlightFaultBehavior : BehaviorExtensionElement, IEndpointBehavior
{        
    
    
public   void  ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
        SilverlightFaultMessageInspector inspector 
=   new  SilverlightFaultMessageInspector();
        endpointDispatcher.DispatchRuntime.MessageInspectors.Add(inspector);
    }

    
public   class  SilverlightFaultMessageInspector : IDispatchMessageInspector
    {
        
        
public   void  BeforeSendReply( ref  Message reply,  object  correlationState)
        {
            
if  (reply.IsFault)
            {
                HttpResponseMessageProperty property 
=   new  HttpResponseMessageProperty();

                
//  Here the response code is changed to 200.
                property.StatusCode  =  System.Net.HttpStatusCode.OK;


                reply.Properties[HttpResponseMessageProperty.Name] 
=  property;
            }
        }
        
        
public   object  AfterReceiveRequest( ref  Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            
//  Do nothing to the incoming message.
             return   null ;
        }


    }

    
//  The following methods are stubs and not relevant. 

    
public   void  AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {
    }

    
public   void  ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
    }


    
public   void  Validate(ServiceEndpoint endpoint)
    {
    }

    
public   override  System.Type BehaviorType
    {
        
get  {  return   typeof (SilverlightFaultBehavior); }
    }

    
protected   override   object  CreateBehavior()
    {
        
return   new  SilverlightFaultBehavior();
    }

}

    这个WCF endpoint behavior需要在WCF service 文件(web.config)配置才能起到作用。

  2.配置Web.config

     

代码
< system.serviceModel >
    
< extensions >
      
< behaviorExtensions >
        
< add  name =”silverlightFaults” 
             
type =”Microsoft.Silverlight.Samples.SilverlightFaultBehavior, 
             
SilverlightFaultBehavior, 
             Version
=1.0.0.0, 
             
Culture =neutral, 
             
PublicKeyToken =null”/>
      
</behaviorExtensions >
    
</ extensions >
    
< behaviors >
        
< endpointBehaviors >
          
< behavior  name =”SilverlightFaultBehavior”>
            
<silverlightFaults />
          
</ behavior >
        
</ endpointBehaviors >
    
</ behaviors >
    
< services >
        
< service  name =”Calculator.Web.Service”>
            
<endpoint address =”” 
                      
binding =”basicHttpBinding” 
                      
contract =”Calculator.Web.Service” 
                      
behaviorConfiguration =”SilverlightFaultBehavior”  />
</ service >
  
</ services >
</ system.serviceModel >

  重要的一点是要在 serviceBehaviors 节点behavior下添加<serviceDebug includeExceptionDetailInFaults="true" />

 3.Silverlight客户端处理

  当异步请求Complected后,这个故障信息 可以被处理:

  

代码
void  proxy_CalculateCompleted( object  sender, CalculateCompletedEventArgs e)
{
    
if  (e.Error  ==   null )
    {
        
//  In case of success
    }
    
else   if  (e.Error  is  FaultException < ExceptionDetail > )
    {
        FaultException
< ExceptionDetail >  fault  =  e.Error  as  FaultException < ExceptionDetail > ;

         
//  fault.Detail.Type contains server exception type
         
//  fault.Detail.Message contains server exception message
         
//  fault.Detail.StackTrace contains servier stack trace
         
    }
}


  参考文章:MSDN

 

转载于:https://www.cnblogs.com/agiledev/archive/2010/02/03/1662626.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值