• wcf服务契约继承


    a. 服务端
    1.契约 使用了继承
    using System;
    using System.ServiceModel;
    
    namespace WCF.Chapter2.InheritanceReworked.Host
    {
        [ServiceContract]
        public interface IHuman
        {
            [OperationContract]
            string HumanSay();
        }
    
        [ServiceContract]
        public interface IMan : IHuman
        {
            [OperationContract]
            string ManSay();
        }
    
        [ServiceContract]
        public interface IWoman : IHuman
        {
            [OperationContract]
            string WomanSay();
        }
    }
    2.服务实现 实现了自己的具体的接口
    using System;
    using System.ServiceModel;
    
    namespace WCF.Chapter2.InheritanceReworked.Host
    {
        public class ManService : IMan
        {
            public string HumanSay()
            {
                return " 我是人,我会思考!";
            }
    
            public string ManSay()
            {
                return "我是男人,我力气比较大!";
            }
        }
    
        public class WomanService : IWoman
        {
            public string HumanSay()
            {
                return " 我是人,我会思考!";
            }
    
            public string WomanSay()
            {
                return "我是女人,我爱漂亮!";
            }
        }
    }
    
    3.服务终结点配置
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
        <services>
          <service name="WCF.Chapter2.InheritanceReworked.Host.ManService" behaviorConfiguration="MEX">
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:8000"/>
              </baseAddresses>
            </host>
            <endpoint address="http://localhost:8001/Man" binding="basicHttpBinding" contract="WCF.Chapter2.InheritanceReworked.Host.IMan"></endpoint>
          </service>
          
          <service name="WCF.Chapter2.InheritanceReworked.Host.WomanService" behaviorConfiguration="MEX">
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:9000"/>
              </baseAddresses>
            </host>
            <endpoint address="net.tcp://localhost:9001/Woman" binding="netTcpBinding" contract="WCF.Chapter2.InheritanceReworked.Host.IWoman"></endpoint>
          </service>
        </services>
        
        <behaviors>
          <serviceBehaviors>
            <behavior name="MEX">
              <serviceMetadata httpGetEnabled="true"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>
    </configuration>
    4.服务寄宿开启
    using System;
    using System.ServiceModel;
    
    namespace WCF.Chapter2.InheritanceReworked.Host
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (ServiceHost hostMan = new ServiceHost(typeof(ManService)))
                {
                    hostMan.Opened += delegate
                    {
                        Console.WriteLine("Man服务已开启...");
                    };
                    hostMan.Open();
    
                    using (ServiceHost hostWoman = new ServiceHost(typeof(WomanService)))
                    {
                        hostWoman.Opened += delegate
                        {
                            Console.WriteLine("Woman服务已开启...");
                        };
                        hostWoman.Open();
    
                        Console.ReadLine();
                    }
                    Console.WriteLine("Woman服务已关闭...");
    
                    Console.ReadLine();
                }
                Console.WriteLine("Man服务已关闭...");
            }
        }
    }
    
    b. 客户端
    1.客户端等效契约 除了命名空间不一样其他的都一样
    using System;
    using System.ServiceModel;
    
    namespace WCF.Chapter2.InheritanceReworked.Client
    {
        [ServiceContract]
        public interface IHuman
        {
            [OperationContract]
            string HumanSay();
        }
    
        [ServiceContract]
        public interface IMan : IHuman
        {
            [OperationContract]
            string ManSay();
        }
    
        [ServiceContract]
        public interface IWoman : IHuman
        {
            [OperationContract]
            string WomanSay();
        }
    }
    2.人类代理 男人和女人在服务端都实现了他,所以既可以是男人代表人,也可以是女人去代表人
    using System;
    using System.ServiceModel;
    
    namespace WCF.Chapter2.InheritanceReworked.Client
    {
        public class HumanProxy : ClientBase<IHuman>, IHuman
        {
            public HumanProxy()
            { }
    
            public HumanProxy(string configurationName) :
                base(configurationName)
            { }
    
            public string HumanSay()
            {
                return base.Channel.HumanSay();
            }
    
        }
    }
    3.由2的结论这里给出终结点配置
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
        <client>
          <endpoint name="human_man" address="http://localhost:8001/Man" binding="basicHttpBinding" contract="WCF.Chapter2.InheritanceReworked.Client.IHuman"></endpoint>
          <endpoint name="man" address="http://localhost:8001/Man" binding="basicHttpBinding" contract="WCF.Chapter2.InheritanceReworked.Client.IMan"></endpoint>
          <endpoint name="human_woman" address="net.tcp://localhost:9001/Woman" binding="netTcpBinding" contract="WCF.Chapter2.InheritanceReworked.Client.IHuman"></endpoint>
          <endpoint name="woman" address="net.tcp://localhost:9001/Woman" binding="netTcpBinding" contract="WCF.Chapter2.InheritanceReworked.Client.IWoman"></endpoint>
        </client>
      </system.serviceModel>
    </configuration>
    3.manProxy
    using System;
    using System.ServiceModel;
    
    namespace WCF.Chapter2.InheritanceReworked.Client
    {
        public class ManProxy : ClientBase<IMan>, IMan
        {
            public ManProxy()
            { }
    
            public ManProxy(string configurationName) :
                base(configurationName)
            { }
    
            public string HumanSay()
            {
                return base.Channel.HumanSay();
            }
    
            public string ManSay()
            {
                return base.Channel.ManSay();
            }
        }
    }
    4.womenproxy
    using System;
    using System.ServiceModel;
    
    namespace WCF.Chapter2.InheritanceReworked.Client
    {
        public class WomanProxy : ClientBase<IWoman>, IWoman
        {
            public WomanProxy()
            { }
    
            public WomanProxy(string configurationName) :
                base(configurationName)
            { }
    
            public string HumanSay()
            {
                return base.Channel.HumanSay();
            }
    
            public string WomanSay()
            {
                return base.Channel.WomanSay();
            }
    
        }
    }
    
    5.客户端调用代理
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace WCF.Chapter2.InheritanceReworked.Client
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (HumanProxy humanProxy_man = new HumanProxy("human_man"))
                {
                    Console.WriteLine("humanProxy_man:");
                    Console.WriteLine(humanProxy_man.HumanSay());
                    Console.WriteLine("");
                }
    
                using (HumanProxy humanProxy_woman = new HumanProxy("human_woman"))
                {
                    Console.WriteLine("humanProxy_woman:");
                    Console.WriteLine(humanProxy_woman.HumanSay());
                    Console.WriteLine("");
                }
    
                using (ManProxy manProxy = new ManProxy("man"))
                {
                    Console.WriteLine("manProxy_human:");
                    Console.WriteLine(manProxy.HumanSay());
                    Console.WriteLine("");
    
                    Console.WriteLine("manProxy_man:");
                    Console.WriteLine(manProxy.ManSay());
                    Console.WriteLine("");
                }
    
                using (WomanProxy womanProxy = new WomanProxy("woman"))
                {
                    Console.WriteLine("womanProxy_human:");
                    Console.WriteLine(womanProxy.HumanSay());
                    Console.WriteLine();
    
                    Console.WriteLine("womanProxy_woman:");
                    Console.WriteLine(womanProxy.WomanSay());
                    Console.WriteLine();
                }
    
                Console.ReadLine();
            }
        }
    }
  • 相关阅读:
    HDU 2045 不容易系列之(3)—— LELE的RPG难题 (递推)
    HDU 2050 折线分割平面 (递推)
    HDU 5441 Travel (并查集+数学+计数)
    HDU 4597 Play Game (DP,记忆化搜索,博弈)
    HDU 4599 Dice (概率DP+数学+快速幂)
    HDU 4497 GCD and LCM (数学,质数分解)
    UVa 1312 Cricket Field (枚举+离散化)
    HDU 4499 Cannon (暴力求解)
    HDU 4496 D-City (并查集)
    javascript你不知道的知识点
  • 原文地址:https://www.cnblogs.com/kexb/p/7436458.html
Copyright © 2020-2023  润新知