您的位置:首页 > 其它

[WCF 学习笔记] 12. 宿主环境

2009-01-17 15:14 323 查看
WCF 部署模式比较灵活,我们可以依据服务的使用目的从多种宿主中选择一个最适合的。

可用的宿主模式包括:

"Self-Hosting" in a Managed Application: 也就是 Console Application 或者 WinForm Application。我们在前面的章节都是使用这种模式进行演示。它的好处是简单、部署方便,但缺乏相关环境支持,不适合用于企业级服务部署。

Managed Windows Services: 可以随着操作系统自动启动,受服务权限限制,安全性要比上一种好些。

Internet Information Services (IIS):
和 Web Services 的部署方式类似,由请求消息来激活服务,还可以使用 IIS 提供的 Process recycling、Idle
shutdown、Process health monitoring 等功能。缺点是只能使用 Http Binding。

Windows Process Activation Service (WAS):
这个宿主只有 Windows Vista 和 Microsoft Windows Server(Longhorn) 才提供,它是 IIS7
的一部分。这应该是所有宿主中最适合企业级部署应用的。除了 IIS 所提供的那些功能外,最关键的是它支持几乎所有的通讯协议。

"Managed Application" 和 "Windows Services" 部署方式非常类似,本文不再详述。以下介绍一下 IIS 部署的步骤。

1. 安装完 VS Extension 后,我们可以创建一个 WCF service 的网站项目。
2. 添加一个 WCF service 新项,系统自动会创建 Service.svc、App_Code/Service.cs 等必要文件。
3. 在 Service.cs 文件中完成服务编码。
4. 添加 web.config 文件,并在其位置单击鼠标右键,打开 "Microsoft Service Configuration Editor" 工具完成服务配置。
5. 注意添加 serviceMetadata,否则我们使用浏览器无法查看,也无法创建客户端代理。

web.config 演示 (注意配置文件中并没有提供服务地址)

<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<system.serviceModel>
<services>
<service behaviorConfiguration="MyServiceTypeBehaviors" name="MyService">
<endpoint binding="wsHttpBinding" contract="IMyService"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<system.web>
<compilation debug="true">
</system.web>
</configuration>

service.svc

<%@ ServiceHost Language="C#" Debug="true" Service="MyService" CodeBehind="~/App_Code/service.cs" %>

运行项目,在浏览器中你会看到相应的信息。(图片内容和上面例子有所不同,仅做演示。)









我个人建议你将服务放到一个单独的 Library 中,这样更改宿主环境会更方便一点,不过上述步骤要做些修改。

1. 安装完 VS Extension 后,我们可以创建一个 WCF service 的网站项目。
2. 添加服务所在类库的引用。
3. 创建 svc 文件,内容也所不同。如 <%@ ServiceHost Debug="true" Service="Learn.Library.WCF.CalculateService" %>,注意使用包含名字空间的全名。
4. 添加 web.config 文件,打开 "Microsoft Service Configuration Editor" 工具完成服务配置。
5. 注意添加 serviceMetadata,否则我们使用浏览器无法查看,也无法创建客户端代理。

web.config

<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<system.serviceModel>
<services>
<service behaviorConfiguration="MyServiceTypeBehaviors" name="Learn.Library.WCF.CalculateService">
<endpoint binding="wsHttpBinding" contract="Learn.Library.WCF.ICalculate"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<system.web>
<compilation debug="true">
</system.web>
</configuration>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: