a. 服务端
.契约 使用了继承
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();
}
}
.服务实现 实现了自己的具体的接口
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 "我是女人,我爱漂亮!";
}
}
} .服务终结点配置
<?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>
.服务寄宿开启
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. 客户端
.客户端等效契约 除了命名空间不一样其他的都一样
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();
}
}
.人类代理 男人和女人在服务端都实现了他,所以既可以是男人代表人,也可以是女人去代表人
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();
} }
}
.由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>
.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();
}
}
}
.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();
} }
} .客户端调用代理
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();
}
}
}

wcf服务契约继承的更多相关文章

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

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

  2. wcf服务契约代理链

    意图:为了是客户端代理呈现出面向对象的多态的特征 a. 服务端 .契约 实现了契约的继承这个在服务端是一点问题没有,因为oprationcontract可以继承,虽然DataContract不能实现继 ...

  3. wcf服务契约的重载

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

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

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

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

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

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

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

  7. WCF服务属性注入基础设施

    WCF服务属性注入基础设施 WCF的服务的创建行为:使用默认构造函数创建WCF服务对象.如果我们想要在WCF内使用外部对象,最简单的方式就是把外部对象做成全局对象.然而这样的话会增加全局对象的数量,让 ...

  8. WCF服务寄宿IIS与Windows服务 - C#/.NET

    WCF是Windows平台下程序间通讯的应用程序框架.整合和 .net Remoting,WebService,Socket的机制,是用来开发windows平台上分布式开发的最佳选择.wcf程序的运行 ...

  9. WCF服务寄宿IIS与Windows服务

      WCF是Windows平台下程序间通讯的应用程序框架.整合和 .net Remoting,WebService,Socket的机制,是用来开发windows平台上分布式开发的最佳选择.wcf程序的 ...

随机推荐

  1. jpa 一对多and 多对一

    配置: <?xml version="1.0" encoding="UTF-8"?> <persistence version="2 ...

  2. 用Redis实现分布式锁 与 实现任务队列【转载】

    这一次总结和分享用Redis实现分布式锁 与 实现任务队列 这两大强大的功能.先扯点个人观点,之前我看了一篇博文说博客园的文章大部分都是分享代码,博文里强调说分享思路比分享代码更重要(貌似大概是这个意 ...

  3. MySQL优化十大技巧

    转自:https://m.2cto.com/database/201701/557910.html MYSQL优化主要分为以下四大方面: 设计:存储引擎,字段类型,范式与逆范式 功能:索引,缓存,分区 ...

  4. 基于OpenGL编写一个简易的2D渲染框架-06 编写一个粒子系统

    在这篇文章中,我将详细说明如何编写一个简易的粒子系统. 粒子系统可以模拟许多效果,下图便是这次的粒子系统的显示效果.为了方便演示,就弄成了一个动图. 图中,同时显示了 7 种不同粒子效果,看上去效果挺 ...

  5. 12 python json&pickle&shelve模块

      1.什么叫序列化 序列化是指把内存里的数据类型转变成字符串,以使其能存储到硬盘或通过网络传输到远程,因为硬盘或网络传输时只能接受bytes(字节) 2.用于序列化的两个模块,json和pickle ...

  6. 11 python shutil 模块

      shutil 模块 高级的 文件.文件夹.压缩包 处理模块 1.将文件内容拷贝到另一个文件中 import shutil f1 = open('os_模块.py','r',encoding='ut ...

  7. 使用JS获取当前地理位置方法汇总

    使用JS获取当前地理位置方法汇总 投稿:hebedich 字体:[增加 减小] 类型:转载 时间:2014-12-18我要评论 这篇文章主要介绍了使用JS获取当前地理位置方法汇总,需要的朋友可以参考下 ...

  8. JavaScript中有三个可以对字符串编码的函数,分别是: escape(),encodeURI(),encodeURIComponent()

    JavaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decod ...

  9. python语法学习之函数、类、模块

    Python中通过使用类(class)和对象(object)来实现面向对象(object-oriented programming,简称OOP)的编程. 面向对象编程的最主要目的是提高程序的重复使用性 ...

  10. C++ 11 中的 Lambda 表达式的使用

    Lambda在C#中使用得非常频繁,并且可以使代码变得简洁,优雅. 在C++11 中也加入了 Lambda. 它是这个样子的 [] () {}...  是的三种括号开会的节奏~ [] 的作用是表示La ...