专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > WCF

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

发布时间:2011-06-23 13:50:17 文章来源:www.iduyao.cn 采编人员:星星草
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 }

 

  

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

2楼okjulien
先收藏了。
1楼toEverybody
性能这么差的,应用的也少,研究这个会不会浪费时间
Re: __浮沉丶若轩◇
@toEverybody,也不能这么说,只能说各有各的好处!
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: