意图:为了是客户端代理呈现出面向对象的多态的特征
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. vue-cli 3.0之跨域请求代理配置及axios路径配置

    vue-cli 3.0之跨域请求代理配置及axios路径配置 问题:在前后端分离的跨域请求中,报跨域问题 配置: vue.config.js: module.exports = { runtimeCo ...

  2. leetcode7

    public class Solution { public int Reverse(int x) { ; ) { fuhao = -; } try { x = Math.Abs(x); } catc ...

  3. Git .gitignore使用 -- 过滤class文件或指定目录

    1. 进入当前的项目根目录 执行 git init touch .gitignore 2. 过滤class文件或指定目录 *.class /target/ 3. 提交 git add . 将所有文件提 ...

  4. 多线程--Java

    多线程: 1.进程和线程 进程是资源分配的最小单位,线程是CPU调度的最小单位. 每个进程的创建都需要系统为其开辟资源内存空间,并发执行的程序在执行过程中分配和管理资源的基本单位,速度和销毁也较慢.进 ...

  5. Linq相关

    Linq(语言集成查询) 相关资料如下: 1. Linq语言集成查询 百度百科 2. 30分钟Linq教程 3. Linq查询表达式(C#编程指南) 4. Linq十个常用查询 5. Linq技术专题 ...

  6. ubuntu16.04 安装opencv3.3

    from: http://www.pyimagesearch.com/2016/10/24/ubuntu-16-04-how-to-install-opencv/ Step #1: Install O ...

  7. 在Linux下判断系统当前是否开启了超线程

    #lscpu Thread(s) per core: 2Core(s) per socket: 6Socket(s): 2 -------------------------------------- ...

  8. PostgreSql别名区分大小写的问题

    PostgreSql是区分大小写的,如果别名的大小不一致就会提示错误: SELECT * FROM ( SELECT cpi."product_item_id" "PRO ...

  9. Servlet Request 请求转发

    request.getRequestDispatcher("logined.jsp").forward(request, response);    //登录用户在登录页面验证通过 ...

  10. HTML CSS + DIV实现整体布局 part2

    9.盒模型的层次关系 我们通过一个经典的盒模型3D立体结构图来理解,如图:     从上往下看,层次关系如下: 第1层:盒子的边框(border),     第2层:元素的内容(content).内边 ...