博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WCF学习之旅----正式篇之基础框架
阅读量:7064 次
发布时间:2019-06-28

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

hot3.png

服务类包括服务契约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{    ///     ///服务类 实现接口    ///     public class WcfServer : IWcfService    {        List
 bookList = new List
();        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
 getBookNameList()        {            List
 bookNameList = new List
();            foreach (Book bk in bookList)            {                Console.WriteLine("返回书籍列表信息成功");                bookNameList.Add(bk.bookName);            }            return bookNameList;        }    }    /// 
    /// 服务契约 操作契约    ///     [ServiceContract]    public interface IWcfService    {        [OperationContract]        Book getBookMessageByName(string bookName);        [OperationContract]        bool addBookMessage(Book book);        [OperationContract]        List
 getBookNameList();    }    /// 
    /// 数据契约    ///     [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();           }        }    }}

220825_CUHr_2403989.png

客户端

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
.CreateChannel(new WSHttpBinding(),                new EndpointAddress("http://localhost:8002/WCFService"));            Book book = new Book("英雄之国", "象不语", "曾经的英雄们,终已化作夜空下闪耀的点点星芒; 而不屈的后来者,则发誓要追随前人足迹、继承先祖无上的荣光。");            List
 bookNameList = new List
();            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;                }            }        }    }}

220846_uWsJ_2403989.jpg

运行后:

220907_WBG1_2403989.png

转载于:https://my.oschina.net/hunjixin/blog/505159

你可能感兴趣的文章
CSS-border属性制作小三角
查看>>
面向接口编程详解(一)——思想基础
查看>>
YARN
查看>>
窗体的事件
查看>>
CentOS 7上安装gitlab-runner
查看>>
手摸手,带你用 vue 动画实现原生 app 切换效果,丝滑般的体验
查看>>
CSS 中 calc() 函数用法
查看>>
ahjesus 让我的MVC web API支持JsonP跨域
查看>>
集合的划分(递归)
查看>>
【笔记】读取properties文件
查看>>
Windows环境下安装 mysql-8.0.11-winx64 遇到的问题解决办法
查看>>
CAD 正在重生成模型然后卡住不动
查看>>
@angular/cli项目构建--Dynamic.Form
查看>>
python cookbook学习笔记[一次完成多个字符串的替换]
查看>>
用LyX写中文幻灯片
查看>>
让虚拟机支持USB HDD 启动
查看>>
八数码问题
查看>>
持续集成之⑤:jenkins结合脚本实现代码自动化部署及一键回滚至上一版本
查看>>
关于malloc的一个未解决的疑问
查看>>
java内存管理机制
查看>>