博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
wcf服务契约继承
阅读量:6851 次
发布时间:2019-06-26

本文共 5892 字,大约阅读时间需要 19 分钟。

a. 服务端1.契约 使用了继承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();    }}2.服务实现 实现了自己的具体的接口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 "我是女人,我爱漂亮!";        }    }}3.服务终结点配置
4.服务寄宿开启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. 客户端1.客户端等效契约 除了命名空间不一样其他的都一样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(); }}2.人类代理 男人和女人在服务端都实现了他,所以既可以是男人代表人,也可以是女人去代表人using System;using System.ServiceModel;namespace WCF.Chapter2.InheritanceReworked.Client{ public class HumanProxy : ClientBase
, IHuman { public HumanProxy() { } public HumanProxy(string configurationName) : base(configurationName) { } public string HumanSay() { return base.Channel.HumanSay(); } }}3.由2的结论这里给出终结点配置
3.manProxyusing System;using System.ServiceModel;namespace WCF.Chapter2.InheritanceReworked.Client{ public class ManProxy : ClientBase
, IMan { public ManProxy() { } public ManProxy(string configurationName) : base(configurationName) { } public string HumanSay() { return base.Channel.HumanSay(); } public string ManSay() { return base.Channel.ManSay(); } }}4.womenproxyusing System;using System.ServiceModel;namespace WCF.Chapter2.InheritanceReworked.Client{ public class WomanProxy : ClientBase
, IWoman { public WomanProxy() { } public WomanProxy(string configurationName) : base(configurationName) { } public string HumanSay() { return base.Channel.HumanSay(); } public string WomanSay() { return base.Channel.WomanSay(); } }}5.客户端调用代理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(); } }}

 

转载于:https://www.cnblogs.com/kexb/p/7436458.html

你可能感兴趣的文章
Instr() 函数
查看>>
hdu-acm steps Max sum
查看>>
Radar Installation
查看>>
组队项目四则运算成果
查看>>
使用UIPickerView显示数据
查看>>
java代码继承基础
查看>>
java继承实例基础
查看>>
数据库增删改查梳理
查看>>
linux下检测每个进程占用swap大小
查看>>
[转] 编译输出文件的区别
查看>>
Java MyBatis 插入数据库返回主键--insertSelective这样就不用每次到数据库里面查询了...
查看>>
springboot集成redis操作
查看>>
x64 QWORD Xor shellcode encoder
查看>>
大数据之mapreduce小实战
查看>>
Elasticsearch(二)
查看>>
一步一步学linq to sql(九)其他补充
查看>>
windows service and process 的关系
查看>>
转 Oracle 12C 之 CDB/PDB用户的创建与对象管理
查看>>
iOS常用设置界面跳转
查看>>
智能家居之红外遥控---手机万能红外遥控器
查看>>