WCF学习之旅----正式篇之基础框架

服务类包括服务契约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();
           }
        }
    }
}

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<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;
                }
            }
        }
    }
}

220846_uWsJ_2403989.jpg

运行后:

220907_WBG1_2403989.png

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值