WCF学习之旅----正式篇之基础框架
2019独角兽企业重金招聘Python工程师标准>>>
服务类包括服务契约IWCFService、操作契约OperationContract、和数据契约DataContract。
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Description;
using System.Runtime.Serialization;
namespace WcfServer
{/// <summary>///服务类 实现接口/// </summary>public class WcfServer : IWcfService{List<Book> bookList = new List<Book>();public WcfServer(){Book bk = new Book("琥珀之剑", " 绯炎", " 命运在我眼前分开成两条互不相关笔直的线");bookList.Add(bk);bk.SetBook("诛仙", "萧鼎", "方今之世,正道大昌,邪魔退避。中原大地山灵水秀,人气鼎盛,物产丰富,为正派诸家牢牢占据。其中尤以“青云门”、“天音寺”、和“焚香谷”为三大支柱,是为领袖。这个故事,便是从“青云门”开始的。");bookList.Add(bk);}public Book getBookMessageByName(string bookName){Book bk = new Book();foreach (var book in bookList){if (book.bookName == bookName){Console.WriteLine("返回书本信息成功");return bk = book;}}return bk;}public bool addBookMessage(Book book){if (book.getBook() != null){if (!bookList.Contains(book)){bookList.Add(book);Console.WriteLine("增加书目{0}", book.bookName);return true;}}return false;}public List<string> getBookNameList(){List<string> bookNameList = new List<string>();foreach (Book bk in bookList){Console.WriteLine("返回书籍列表信息成功");bookNameList.Add(bk.bookName);}return bookNameList;}}/// <summary>/// 服务契约 操作契约/// </summary>[ServiceContract]public interface IWcfService{[OperationContract]Book getBookMessageByName(string bookName);[OperationContract]bool addBookMessage(Book book);[OperationContract]List<string> getBookNameList();}/// <summary>/// 数据契约/// </summary>[DataContract]public struct Book{[DataMember]public string bookName;[DataMember]public string author;[DataMember]public string briefIntroduce;public Book(string bookName, string author, string briefIntroduce){this.bookName = bookName;this.author = author;this.briefIntroduce = briefIntroduce;}public void SetBook(string bookName, string author, string briefIntroduce){this.bookName = bookName;this.author = author;this.briefIntroduce = briefIntroduce;}public Book? getBook(){if (string.IsNullOrEmpty(this.bookName)){return null;}else{return this;}}}
}
WCF寄主
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using WcfServer;
namespace WCFHost
{class Program{static void Main(string[] args){using (ServiceHost host=new ServiceHost (typeof(WcfServer.WcfServer))){Uri httpAddress = new Uri("http://localhost:8002/WCFService");host.AddServiceEndpoint(typeof(WcfServer.IWcfService),new WSHttpBinding(),httpAddress);if (host.State != CommunicationState.Opening)host.Open();Console.WriteLine("************************");Console.WriteLine("**** ****");Console.WriteLine("**** 服务正在运行...****");Console.WriteLine("**** ****");Console.WriteLine("************************");Console.Read();}}}
}
客户端
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using WcfServer;
namespace WCFCliet
{class Program{static void Main(string[] args){IWcfService proxy = ChannelFactory<IWcfService>.CreateChannel(new WSHttpBinding(),new EndpointAddress("http://localhost:8002/WCFService"));Book book = new Book("英雄之国", "象不语", "曾经的英雄们,终已化作夜空下闪耀的点点星芒; 而不屈的后来者,则发誓要追随前人足迹、继承先祖无上的荣光。");List<string> bookNameList = new List<string>();while (true){Console.WriteLine("*****************************");Console.WriteLine("**** 0: 获取列表 ****");Console.WriteLine("**** 1:添加书本 ****");Console.WriteLine("**** 2:根据名字获取信息 ****");Console.WriteLine("**** 3:退出程序 ****");Console.WriteLine("*****************************");ConsoleKeyInfo info = Console.ReadKey();switch (info.Key){case ConsoleKey.D0:bookNameList = proxy.getBookNameList();foreach (string bookName in bookNameList){Console.WriteLine();Console.WriteLine(":: {0} ::", bookName);}break;case ConsoleKey.D1:if (proxy.addBookMessage(book)){Console.WriteLine("添加成功");}else{Console.WriteLine("添加失败或已存在");}break;case ConsoleKey.D2:Console.WriteLine("输入书名");string name = Console.ReadLine();book = proxy.getBookMessageByName(name);if (book.getBook() != null){Console.WriteLine("书名: {0}\r\n作者: {1}\r\n简介: {2}",book.bookName,book.author,book.briefIntroduce);}break;case ConsoleKey.D3:Environment.Exit(0);break;default:Console.WriteLine("输入有误,重新输入");break;}}}}
}
运行后:
转载于:https://my.oschina.net/hunjixin/blog/505159
WCF学习之旅----正式篇之基础框架相关推荐
- Python学习之旅(核心编程基础篇003运算符)
Python学习之旅 Python核心编程基础篇2020.12.18 一.算数运算符 二.比较运算符 三.赋值运算符 四.逻辑运算符 五.成员运算符 六.身份运算符 七.三目运算符 八.运算符优先级 ...
- WCF学习之旅—实现支持REST客户端应用(二十四)
WCF学习之旅-实现REST服务(二十二) WCF学习之旅-实现支持REST服务端应用(二十三) 在上二篇文章中简单介绍了一下RestFul与WCF支持RestFul所提供的方法,及创建一个支持RES ...
- WCF学习之旅—WCF服务的WAS寄宿(十二)
上接 WCF学习之旅-WCF服务部署到IIS7.5(九) WCF学习之旅-WCF服务部署到应用程序(十) WCF学习之旅-WCF服务的Windows 服务程序寄宿(十一) 八.WAS宿主 IIS ...
- WCF学习之旅—基于Fault Contract 的异常处理(十八)
WCF学习之旅-WCF中传统的异常处理(十六) WCF学习之旅-基于ServiceDebug的异常处理(十七) 三.基于Fault Contract 的异常处理 第二个示例是通过定制Servic ...
- WCF学习之旅—第三个示例之二(二十八)
上接WCF学习之旅-第三个示例之一(二十七) 五.在项目BookMgr.Model创建实体类数据 第一步,安装Entity Framework 1) 使用NuGet下载最新版的Entity Fram ...
- WCF学习之旅—第三个示例之四(三十)
上接WCF学习之旅-第三个示例之一(二十七) WCF学习之旅-第三个示例之二(二十八) WCF学习之旅-第三个示例之三(二十九) ...
- WCF学习之旅(一)---Hello World.
WCF学习之旅(一)---Hello World. 看了一些关于WCF的资料,从实例入手.记录自己学习WCF的过程. 从最简单的Hello World入门. 下图是我的solution及项目. 项目名 ...
- Java学习笔记系列-入门篇-计算机基础
Java学习笔记 Java学习笔记是一个持续更新的系列,工作多年,抽个空对自身知识做一个梳理和总结归纳,温故而知新,同时也希望能帮助到更多正在学习Java 的同学们. 本系列目录: 入门篇 计算机基础 ...
- LINQ学习之旅——最后一站LTX之基础和编程接口所包含类的简介
对LINQ TO SQL的内容讲解将告一段落,下面我们进入LINQ学习之旅的最后一站"LTX",即LINQ TO XML.当前XML技术在应用开发中显得越来越重要,已成为各类数据交 ...
最新文章
- 第2关:利用栈判断字符串括号是否匹配
- Redis集群管理(二)
- 【OpenGL从入门到精通(一)】Windows搭建OpenGL的渲染环境,并初始化一个OPenGL窗口
- 99%的程序员都在用Lombok,原理竟然这么简单?我也手撸了一个!
- 【SSH网上商城项目实战01】整合Struts2、Hibernate4.3和Spring4.2
- css中关于transform、transition、animate的区别
- Div 在页面中居中
- YOLOv5目标检测算法——通俗易懂的解析
- 带色彩恢复的视网膜增强算法实现 (MATLAB版本)
- 零基础转行学编程技术难吗?
- 什么是测试环境?如何搭建测试环境?
- # unity shader 后期轮廓(更新中)
- 【数字设计】联发科技_笔试面试题目分享
- SDUT 2021 Spring Individual Contest(for 20) - 9(2)(部分)补题
- linux开机启动动画
- 0成本涨粉过万详细复盘,新手也可100%实操上手!
- 磊科nw332 linux驱动下载,磊科Netcore NW332驱动
- 视频博主都在用的 音频素材网,免费还可商用
- ES analyzer和search_analyzer的比较
- 魔兽世界怀旧服服务器信息,魔兽世界怀旧服服务器类型有哪些_怀旧服服务器类型介绍...