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(); } } }
时间: 2024-10-05 05:31:26