zoukankan      html  css  js  c++  java
  • WCF 常见逻辑和代码 1.错误处理和配置

    这些东西都很多人写过了.....

    放在这里只是做一个备份,要用的时候找的到 - -#

    需要的人直接拷贝代码就可以运行了

    注意只能运行在WCF 4.0中...因为为了少些点.....

    以下是c#代码用于定义错误处理

    View Code
        public class ErrorHandler : IErrorHandler
    {
    #if DEBUG
    public static ConcurrentDictionary<DateTime, Exception> ExceptionList = new ConcurrentDictionary<DateTime, Exception>();
    #endif

    private static WebBodyFormatMessageProperty bodyFormat = new WebBodyFormatMessageProperty(WebContentFormat.Xml);
    private static HttpResponseMessageProperty responseMessage = new HttpResponseMessageProperty() { StatusCode = System.Net.HttpStatusCode.InternalServerError };
    public bool HandleError(Exception error)
    {
    return true;
    }

    public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
    {
    #if DEBUG
    ExceptionList[DateTime.Now] = error;//just debug, don't care time conflict
    #endif
    Logger.Current.WriteEntry(error);

    var exception = error as OperationException;//custom exception type
    if (exception != null)
    {
    //要修改返回内容注意这里
    fault = Message.CreateMessage(version, "", new OperationResult(Convert.ToInt32(exception.ErrorCode), exception.ToString()), new DataContractJsonSerializer(typeof(OperationResult)));
    }
    else
    {
    fault = Message.CreateMessage(version, "", new OperationResult(-1, error.ToString()), new DataContractJsonSerializer(typeof(OperationResult)));
    }
    //要修改返回类型注意这里
    fault.Properties.Add(WebBodyFormatMessageProperty.Name, bodyFormat);//use xml format
    fault.Properties.Add(HttpResponseMessageProperty.Name, responseMessage);//error code
    }
    }

    public class ErrorHandlerExtension : BehaviorExtensionElement, IServiceBehavior
    {
    public override Type BehaviorType
    {
    get { return GetType(); }
    }

    protected override object CreateBehavior()
    {
    return this;
    }

    private IErrorHandler GetInstance()
    {
    return new ErrorHandler();
    }

    void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
    {
    }

    void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
    IErrorHandler errorHandlerInstance = GetInstance();
    foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers)
    {
    dispatcher.ErrorHandlers.Add(errorHandlerInstance);
    }
    }

    void IServiceBehavior.Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {

    }
    }

    /// <summary>
    /// just wrap errorcode and errormessage
    /// </summary>
    public class OperationException : System.ApplicationException
    {
    public ErrorCode ErrorCode { get; set; }
    public OperationException(ErrorCode errorCode)
    {
    this.ErrorCode = errorCode;
    }
    public OperationException(ErrorCode errorCode, string message)
    : base(message)
    {
    this.ErrorCode = errorCode;
    }
    }

    以下是在web.config中的配置

        <extensions>
    <behaviorExtensions>
    <add name="errorHandler" type="{命名空间}.ValidationBehaviorSection, {dll名字}"/>
    </behaviorExtensions>
    </extensions>
    <behaviors>
    <serviceBehaviors>
    <behavior>
    <errorHandler/>
    </behavior>
    </serviceBehaviors>
    </behaviors>

    要注意的方法就是ProvideFault 用于处理异常 (在没有执行完这个方法前不返回)

    HandleError方法不阻止消息返回..用来写日志会好点

  • 相关阅读:
    黑板客爬虫闯关 代码
    新浪云SAE搭建python环境 问题拾遗
    关于python中的字符串编码理解
    linux环境中使用转义字符使命令行字符颜色高亮
    python中list作为全局变量无需global声明的原因
    获取youku视频下载链接(wireshark抓包分析)
    改变linux默认配色方案(dircolors和dircolors-solarized使用)
    限流常规设计和实例
    连接池-Mybatis源码
    Transaction-Mybatis源码
  • 原文地址:https://www.cnblogs.com/PurpleTide/p/2200402.html
Copyright © 2011-2022 走看看