zoukankan      html  css  js  c++  java
  • WCF学习笔记二:服务端Log

    问题提出:

    通过这几天对WCF的学习,准备使用WCF实现 服务端Log

    参考资料:

    服务站: 使用自定义行为扩展 WCF

    总结WCF中截获消息的几种方式

    总结WCF中截获消息的几种方式(二)

    聚焦WCF行为的扩展

    总结WCF中截获消息的几种方式中的几种方法进行分析。

    1  路由截获法:

      根据总结WCF中截获消息的几种方式文中优缺点总结,不适合。

    2  自定义Binding法:

      因为与Binding关联,使Binding的替换实现起来比较复杂,所以不打算使用。

    3  实现IMessageInspector接口法:

      步骤1、编写MessageInspector类,实现IDispatchMessageInspector接口。

    代码
    public class MessageInspector : IDispatchMessageInspector
    {
    #region IDispatchMessageInspector Members

    public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
    {
    Console.WriteLine(request.ToString());
    return null;
    }

    public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
    {

    }

    #endregion
    }

      步骤2、将MessageInspector注入到EndpointDispatcher.DispatchRuntime.MessageInspectors中。

        有两种实现方法:

          a、编写MessageServiceBehavior类,实现IServiceBehavior接口。

    代码
    public class MessageServiceBehavior : IServiceBehavior
    {
    #region IServiceBehavior Members

    public void AddBindingParameters(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
    {

    }

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
    {
    foreach (ChannelDispatcher cDispatcher in serviceHostBase.ChannelDispatchers)
    foreach (EndpointDispatcher endpointDispatcher in cDispatcher.Endpoints)
    endpointDispatcher.DispatchRuntime.MessageInspectors.Add(
    new MessageInspector());

    }

    public void Validate(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
    {

    }

    #endregion
    }

          b、编写MessageEndpointBehavior类,实现IEndpointBehavior接口。

    代码
    public class MessageEndpointBehavior:IServiceBehavior, IEndpointBehavior
    {
    #region IServiceBehavior Members

    public void AddBindingParameters(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
    {
    foreach (ServiceEndpoint endpoint in serviceHostBase.Description.Endpoints)
    {
    endpoint.Behaviors.Add(
    this);
    }
    }

    public void Validate(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
    {
    }

    #endregion

    #region IEndpointBehavior Members

    public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
    {
    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
    {
    endpointDispatcher.DispatchRuntime.MessageInspectors.Add(
    new MessageInspector());
    }

    public void Validate(ServiceEndpoint endpoint)
    {
    }

    #endregion
    }

      其实结果是一样的:都是将MessageInspector添加到服务端每个EndpointDispatcher(Host.ChannelDispatchers.Endpoints中)的MessageInspectors(EndpointDispatcher.DispatchRuntime.MessageInspectors)中。

    4  跟踪诊断法:

      自定义CustomTraceListener,继承自TraceListener抽象类。

    代码
    public class CustomTraceListener : TraceListener
    {
    public override void Write(string message)
    {
    Console.Write(message);
    }

    public override void WriteLine(string message)
    {
    Console.WriteLine(message);
    }
    }

      配置参考:配置消息日志记录

     

    代码下载:https://files.cnblogs.com/xujiaoxiang/Fly_Wcf_MessageLog.zip

  • 相关阅读:
    基于html2canvas实现网页保存为图片及图片清晰度优化
    玩转 React(四)- 创造一个新的 HTML 标签
    浅谈前后端分离与实践(一)
    javascript新手实例1-DOM基本操作
    一个看一次就永远不会忘的windows环境开发小技巧
    细说Web API中的Blob
    所见即所得,实现一个有趣的动画效果
    带你玩转prefetch, preload, dns-prefetch,defer和async
    Hologres+Flink流批一体首次落地4982亿背后的营销分析大屏
    浏览器报错:ERR_PROXY_CONNECTION_FAILED的解决方法
  • 原文地址:https://www.cnblogs.com/xujiaoxiang/p/1750040.html
Copyright © 2011-2022 走看看