新聞中心
WCF開發(fā)工具是微軟公司研發(fā)的一種功能強大的開發(fā)插件,是一個.NET Framework 3.5的重要組成部分。我們今天將會通過這篇文章中介紹的內(nèi)容充分的了解到有關(guān)WCF服務(wù)宿主程序的實現(xiàn)方法。#t#

創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供大同網(wǎng)站建設(shè)、大同做網(wǎng)站、大同網(wǎng)站設(shè)計、大同網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、大同企業(yè)網(wǎng)站模板建站服務(wù),十多年大同做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。
(1)在類文件中,添加using語句來導(dǎo)入下面的名字空間:
·System.ServiceModel
·System.Configuration
·DerivativesCalculatorService
(2)代碼看起來應(yīng)該如下所示:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Configuration;
- using System.ServiceModel;
- using DerivativesCalculatorService;
- namespace Host
- {
- class Program
- {
- static void Main(string[] args)
- {
- }
- }
- }
(3)在Main方法中添加下面的代碼:
- static void Main(string[] args)
- {
- Type serviceType = typeof(Calculator);
- using (ServiceHost host = new ServiceHost(serviceType))
- {
- }
- }
***行WCF服務(wù)宿主程序的代碼得到一個類型引用,這個類型就是具體實現(xiàn)WCF服務(wù)的那個類,也是我們將要在宿主程序中運行的類。
using語句用來對ServiceHost實例進行初始化,在作用域結(jié)束時ServiceHost的Dispose()會被自動調(diào)用。
(4)在using語句內(nèi)部,我們先啟動ServiceHost,然后通過等待用戶輸入的方式來阻止應(yīng)用程序退出。
(5)下面是完整的WCF服務(wù)宿主程序代碼,新增的代碼加亮顯示。
- namespace Host
- {
- class Program
- {
- static void Main(string[] args)
- {
- Type serviceType = typeof(Calculator);
- using (ServiceHost host = new ServiceHost(serviceType))
- {
- host.Open();
- Console.WriteLine("The calculator service is available.");
- Console.ReadKey();
- }
- }
- }
- }
(6)選擇File | Save All菜單項。
(7)在進入下一個任務(wù)之前請確保解決方案能夠編譯通過(按CTRL+Shift+B快捷鍵)。
以上就是我們?yōu)榇蠹医榻B的有關(guān)WCF服務(wù)宿主程序的相關(guān)內(nèi)容。
當(dāng)前名稱:WCF服務(wù)宿主程序正確實現(xiàn)方法解析
網(wǎng)頁URL:http://www.dlmjj.cn/article/dhpjiho.html


咨詢
建站咨詢
