Silverlight处理WCF异常

将WCF端的异常返回到Silverlight端有两种方法(不提示 远程服务not found,而是具体的错误)

第一种 在App.cs的构造里  bool bRegisterPrefix =WebRequest.RegisterPrefix(http://localhost:9999, WebRequestCreator.ClientHttp);  地址为服务的地址

第二种:  首先在服务里增加如下类文件

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();
        }
 
    }

 然后,在具体的服务类上加 [ServiceBehavior(IncludeExceptionDetailInFaults = true)]  , 如

           [ServiceBehavior(IncludeExceptionDetailInFaults = true)]

    public class Service2 : IService2
    {
        public void DoWork()
        {
            throw new Exception("Customer error!");
        }
    }

最后,修改web.config

  <system.serviceModel>

    <extensions>

           <behaviorExtensions>

                  <add name="silverlightFaults" type="MyWcfServices.SilverlightFaultBehavior,  MyWcfServices,  Version=1.0.0.0,  Culture=neutral, PublicKeyToken=null"/>

                           注意:type里,逗号加空格,不然报错 silverlightFaults未注册

</behaviorExtensions>

</extensions>

 <behaviors>

 <endpointBehaviors>

 <behavior name="silverlightFaults">

<silverlightFaults />

</behavior>

 </endpointBehaviors>

 

 <serviceBehaviors>

 <behavior >

 <serviceMetadata httpGetEnabled="true" />

 <serviceDebug includeExceptionDetailInFaults="true" />

</behavior>

 </serviceBehaviors>

</behaviors>

 

 <bindings>

 <basicHttpBinding>

<binding name="customBinding0">

 <binaryMessageEncoding />

 <httpTransport />

 </binding>

 </basicHttpBinding>

</bindings>

 

 

<services>

 <service name="SoloWcfServices">

<endpoint address="" binding="customBinding"

behaviorConfiguration="silverlightFaults"

bindingConfiguration="customBinding0"

contract="SoloWcfServices" />

 </service>

 </services>

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

</system.serviceModel>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值