wcf服务契约代理链
Posted 听哥哥的话
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了wcf服务契约代理链相关的知识,希望对你有一定的参考价值。
意图:为了是客户端代理呈现出面向对象的多态的特征 a. 服务端 1.契约 实现了契约的继承这个在服务端是一点问题没有,因为oprationcontract可以继承,虽然DataContract不能实现继承,注意IAnimal和IDog都是契约,但是我们通常喜欢用最 具体的那个契约来发布服务,因为他最丰富 using System; using System.ServiceModel; namespace WCF.Chapter2.InheritanceProxyChaining.Host { [ServiceContract] public interface IAnimal { [OperationContract] string AnimalSay(); } [ServiceContract] public interface IDog : IAnimal { [OperationContract] string DogSay(); } } 2.服务的实现 using System; using System.ServiceModel; namespace WCF.Chapter2.InheritanceProxyChaining.Host { public class Service : IDog { public string AnimalSay() { return "动物会叫会走路..."; } public string DogSay() { return "小狗汪汪叫..."; } } } 3.服务寄宿 using System; using System.ServiceModel; namespace WCF.Chapter2.InheritanceProxyChaining.Host { class Program { static void Main(string[] args) { using (ServiceHost host = new ServiceHost(typeof(Service))) { host.Opened += delegate { Console.WriteLine("服务已开启..."); }; host.Open(); Console.ReadLine(); } Console.WriteLine("服务已关闭..."); Console.ReadLine(); } } } 4.终结点配置 契约使用IDog的契约来发布服务 <?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="MEX"> <serviceMetadata httpGetEnabled="true"/> </behavior> </serviceBehaviors> </behaviors> <services> <service name="WCF.Chapter2.InheritanceProxyChaining.Host.Service" behaviorConfiguration="MEX"> <host> <baseAddresses> <add baseAddress="http://loacalhost:8000/"/> </baseAddresses> </host> <endpoint address="http://localhost:8001/Dog" binding="basicHttpBinding" contract="WCF.Chapter2.InheritanceProxyChaining.Host.IDog"></endpoint> </service> </services> </system.serviceModel> </configuration> b. 客户端 1.契约继承 只有命名空间不一样而已 using System; using System.ServiceModel; namespace WCF.Chapter2.InheritanceProxyChaining.Client { [ServiceContract] public interface IAnimal { [OperationContract] string AnimalSay(); } [ServiceContract] public interface IDog : IAnimal { [OperationContract] string DogSay(); } } 2. 客户端代理链的实现 using System; using System.ServiceModel; namespace WCF.Chapter2.InheritanceProxyChaining.Client { //clientbase<T> T使用最具体的接口 public class AnimalClientProxy : ClientBase<IDog>, IAnimal { public string AnimalSay() { return base.Channel.AnimalSay(); } } //代理链继承上一级代理实现 public class DogClientProxy : AnimalClientProxy, IDog { public string DogSay() { return base.Channel.DogSay(); } } } 3. 本地调用最终呈现出了面向对象的多态,本地化之后就是CLR的内容了,能实现面向对象的特性也是正常的 using System; using System.ServiceModel; namespace WCF.Chapter2.InheritanceProxyChaining.Client { class Program { static void Main(string[] args) { using (AnimalClientProxy animalProxy = new AnimalClientProxy()) { Console.WriteLine(animalProxy.AnimalSay()); Console.WriteLine(); } using (AnimalClientProxy animalProxy1 = new DogClientProxy()) { Console.WriteLine(animalProxy1.AnimalSay()); Console.WriteLine(); } using (DogClientProxy dogProxy = new DogClientProxy()) { Console.WriteLine(dogProxy.AnimalSay()); Console.WriteLine(dogProxy.DogSay()); Console.WriteLine(); } Console.ReadLine(); } } } 4.客户端终结点 用的也是最具体的契约 <?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="MEX"> <serviceMetadata httpGetEnabled="true"/> </behavior> </serviceBehaviors> </behaviors> <services> <service name="WCF.Chapter2.InheritanceProxyChaining.Host.Service" behaviorConfiguration="MEX"> <host> <baseAddresses> <add baseAddress="http://loacalhost:8000/"/> </baseAddresses> </host> <endpoint address="http://localhost:8001/Dog" binding="basicHttpBinding" contract="WCF.Chapter2.InheritanceProxyChaining.Host.IDog"></endpoint> </service> </services> </system.serviceModel> </configuration>
以上是关于wcf服务契约代理链的主要内容,如果未能解决你的问题,请参考以下文章