zoukankan      html  css  js  c++  java
  • Silverlight处理wcf异常

    1:新增一个类SilverlightFaultBehavior,代码如下

     public class SilverlightFaultBehavior : Attribute, IServiceBehavior
        {
            private class SilverlightFaultEndpointBehavior : IEndpointBehavior
            {
                public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
                {
                }
    
                public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
                {
                }
    
                public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
                {
    
                    endpointDispatcher.ChannelDispatcher.ErrorHandlers.Add(new ErrorHandler());
                    endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new SilverlightFaultMessageInspector());
                }
    
                public void Validate(ServiceEndpoint endpoint)
                {
                }
    
                private class SilverlightFaultMessageInspector : IDispatchMessageInspector
                {
                    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
                    {
                        return null;
                    }
    
                    public void BeforeSendReply(ref Message reply, object correlationState)
                    {
                        if ((reply != null) && reply.IsFault)
                        {
                            HttpResponseMessageProperty property = new HttpResponseMessageProperty();
                            property.StatusCode = HttpStatusCode.OK;
                            reply.Properties[HttpResponseMessageProperty.Name] = property;
                        }
                    }
                }
            }
    
            public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
            {
            }
    
            public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
            {
                foreach (ServiceEndpoint endpoint in serviceDescription.Endpoints)
                {
                    endpoint.Behaviors.Add(new SilverlightFaultEndpointBehavior());
                }
            }
    
            public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
            {
            }
        }
        public class ErrorHandler : IErrorHandler
        {
            public bool HandleError(System.Exception error)
            {
                return true;
            }
    
            public void ProvideFault(System.Exception error, MessageVersion version, ref Message fault)
            {
                //Log處理
                StreamWriter streamwriter = new StreamWriter(@"D:log.txt");
                streamwriter.Write(error.Message);
                streamwriter.Close();
            }
        }

    2:在配置文件中

    <system.serviceModel>
        <extensions>
          <behaviorExtensions>
            <add name="silverlightFaults"  type="ERPAssist.Services.Library.SilverlightFaultBehavior,ERPAssist.Services.Library,Version=1.0.0.0, Culture=neutral,PublicKeyToken=null"/>
          </behaviorExtensions>
        </extensions>
        <bindings>
          <basicHttpBinding>
            <binding maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
              <readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147483647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647"/>
            </binding>
          </basicHttpBinding>
        </bindings>
        <behaviors>
          <endpointBehaviors>
            <behavior name="silverlightFaults"></behavior>
          </endpointBehaviors>
          <serviceBehaviors>
            <behavior>
              <serviceMetadata httpGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="true" />
              <dataContractSerializer maxItemsInObjectGraph="2147483647" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true">
          <serviceActivations>
            <add relativeAddress="Services/IBarCodeContract.svc" service="ERPAssist.Services.BarCodeContract" />
          </serviceActivations>
          <baseAddressPrefixFilters />
        </serviceHostingEnvironment>
      </system.serviceModel>

    3:在wcf服务接口的实现类中加入 [SilverlightFaultBehavior]类特性

    4:在客户端即可直接捕捉异常,并且会把记录些到D盘

  • 相关阅读:
    计算机网络知识技能水平的测评试题
    Socket与系统调用深度分析
    学习构建调试Linux内核网络代码的环境MenuOS系统
    深入学习socket网络编程,以java语言为例
    网络配置工具iproute2和net-tools的基本原理和基本使用方法
    Linux系统学习总结报告
    结合中断上下文切换和进程上下文切换分析Linux内核的一般执行过程
    深入理解系统调用-40号调用
    基于mykernel2.0 编写一个操作系统内核
    交互式多媒体图书平台的设计与实现
  • 原文地址:https://www.cnblogs.com/Rmeo/p/3399364.html
Copyright © 2011-2022 走看看