意图:为了是客户端代理呈现出面向对象的多态的特征
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. linnux-shell知识

    awk 是单行处理文本 正则: cat test.py | awk '/a/ {print $1}'  # 如果test.py中存在a这个字母,就打印所在行的第一列(注意是该列,不是该词).匹配成功, ...

  2. GET和POST的真正区别

    文章来源: http://www.nowamagic.net/librarys/veda/detail/1919 如果有人问你,GET和POST,有什么区别?你会如何回答? 我的经历 前几天有人问我这 ...

  3. getitem, setitem, delitem (把类实例化成字典的类型)

    class Foo(object):     def __init__(self):         self.data = {} def __getitem__(self, key):        ...

  4. idea常用设置(持续更新)

    1.注释模板 Setting里找到live Templates (1)创建一个Templates Group (2)在Templates Group下创建Live Template 2.常用内置模板 ...

  5. egret -纹理集的制作

    1. 理集的使用 :http://www.codeandweb.com/ 下载软件: TexturePackergithub: 相关工具:https://github.com/ping-chen/eg ...

  6. Supervisor 进程管理工具

    简介: Supervisor 进程管理工具 一.安装 shell > yum -y install python-pip shell > pip install supervisor # ...

  7. delphi 中判断对象是否具备某一属性

    Uses   TypInfo;         {$R   *.dfm}         procedure   TForm1.Button1Click(Sender:   TObject);     ...

  8. c++实现贪食蛇

    今天老师布置了作业 让我们观察c++实现贪食蛇的代码 #include<windows.h> #include<time.h> #include<stdlib.h> ...

  9. spring 整合 struts2 xml配置

    整合之前要搞清楚struts2是什么; struts2:表现层框架  增删改查  作用域  页面跳转   异常处理  ajax 上传下载  excel   调用service spring :IOC/ ...

  10. sar命令详细信息

    sar(System Activity Reporter系统活动情况报告)是目前 Linux 上最为全面的系统性能分析工具之一,可以从多方面对系统的活动进行报告,包括:文件的读写情况.系统调用的使用情 ...