zoukankan      html  css  js  c++  java
  • 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");
            }
        }
    }
    

      

  • 相关阅读:
    OGG初始化之使用Oracle Data Pump加载数据
    OGG初始化之使用数据库实用程序加载数据
    OGG初始加载过程概述
    管理凭证存储中的身份
    使用ENCKEYS方法加密数据
    使用主密钥和钱包方法加密数据
    配置Oracle GoldenGate安全性
    建立Oracle GoldenGate凭证
    System.DllNotFoundException:“无法加载 DLL“SQLite.Interop.dll”: 找不到指定的模块 的解决方法
    记录一下Oracle添加、查看表、字段注释及导出ddl
  • 原文地址:https://www.cnblogs.com/xiangxiong/p/6775025.html
Copyright © 2011-2022 走看看