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.服务终结点配置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 { public HumanProxy() { } public HumanProxy(string configurationName) : base(configurationName) { } public string HumanSay() { return base.Channel.HumanSay(); } }}3.由2的结论这里给出终结点配置 3.manProxyusing System;using System.ServiceModel;namespace WCF.Chapter2.InheritanceReworked.Client{ public class ManProxy : ClientBase , IMan { public ManProxy() { } public ManProxy(string configurationName) : base(configurationName) { } public string HumanSay() { return base.Channel.HumanSay(); } public string ManSay() { return base.Channel.ManSay(); } }}4.womenproxyusing System;using System.ServiceModel;namespace WCF.Chapter2.InheritanceReworked.Client{ public class WomanProxy : ClientBase , 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(); } }}