您所在的位置: 首页>>开发>>语言&工具>>

WCF宿主与服务托管(2)

http://developer.51cto.com  2008-03-07 10:22  张逸  博客园  我要评论(0)
  • 摘要:WAS是IIS 7.0的一部分,但也可以独立地安装与配置。WAS支持所有可用的WCF传输协议、端口与队列。利用WAS托管服务与IIS宿主托管服务的方法并没有太大的区别,仍然需要创建svc文件,同时在IIS中需要在站点中创建应有程序指向托管应用程序,还可以设置访问服务的别名与应用程序池。
  • 标签:WCF  宿主  服务  托管

1、自托管宿主

利用WCF提供的ServiceHost提供的Open()和Close()方法,可以便于开发者在控制台应用程序,Windows应用程序乃至于ASP.NET应用程序中托管服务。不管自宿主的环境是何种应用程序,实质上托管服务的方式都是一致的。例如在控制台应用程序中:

using (ServiceHost host = new ServiceHost(typeof(DocumentsExplorerService)))
{
host.Open();

Console.WriteLine("The Service had been launched.");
Console.Read();
}


由于ServiceHost实例是被创建在应用程序域中,因此我们必须保证宿主进程在调用服务期间不会被关闭,因此我们利用Console.Read()来阻塞进程,以使得控制台应用程序能够一直运行,直到认为地关闭应用程序。如果是Windows应用程序,则可以将创建ServiceHost实例的代码放在主窗体的相关代码中,保证服务宿主不会被关闭。

相应地,我们需要配置应用程序的app.config配置文件:

<configuration>
<system.serviceModel>
<services>
<service name=

"BruceZhang.WCF.DocumentsExplorerServiceImplementation.DocumentsExplorerService"

behaviorConfiguration="DocumentExplorerServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8008/DocumentExplorerService"/>
</baseAddresses>
</host>          
<endpoint
address=""
binding="basicHttpBinding"
bindingConfiguration="DocumentExplorerServiceBinding"
contract="BruceZhang.WCF.DocumentsExplorerServiceContract.IDocumentsExplorerService"/>

<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>       
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="DocumentExplorerServiceBinding" sendTimeout="00:10:00"

transferMode="Streamed"
messageEncoding="Text" textEncoding="utf-8"

maxReceivedMessageSize="9223372036854775807">          
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="DocumentExplorerServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>          
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>


注意,配置文件中的服务名必须包含服务契约以及服务类的命名空间。此外,在配置文件中我通过标签为服务添加了基地址,因此在endpoint中,address为""。

此时,调用服务的客户端配置文件也应与服务的配置保持一致:

<configuration>
<system.serviceModel>
<client>
<endpoint        
address="http://localhost:8008/DocumentExplorerService"
binding="basicHttpBinding"
bindingConfiguration="DocumentExplorerServiceBinding"
contract="IDocumentsExplorerService"/>
</client>
<bindings>
<basicHttpBinding>
<binding name="DocumentExplorerServiceBinding" sendTimeout="00:10:00"

transferMode="Streamed"
messageEncoding="Text" textEncoding="utf-8"

maxReceivedMessageSize="9223372036854775807">
</binding>
</basicHttpBinding>
</bindings>
</system.serviceModel>
</configuration>


注意,两个配置文件中的服务地址都是一样的,对于绑定的配置也基本一致。

在通常的企业应用中,我们很少会采用自宿主方式托管服务,这是因为这种方式必须要在应用程序运行下,客户端才能够调用服务,且并不便于随时启动和停止服务。除了不具有易用性与易管理性之外,在可靠性、性能等诸多方面受到很多限制。但由于它简单、易于实现,因而往往用于开发期间的调试或演示环境。

自托管宿主支持所有的绑定。

2、Windows Services宿主

Windows Services宿主则完全克服了自托管宿主的缺点,它便于管理者方便地启动或停止服务,且在服务出现故障之后,能够重新启动服务。我们还可以通过Service Control Manager(服务控制管理器),将服务设置为自动启动方式,省去了服务的管理工作。此外,Windows Services自身还提供了一定的安全性以及检测机制和日志机制。

Windows Services宿主的实现也非常简单。我们可以在Visual Studio中创建Windows Services项目。在创建项目之后,就可以创建一个继承了System.ServiceProcess.ServiceBase类的Windows服务类。Windows服务类继承了ServiceBase类的OnStart()和OnStop()方法,完成Windows服务的启动与停止。我们可以重写这两个方法,将ServiceHost的启动与关闭对应地放入这两个方法的实现中。例如我们创建的DocumentsExplorerWindowsService类:

namespace BruceZhang.WCF.DocumentsExplorer
{
public partial class DocumentsExplorerWindowsService : ServiceBase
{
private ServiceHost m_serviceHost = null;

public static void Main()
{
ServiceBase.Run(new DocumentsExplorerWindowsService());
}

public DocumentsExplorerWindowsService()
{
InitializeComponent();

ServiceName = "DocumentsExplorerService";
}

protected override void OnStart(string[] args)
{
if (m_serviceHost != null)
{
m_serviceHost.Close();
}

m_serviceHost = new ServiceHost(typeof(DocumentsExplorerService));
m_serviceHost.Open();
}

protected override void OnStop()
{
if (m_serviceHost != null)
{
m_serviceHost.Close();
m_serviceHost = null;
}
}
}
}


共5页: 上一页 [1] 2 [3] [4] [5] 下一页
【内容导航】
华硕IDC绿色节能服务器推荐
WCF开发基础
Windows Home Server 家用服务器专题
服务器维护与应用基础
服务器基础知识入门
 
 验证码: (点击刷新验证码)   匿名发表
  • Visual C++ 完全自学宝典

  • 作者:强锋科技,朱洪波
  • Visual C++ 6.0是微软公司为程序人员提供的Visual Studio 6.0工具套件中的重要组成部分。本书由浅入深地介绍使用Visual C++ 6.0..
Copyright©2005-2008 51CTO.COM 版权所有