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

WCF宿主与服务托管(1)

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

【引自张逸的博客】WCF宿主与服务托管

若要公开WCF服务,需要提供一个运行服务的宿主环境。就像.NET CLR需要创建宿主环境以托管代码一般,WCF的宿主环境同样运行在进程的应用程序域中。在应用程序域中可以创建一个或多个ServiceHost实例,其关系如图一所示:

图1:托管ServiceHost
 
WCF并不推荐在应用程序域中创建多个ServiceHost实例。如果要托管多个服务,完全可以在一个宿主中通过多个Endpoint公开多个WCF服务。由于应用程序域对安全进行了隔离,如果需要提供不同的安全上下文,则有必要创建多个ServiceHost实例。

WCF的典型宿主包括以下四种:

1、"Self-Hosting" in a Managed Application(自托管宿主)
2、Managed Windows Services(Windows Services宿主)
3、Internet Information Services(IIS宿主)
4、Windows Process Activation Service(WAS宿主)

以下将通过一个具体的实例分别介绍这几种宿主的托管方式及其相关的注意事项。在这样的一个实例中,我们定义了如下的服务契约:

namespace BruceZhang.WCF.DocumentsExplorerServiceContract
{
[ServiceContract]
public interface IDocumentsExplorerService
{
[OperationContract]
[FaultContract(typeof(DirectoryNotFoundException))]
DocumentList FetchDocuments(string homeDir);

[OperationContract]
Stream TransferDocument(Document document);       
}   
}


服务的实现则如下所示:

namespace BruceZhang.WCF.DocumentsExplorerServiceImplementation
{
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public class DocumentsExplorerService : IDocumentsExplorerService
{
#region IDocumentsExplorerService Members

public DocumentList FetchDocuments(string homeDir)
{
//implementation code
}       

public Stream TransferDocument(Document document)
{
//implementation code
}

#endregion    
}
}


在服务契约的操作中,DocumentList与Document则为自己定义的数据契约:

namespace BruceZhang.WCF.DocumentsExplorerDataContract
{  
[DataContract]
public class Document
{
//DataMembers
}
}
namespace BruceZhang.WCF.DocumentsExplorerDataContract
{
[KnownType(typeof(Document))]   
[CollectionDataContract]
public class DocumentList:IList
{
//IList Methods
}
}


注意以上定义的服务契约、服务类与数据契约的命名空间。


共5页: 1 [2] [3] [4] [5] 下一页
【内容导航】
华山论剑——五款主流服务器OS评测
服务器虚拟机备份实战
DNS服务器安装优化全攻略
华硕IDC绿色节能服务器推荐
WCF开发基础
 
 验证码: (点击刷新验证码)   匿名发表
  • 亮剑.NET. 图解C#开发实战

  • 作者:李新峰 付志涛 缪勇
  • 本书采用全新的图解思路,分3篇介绍使用微软C#语言开发实际应用程序的基本知识。第1篇包括10章,介绍了C#语言的基础知识,主要..
Copyright©2005-2009 51CTO.COM 版权所有