十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
WCF开发工具是微软公司研发的一种功能强大的开发插件,是一个.NET Framework 3.5的重要组成部分。我们今天将会通过这篇文章中介绍的内容充分的了解到有关WCF服务宿主程序的实现方法。#t#

创新互联建站专业为企业提供大同网站建设、大同做网站、大同网站设计、大同网站制作等企业网站建设、网页设计与制作、大同企业网站模板建站服务,十多年大同做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
(1)在类文件中,添加using语句来导入下面的名字空间:
·System.ServiceModel
·System.Configuration
·DerivativesCalculatorService
(2)代码看起来应该如下所示:
- 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服务宿主程序的代码得到一个类型引用,这个类型就是具体实现WCF服务的那个类,也是我们将要在宿主程序中运行的类。
using语句用来对ServiceHost实例进行初始化,在作用域结束时ServiceHost的Dispose()会被自动调用。
(4)在using语句内部,我们先启动ServiceHost,然后通过等待用户输入的方式来阻止应用程序退出。
(5)下面是完整的WCF服务宿主程序代码,新增的代码加亮显示。
- 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)在进入下一个任务之前请确保解决方案能够编译通过(按CTRL+Shift+B快捷键)。
以上就是我们为大家介绍的有关WCF服务宿主程序的相关内容。