WCF学习——构建第二个WCF应用程序(五)

一、创建数据服务

  1.在“解决方案资源管理器”中,使用鼠标左键选中“WcfService”项目,然后在菜单栏上,依次选择“项目”、“添加新项”。

  2.在“添加新项”对话框中,选择“Web”节点,然后选择“WCF 服务”项。

  3.在“名称”文本框中,输入 BookService,然后选择“添加”按钮。如下图

  

  4.Visual Studio 2013会同时添加一个IBookService接口文件。这个接口代码文件中的代码如下:

  

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Runtime.Serialization;
 5 using System.ServiceModel;
 6 using System.Text;
 7
 8 namespace WcfService
 9 {
10     /// <summary>
11     /// 书籍协定
12     /// </summary>
13     [ServiceContract]
14     public interface IBookService
15     {
16         /// <summary>
17         /// 通过Id得到书籍信息
18         /// </summary>
19         /// <param name="Id"></param>
20         /// <returns></returns>
21         [OperationContract]
22         Books GetBook(int Id);
23
24         /// <summary>
25         /// 得到所有书籍
26         /// </summary>
27         /// <returns></returns>
28         [OperationContract]
29         List<Books> GetList();
30
31         /// <summary>
32         /// 添加书籍
33         /// </summary>
34         /// <param name="books"></param>
35         /// <returns></returns>
36         [OperationContract]
37         int AddBook(Books books);
38
39
40         /// <summary>
41         /// 删除书籍
42         /// </summary>
43         /// <param name="id"></param>
44         /// <returns></returns>
45         [OperationContract]
46         int delBook(int id);
47
48         /// <summary>
49         /// 修改书籍
50         /// </summary>
51         /// <param name="book"></param>
52         /// <returns></returns>
53         [OperationContract]
54         int editBook(Books book);
55     }
56 }

  5. 在“解决方案资源管理器中”中,定位BookService.svc.cs文件,双击在“代码编辑器”中打开,并编写如下代码。

  

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Runtime.Serialization;
  5 using System.ServiceModel;
  6 using System.Text;
  7
  8 namespace WcfService
  9 {
 10     /// <summary>
 11     /// 书籍服务协定实现
 12     /// </summary>
 13     public class BookService : IBookService
 14     {
 15         /// <summary>
 16         /// 通过Id得到书籍信息
 17         /// </summary>
 18         /// <param name="Id"></param>
 19         /// <returns></returns>
 20         public Books GetBook(int id)
 21         {
 22             using ( BookShopPlusEntities book = new BookShopPlusEntities())
 23             {
 24                 try
 25                 {
 26                     var books = (from s in book.Books
 27                              where s.Id == id
 28                              select s).SingleOrDefault();
 29                     //var bookss = book.Books.Where(e => e.Id == Id).SingleOrDefault();
 30                     return books as Books;
 31                 }
 32                 catch (Exception ex)
 33                 {
 34                     throw ex;
 35                 }
 36             }
 37         }
 38
 39         /// <summary>
 40         /// 得到所有书籍
 41         /// </summary>
 42         /// <returns></returns>
 43         public List<Books> GetList()
 44         {
 45             using (BookShopPlusEntities book = new BookShopPlusEntities())
 46             {
 47                 try
 48                 {
 49                     var books = from b in book.Books select b;
 50                     return books as List<Books>;
 51                 }
 52                 catch (Exception ex)
 53                 {
 54                     throw ex;
 55                 }
 56             }
 57         }
 58
 59         /// <summary>
 60         /// 添加书籍
 61         /// </summary>
 62         /// <param name="books"></param>
 63         /// <returns></returns>
 64         public int AddBook(Books books)
 65         {
 66             using (BookShopPlusEntities book = new BookShopPlusEntities())
 67             {
 68                 try
 69                 {
 70                     book.Books.Add(books);
 71                     //保存到数据库
 72                     return book.SaveChanges();
 73                 }
 74                 catch (Exception ex)
 75                 {
 76                     throw ex;
 77                 }
 78             }
 79         }
 80
 81         /// <summary>
 82         /// 删除书籍
 83         /// </summary>
 84         /// <param name="id"></param>
 85         /// <returns></returns>
 86         public int delBook(int id)
 87         {
 88             using (BookShopPlusEntities book = new BookShopPlusEntities())
 89             {
 90                 try
 91                 {
 92                     var books = book.Books.Where(e => e.Id == id).SingleOrDefault();
 93                     book.Books.Remove(books);
 94                     return book.SaveChanges();
 95                 }
 96                 catch (Exception ex)
 97                 {
 98                     throw ex;
 99                 }
100             }
101         }
102
103         /// <summary>
104         /// 修改书籍
105         /// </summary>
106         /// <param name="books"></param>
107         /// <returns></returns>
108         public int editBook(Books books)
109         {
110             using (BookShopPlusEntities book = new BookShopPlusEntities())
111             {
112                 try
113                 {
114                     //得到一条数据
115                     var bk = book.Books.Where(e => e.Id == books.Id).SingleOrDefault();
116                     //进行修改
117                     bk.Title = books.Title;
118                     bk.Author = books.Author;
119                     bk.PublishDate = books.PublishDate;
120                     bk.UnitPrice = books.UnitPrice;
121                     bk.TOC = books.TOC;
122                     bk.Publishers.Name = books.Publishers.Name;
123                     //提交
124                     return book.SaveChanges();
125
126                 }
127                 catch (Exception ex)
128                 {
129                     throw ex;
130                 }
131             }
132         }
133     }
134 }

 

    6.  在菜单栏上,依次选择“调试”、“开始执行(不调试)”以运行服务。

    7.打开一个浏览窗口,把地址(http://localhost:64501/BookService.svc)输入到游览器,然后按 Enter 键,可以看到该服务的 XML 架构。

  

8. 关闭浏览器窗口。

二、创建宿主程序

  

  1.WCF服务需要依存一个运行着的进程(宿主),服务寄宿就是为服务指定一个宿主的过程。

  2.在菜单栏上,依次选择“文件-->新建-->项目”,或者如下图在“解决方案资源管理器”中使用鼠标右键,弹出快捷菜单。

  

  

  3.在“添加新项目”对话框中,展开 “Visual C#”和“Windows”节点,然后选择“控制台应用程序”模板。 在“名称”文本框中,输入 Hosting,然后选择“确定”按钮。 如下图。

  

  4.我们可以通过代码的方式完成所有的服务寄宿工作。在“解决方案资源管理器中”中,定位到Program.cs文件,双击在“代码编辑器”中打开,并编写如下代码。下面的代码通过一个控制台应用对 BookService的寄宿的实现。关于配置方式参见前一文章。

  

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using WcfService;
 7 using System.ServiceModel;
 8 using System.ServiceModel.Description;
 9
10 namespace Hosting
11 {
12     class Program
13     {
14         static void Main(string[] args)
15         {
16             //没有通过配置文件的方式启动程序
17
18             //提供服务的主机
19             using (ServiceHost host = new ServiceHost(typeof(BookService)))
20             {
21                 //添加终结点
22                 host.AddServiceEndpoint(typeof(BookService),new WSHttpBinding(),"http://127.0.0.1:9898/BookService");
23                 //判断控制服务元数据和相关信息的发布
24                 if(host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
25                 {
26                     //创建一个控制服务
27                     ServiceMetadataBehavior metdata = new ServiceMetadataBehavior();
28                     //控制服务元数据和相关信息的发布时使用Http/Get进行检索
29                     metdata.HttpGetEnabled = true;
30                     //http/Get请求的位置
31                     metdata.HttpGetUrl = new Uri("http://127.0.0.1:9898/BookService/wcf");
32                     //添加到主机中
33                     host.Description.Behaviors.Add(metdata);
34                 }
35                 //添加一个事件
36                 host.Opened += delegate
37                 {
38                     Console.WriteLine("BookService已启动,按任意键终止服务!");
39                 };
40                 //启动服务
41                 host.Open();
42
43                 Console.ReadLine();
44             }
45         }
46     }
47 }

 

在接下来的步骤中,将创建一个 控制台应用程序以使用该服务。

时间: 2024-08-26 06:04:34

WCF学习——构建第二个WCF应用程序(五)的相关文章

WCF学习——构建第二个WCF应用程序(四)

一.WCF服务端应用程序 1.创建WCF服务端应用程序项目 打开Visual Studio 2013,在菜单上点击文件->新建->项目->WCF服务应用程序.在弹出界面的"名称"对应的文本框中输入"WcfService",然后点 击"确定"按钮.如下图.   2.安装Entity Framework 1)使用NuGet下载最新版的Entity Framework 6.1.3.在解决方案资源管理器中-->在项目WcfServ

WCF学习系列二---【WCF Interview Questions – Part 2 翻译系列】

http://www.topwcftutorials.net/2012/09/wcf-faqs-part2.html WCF Interview Questions – Part 2 This WCF tutorial is part-2 in series of WCF Interview Questions. Other parts in this series can be found here. 这是WCF问答系列教程中的第二部分,其他部分可以在下面的链接中找到: WCF Service

WCF学习系列一【WCF Interview Questions-Part 1 翻译系列】

http://www.topwcftutorials.net/2012/08/wcf-faqs-part1.html WCF Interview Questions – Part 1 This WCF Tutorial is a collection of most frequently asked interview questions about Windows Communication Foundation (WCF) covering the beginner to professio

WCF学习系列四--【WCF Interview Questions – Part 4 翻译系列】

WCF Interview Questions – Part 4 This WCF service tutorial is part-4 in series of WCF Interview Questions. Before reading this please go through the following articles in this series. 这是WCF问答教程的第四部分,在阅读之前请先去看下面列出来的文章. WCF Service Interview Questions 

WCF学习——构建一个简单的WCF应用(一)

本文的WCF服务应用功能很简单,却涵盖了一个完整WCF应用的基本结构.希望本文能对那些准备开始学习WCF的初学者提供一些帮助. 在这个例子中,我们将实现一个简单的计算器和传统的分布式通信框架一样,WCF本质上提供一个跨进程.跨机器.跨网络的服务调用.在本例中,客户端和WCF应用服务通过运行在同一台机器上的不同进程模拟. 步骤一.构建整个解决方案 1.创建一个空白的解决方案 2.添加四个项目和引用及关系  Service.Interface  用于定义服务契约的类库项目,引用WCF核心程序集Sys

WCF学习——构建一个简单的WCF应用(二)

我们接着上一篇文章进行讲解 http://www.cnblogs.com/songjianhui/p/7060698.html 一:客户端通过添加引用调用服务 WCF应用服务被成功寄宿后,WCF服务应用便开始了服务调用请求的监听工作.此外,服务寄宿将服务描述通过元数据的形式发布出来,相应的客户端就可以获取这些元数据.接下来我们来创建客户端程序进行服务的调用. 1)先运行服务寄宿程序(Hosting.exe) 2) 在Visual Studio 2013的"解决方案资源管理器"中,把Cl

WCF学习系列汇总

最近在学习WCF,打算把一整个系列的文章都”写“出来,包括理论和实践,这里的“写”是翻译,是国外的大牛写好的,我只是搬运工外加翻译.翻译的不好,大家请指正,谢谢了.如果觉得不错的话,也可以给我点赞,这样我翻译下去的动力就更足了~~~ 1. Beginning WCF - MUST HAVE WCF Basics - FAQs Series[WCF基础----问答系列教程] WCF Vs ASMX WCF Known Types WCF Contracts WCF Bindings Instanc

WCF Basics - FAQs Series【WCF基础----问答系列教程】

WCF学习系列一[WCF Interview Questions-Part 1 翻译系列] WCF学习系列二---[WCF Interview Questions – Part 2 翻译系列] WCF学习系列三--[WCF Interview Questions – Part 3 翻译系列] WCF学习系列四--[WCF Interview Questions – Part 4 翻译系列]

WCF学习之旅—WCF服务部署到应用程序(十)

五.控制台应用程序宿主 (1) 在解决方案下新建控制台输出项目 ConsoleHosting.如下图. (2)添加 System.ServiceModel.dll 的引用. (3)添加 WCF 服务类库(WcfServiceLib)的项目引用. (4)创建宿主程序,代码如下: using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.Serv