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

創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供大同網(wǎng)站建設(shè)、大同做網(wǎng)站、大同網(wǎng)站設(shè)計(jì)、大同網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、大同企業(yè)網(wǎng)站模板建站服務(wù),十多年大同做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(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ù)宿主程序的代碼得到一個(gè)類型引用,這個(gè)類型就是具體實(shí)現(xiàn)WCF服務(wù)的那個(gè)類,也是我們將要在宿主程序中運(yùn)行的類。
using語句用來對(duì)ServiceHost實(shí)例進(jìn)行初始化,在作用域結(jié)束時(shí)ServiceHost的Dispose()會(huì)被自動(dòng)調(diào)用。
(4)在using語句內(nèi)部,我們先啟動(dòng)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菜單項(xiàng)。
(7)在進(jìn)入下一個(gè)任務(wù)之前請(qǐng)確保解決方案能夠編譯通過(按CTRL+Shift+B快捷鍵)。
以上就是我們?yōu)榇蠹医榻B的有關(guān)WCF服務(wù)宿主程序的相關(guān)內(nèi)容。
網(wǎng)站欄目:WCF服務(wù)宿主程序正確實(shí)現(xiàn)方法解析
分享網(wǎng)址:http://m.fisionsoft.com.cn/article/dhpjiho.html


咨詢
建站咨詢
