意图:为了是客户端代理呈现出面向对象的多态的特征
a. 服务端
.契约 实现了契约的继承这个在服务端是一点问题没有,因为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();
}
} .服务的实现
using System;
using System.ServiceModel; namespace WCF.Chapter2.InheritanceProxyChaining.Host
{
public class Service : IDog
{
public string AnimalSay()
{
return "动物会叫会走路...";
} public string DogSay()
{
return "小狗汪汪叫...";
}
}
} .服务寄宿
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();
}
}
} .终结点配置 契约使用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. 客户端
.契约继承 只有命名空间不一样而已
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();
}
}
. 客户端代理链的实现
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();
}
}
}
. 本地调用最终呈现出了面向对象的多态,本地化之后就是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();
}
}
} .客户端终结点 用的也是最具体的契约
<?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服务契约代理链的更多相关文章

  1. WCF分布式开发步步为赢(6):WCF服务契约继承与分解设计

    上一节我们学习了WCF分布式开发步步为赢(5)服务契约与操作重载部分.今天我们来继续学习WCF服务契约继承和服务分解设计相关的知识点.WCF服务契约继承有何优势和缺点?实际项目里契约设计有什么原则和依 ...

  2. WCF数据契约代理和已知类型的使用

    using Bll; using System; using System.CodeDom; using System.Collections.Generic; using System.Collec ...

  3. wcf服务契约继承

    a. 服务端 .契约 使用了继承 using System; using System.ServiceModel; namespace WCF.Chapter2.InheritanceReworked ...

  4. wcf服务契约的重载

    a. 服务端 .服务端 契约用OperationContract的Name实现重载 using System; using System.Collections.Generic; using Syst ...

  5. 跟我一起学WCF(6)——深入解析服务契约[下篇]

    一.引言 在上一篇博文中,我们分析了如何在WCF中实现操作重载,其主要实现要点是服务端通过ServiceContract的Name属性来为操作定义一个别名来使操作名不一样,而在客户端是通过重写客户端代 ...

  6. 跟我一起学WCF(5)——深入解析服务契约[上篇]

    一.引言 在上一篇博文中,我们创建了一个简单WCF应用程序,在其中介绍到WCF最重要的概念又是终结点,而终结点又是由ABC组成的.对于Address地址也就是告诉客户端WCF服务所在的位置,而Cont ...

  7. 客户端使用自定义代理类访问WCF服务 z

    通常在客户端访问WCF服务时,都需要添加服务引用,然后在客户端app.config或 web.config文件中产生WCF服务的客户端配置信息.若是每添加一个服务都是这样做,这样势必会将比较麻烦,能否 ...

  8. 客户端使用自定义代理类访问WCF服务

    通常在客户端访问WCF服务时,都需要添加服务引用,然后在客户端app.config或web.config文件中产生WCF服务的客户端配置信息.若是每添加一个服务都是这样做,这样势必会将比较麻烦,能否简 ...

  9. 实现jquery.ajax及原生的XMLHttpRequest调用WCF服务的方法

    废话不多说,直接讲解实现步骤 一.首先我们需定义支持WEB HTTP方法调用的WCF服务契约及实现服务契约类(重点关注各attribute),代码如下: //IAddService.cs namesp ...

随机推荐

  1. windows,linux,esxi系统判断当前主机是物理机还是虚拟机?查询主机序列号命令

    参考网站:https://blog.csdn.net/yangzhenping/article/details/49996765 查序列号: http://www.bubuko.com/infodet ...

  2. Genymotion——VirtualBox cannot start virtual device

    提示"VirtualBox cannot start virtual device" 打开VirtualBox,想要在里面直接启动Genymotion模拟器,又出现错误,提示“Un ...

  3. linux 关于数据库的部分命令

    开启数据库服务 service mysqld start 关闭数据库服务 service mysqld stop 链接数据库 mysql -h localhost -u root -p 回车然后输入密 ...

  4. ADO 读写文本文件

    ' 创建配置文件            Open ThisWorkbook.Path & "\schema.ini" For Append As #1            ...

  5. H5-BLOB

    BLOB 对象为h5的产物.普遍用于传输或者存储数据. <a 标签的新属性 download 表明 此a标签点击后,不是href跳转而是要下载.download的内容表示下载文件名.但是目前部分 ...

  6. SpringMvc 获取ApplicationContext

    有时,我们不通过Controller层进入Service层,比如同步数据,任务,以及文件上传共通Handler对文件处理后保存数据等都会由一个非Controller类调用Service. 这时候如果n ...

  7. 16 MySQL--正确使用索引

    count 统计 count(*)和count(字段名) 基本结果是一样的 但是一种情况例外,就是当某字段名下边的数据有null值的时候,不计入这个count中,*则全部列入count中 一 .索引未 ...

  8. ABAP-Generate dynpro动态屏幕

    1.获取屏幕参数值 FUN: RS_SCRP_GET_SCREEN_INFOS call function 'RS_SCRP_GET_SCREEN_INFOS' exporting dynnr = ' ...

  9. Null value was assigned to a property of primitive type setter of"原因及解决方法

    在action请求数据的过程中报出"Null value was assigned to a property of primitive type setter of"错误,搜索之 ...

  10. How to Pronounce PROBABLY

    How to Pronounce PROBABLY Share Tweet Share Though this is a content word, it’s frequently reduced. ...