WCF學習之旅目錄
第五步,創建數據服務
- 在“解決方案資源管理器”中,使用鼠標左鍵選中“SCF.WcfService”項目,然后在菜單欄上,依次選擇“項目”、“添加新項”。
- 在“添加新項”對話框中,選擇“Web”節點,然后選擇“WCF 服務”項。
- 在“名稱”文本框中,輸入 BookService,然后選擇“添加”按鈕。如下圖。
4.Visual Studio 2015會同時添加一個IBookService接口文件。這個接口代碼文件中的代碼如下:
[ServiceContract] public interface IBookService { [OperationContract] string GetBook(string Id); [OperationContract] string AddBook(string book); [OperationContract] string EditBook(string book); [OperationContract] string Search(string Category, string searchString); }
5. 在“解決方案資源管理器中”中,定位BookService.svc.cs文件,雙擊在“代碼編輯器”中打開,並編寫如下代碼。
public class BookService : IBookService { Entities db = new Entities(); public string AddBook(string mbook) { try { Books book = XMLHelper.DeSerializer<Books>(mbook); db.Books.Add(book); db.SaveChanges(); } catch (Exception ex) { return ex.Message; } return "true"; } public string EditBook(string mbook) { try { Books book = XMLHelper.DeSerializer<Books>(mbook); db.Entry(book).State = EntityState.Modified; db.SaveChanges(); } catch (Exception ex) { return ex.Message; } return "true"; } public string GetBook(string Id) { int bookId = Convert.ToInt32(Id); Books book= db.Books.Find(bookId); string xml=XMLHelper.ToXML<Books>(book); return xml; } public string Search(string Category, string searchString) { var cateLst = new List<string>(); var cateQry = from d in db.Books orderby d.Category select d.Category; cateLst.AddRange(cateQry.Distinct()); var books = from m in db.Books select m; if (!String.IsNullOrEmpty(searchString)) { books = books.Where(s => s.Name.Contains(searchString)); } List<Books> list = null; if (string.IsNullOrEmpty(Category)) { list = books.ToList<Books>(); } else { list = books.Where(x => x.Category == Category).ToList<Books>(); } return XMLHelper.ToXML<List<Books>>(list); } }
6. 在菜單欄上,依次選擇“調試”、“開始執行(不調試)”以運行服務。 此時將打開一個瀏覽窗口,並顯示該服務的 XML 架構。
7. 在地址欄中,在 BookService.svc 的 URL 末尾處輸入 Books,然后按 Enter 鍵。如下圖。
8. 關閉瀏覽器窗口。
第六步,創建宿主程序
- WCF服務需要依存一個運行着的進程(宿主),服務寄宿就是為服務指定一個宿主的過程。
- 在菜單欄上,依次選擇“文件-->新建-->項目”,或者如下圖在“解決方案資源管理器”中使用鼠標右鍵,彈出快捷菜單。
- 在“添加新項目”對話框中,展開 “Visual C#”和“Windows”節點,然后選擇“控制台應用程序”模板。 在“名稱”文本框中,輸入 Hosting,然后選擇“確定”按鈕。 如下圖。
- 我們可以通過代碼的方式完成所有的服務寄宿工作。在“解決方案資源管理器中”中,定位到Program.cs文件,雙擊在“代碼編輯器”中打開,並編寫如下代碼。下面的代碼通過一個控制台應用對 BookService的寄宿的實現。關於配置方式參見前一文章。
using SCF.WcfService; using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.ServiceModel.Description; using System.Text; using System.Threading.Tasks; namespace Hosting { class Program { // 無配置文件的啟動程序
static void Main(string[] args) { using (ServiceHost host = new ServiceHost(typeof(BookService))) { host.AddServiceEndpoint(typeof(IBookService), new WSHttpBinding(),
"http://127.0.0.1:8888/BookService"); if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null) { ServiceMetadataBehavior behavior = new ServiceMetadataBehavior(); behavior.HttpGetEnabled = true; behavior.HttpGetUrl = new Uri("http://127.0.0.1:8888/BookService/metadata"); host.Description.Behaviors.Add(behavior); } host.Opened += delegate { Console.WriteLine("BookService,按任意鍵終止服務!"); }; host.Open(); Console.Read(); } } }
在接下來的步驟中,將創建一個 Windows 窗體客戶端應用程序以使用該服務。