端点行为EndPointBehavior

定义IEndpointBehavior和IDispatchMessageInspector对入栈消息统计

服务端

static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(StudentService)))
            {
                host.Description.Endpoints[0].EndpointBehaviors.Add(new MyEndPointBehavior());

                host.Open();

                Console.WriteLine("opened");
                Console.ReadKey();
            }
        }
View Code
 public class MyEndPointBehavior : IEndpointBehavior
    {
        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {

        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {

        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
            endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new MyIDispatchMessageInspector());
        }

        public void Validate(ServiceEndpoint endpoint)
        {

        }
    }

    /// <summary>
    /// 消息检查器
    /// </summary>
    public class MyIDispatchMessageInspector : IDispatchMessageInspector
    {
        public static Dictionary<string, int> dic = new Dictionary<string, int>();
        /// <summary>
        /// 收到之后
        /// </summary>
        /// <param name="request"></param>
        /// <param name="channel"></param>
        /// <param name="instanceContext"></param>
        /// <returns></returns>
        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            var action = request.Headers.GetHeader<string>("Action"
                , "http://schemas.microsoft.com/ws/2005/05/addressing/none");

            if (!dic.ContainsKey(action))
            {
                dic[action] = 0;
            }

            dic[action]++;
            Console.WriteLine("当前时间:{0} action:{1} 调用次数{2} ", DateTime.Now, action, dic[action]);
            return request;
        }

        public void BeforeSendReply(ref Message reply, object correlationState)
        {

        }
    }
View Code
[ServiceContract]
    public interface IStudent
    {
        [OperationContract]
        void AddStu();

        [OperationContract]
        void GetStu();

        [OperationContract]
        void EditStu();

        [OperationContract]
        void delStu();
    }

public class StudentService : IStudent
    {
        public void AddStu()
        {
            Console.WriteLine("add stu");
        }

        public void delStu()
        {
            Console.WriteLine("del stu");
        }

        public void EditStu()
        {
            Console.WriteLine("edit stu");
        }

        public void GetStu()
        {
            Console.WriteLine("get stu");
        }
    }
View Code

客户端

static void Main(string[] args)
        {
            ServiceReference3.StudentClient client = new ServiceReference3.StudentClient();
            Action[] actions = new Action[4] {
                client.GetStu,
                client.AddStu,
                client.delStu,
                client.EditStu
            };

            while (true)
            {
                int index = new Random().Next(0, 4);
                actions[index]();
                Console.WriteLine("调用成功{0}", DateTime.Now);
                System.Threading.Thread.Sleep(200);
            }
        }
View Code

 

posted @ 2020-02-07 22:52  vvf  阅读(337)  评论(0编辑  收藏  举报