zoukankan      html  css  js  c++  java
  • WCF 第五章 行为 为服务终结点行为实现一个消息检测器

    列表5.23通过将一个终结点发送的和接收的每条消息打印出来来实现一个日志功能。代码显示了从一个终结点行为调用的消息检测器。这也是自定义寄宿服务如何将终结点行为手动添加到服务描述中去。

    提示  为跟踪实现自定义行为

    实际应用时,如果你需要为诊断的目的实现一个消息检测器,请查看第十章”异常处理”以获得跟踪技术。

      myMessageInspector 类实现了IDispatchMessageInspector接口。在它的BeforeSendRequest和AfterReceiveReply方法 中它把消息打印到控制台。类myEndpointBehavior实现了IEndpointBehavior接口。在它的 AddDispatchBehavior方法中它把myMessageInspector类添加到消息检测器列表中以便于可以被每一条消息调用。最后,主 程序将myEndpointBehavior类添加到所有终结点的行为列表中。注意因为服务也有一个MEX终结点,对终结点的请求和回复也会被 myEndpointBehavior打印出来。

    列表5.23 在一个服务终结点行为中的消息检测器

    01public class myEndpointBehavior : IEndpointBehavior
    02{
    03    public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
    04    {
    05    }
    06 
    07    public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
    08    {
    09    }
    10 
    11    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
    12    {
    13        endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new myMessageInspector());
    14    }
    15 
    16    public void Validate(ServiceEndpoint endpoint)
    17    {
    18    }
    19}
    01    public class myMessageInspector : IDispatchMessageInspector
    02    {
    03        public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request,
    04 
    05System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
    06        {
    07            Console.WriteLine(request.ToString());
    08            return request;
    09        }
    10 
    11        public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
    12        {
    13            Console.WriteLine(reply.ToString());
    14        }
    15    }
    01[ServiceContract]
    02public interface IStockService
    03{
    04    [OperationContract]
    05    double GetPrice(string ticker);
    06}
    07 
    08public class StockService : IStockService
    09{
    10    public double GetPrice(string ticker)
    11    {
    12        return 94.85;
    13    }
    14}


    ======

    转载自

     

  • 相关阅读:
    运用VS制作安装包
    return的总结
    Swift UIAlertController、UISegmentedControl
    Swift 菊花、UIPageControl和UIProgressView
    Swift UITextField各种属性的设置
    Swift 发送邮件和发短信
    Swift GCD
    swift 定义枚举和结构体 及使用
    iOS怎么来实现关闭自动锁屏
    IOS开发 清空数组正确方法
  • 原文地址:https://www.cnblogs.com/llbofchina/p/2094095.html
Copyright © 2011-2022 走看看