• IServiceBehavior IContractBehavior IEndpointBehavior IOperationBehavior


    using System;
    using System.Collections.ObjectModel;
    using System.Reflection;
    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using System.ServiceModel.Description;
    using System.ServiceModel.Dispatcher;
    
    namespace Behaviors
    {
        class MyServiceBehaviorAttribute : Attribute, IServiceBehavior
        {
            public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
            {
                Console.WriteLine("Inside {0}.{1}", this.GetType().Name, MethodBase.GetCurrentMethod().Name);
            }
    
            public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
            {
                Console.WriteLine("Inside {0}.{1}", this.GetType().Name, MethodBase.GetCurrentMethod().Name);
            }
    
            public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
            {
                Console.WriteLine("Inside {0}.{1}", this.GetType().Name, MethodBase.GetCurrentMethod().Name);
            }
        }
    
        class MyEndpointBehavior : IEndpointBehavior
        {
            public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
            {
                Console.WriteLine("Inside {0}.{1}, endpoint {2}", this.GetType().Name, MethodBase.GetCurrentMethod().Name, endpoint.Name);
            }
    
            public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
            {
                Console.WriteLine("Inside {0}.{1}, endpoint {2}", this.GetType().Name, MethodBase.GetCurrentMethod().Name, endpoint.Name);
            }
    
            public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
            {
                Console.WriteLine("Inside {0}.{1}, endpoint {2}", this.GetType().Name, MethodBase.GetCurrentMethod().Name, endpoint.Name);
            }
    
            public void Validate(ServiceEndpoint endpoint)
            {
                Console.WriteLine("Inside {0}.{1}, endpoint {2}", this.GetType().Name, MethodBase.GetCurrentMethod().Name, endpoint.Name);
            }
        }
    
        class MyContractBehaviorAttribute : Attribute, IContractBehavior
        {
            public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
            {
                Console.WriteLine("Inside {0}.{1}, contract {2}", this.GetType().Name, MethodBase.GetCurrentMethod().Name, contractDescription.ContractType.Name);
            }
    
            public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
            {
                Console.WriteLine("Inside {0}.{1}, contract {2}", this.GetType().Name, MethodBase.GetCurrentMethod().Name, contractDescription.ContractType.Name);
            }
    
            public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
            {
                Console.WriteLine("Inside {0}.{1}, contract {2}", this.GetType().Name, MethodBase.GetCurrentMethod().Name, contractDescription.ContractType.Name);
            }
    
            public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
            {
                Console.WriteLine("Inside {0}.{1}, contract {2}", this.GetType().Name, MethodBase.GetCurrentMethod().Name, contractDescription.ContractType.Name);
            }
        }
    
        class MyOperationBehaviorAttribute : Attribute, IOperationBehavior
        {
            public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
            {
                Console.WriteLine("Inside {0}.{1}, operation {2}", this.GetType().Name, MethodBase.GetCurrentMethod().Name, operationDescription.Name);
            }
    
            public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
            {
                Console.WriteLine("Inside {0}.{1}, operation {2}", this.GetType().Name, MethodBase.GetCurrentMethod().Name, operationDescription.Name);
            }
    
            public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
            {
                Console.WriteLine("Inside {0}.{1}, operation {2}", this.GetType().Name, MethodBase.GetCurrentMethod().Name, operationDescription.Name);
            }
    
            public void Validate(OperationDescription operationDescription)
            {
                Console.WriteLine("Inside {0}.{1}, operation {2}", this.GetType().Name, MethodBase.GetCurrentMethod().Name, operationDescription.Name);
            }
        }
    
        [ServiceContract]
        [MyContractBehavior]
        public interface ITest
        {
            [OperationContract]
            [MyOperationBehavior]
            int Add(int x, int y);
            [OperationContract]
            int Multiply(int x, int y);
        }
    
        [ServiceContract]
        [MyContractBehavior]
        public interface ITest2
        {
            [OperationContract]
            [MyOperationBehavior]
            string Echo(string text);
        }
    
        [MyServiceBehavior]
        public class Service : ITest, ITest2
        {
            public int Add(int x, int y) { return x + y; }
            public int Multiply(int x, int y) { return x * y; }
            public string Echo(string text) { return text; }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
                ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
                ServiceEndpoint ep1 = host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "basic");
                ep1.Name = "Endpoint1-BasicHttp";
                ep1.Behaviors.Add(new MyEndpointBehavior());
                ServiceEndpoint ep2 = host.AddServiceEndpoint(typeof(ITest2), new WSHttpBinding(), "ws");
                ep2.Name = "Endpoint2-WSHttp";
                ep2.Behaviors.Add(new MyEndpointBehavior());
                Console.WriteLine("Opening the host...");
                host.Open();
                Console.WriteLine("Host opened");
            }
        }
    }
    

      

  • 相关阅读:
    [整机笔记][转贴]硬盘无法双击打开的解决办法
    [网络随摘][转载]值得用一生回味的经典语录
    [网络随摘][转载]如果你已经过了20还不到25岁
    [网络随摘][转摘]只有十句话,我却看了十分钟
    实现百台手机异步并发定时自动GPS定位打卡,基于python全天后定时签到稳定版。
    【FireFox】在Firefox中,关于隐藏table中某一行tr,其他td的边框显示异常
    【记】屏蔽浏览器shift+鼠标滚轴事件
    【记】Javascript的函数直接量定义
    【Javascript】Javascript中的函数调用模式
    【记】three.js的一个简单的代码记录
  • 原文地址:https://www.cnblogs.com/xiangxiong/p/6775025.html
Copyright © 2020-2023  润新知