[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>
可用的宿主模式包括:
"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>
相关文章推荐
- WCF学习笔记一:WCF服务库和宿主
- WCF学习笔记(4)——宿主iis部署wcf
- WCF学习笔记之宿主在IIS中
- 12.lua学习笔记:环境
- UNIX环境编程学习笔记(12)——文件I/O之目录操作
- WCF基础学习笔记--多种Client访问宿主
- UNIX环境编程学习笔记(12)——文件I/O之目录操作
- UNIX环境高级编程学习笔记:9_12 孤儿进程 孤儿进程组
- Android开发学习笔记1---环境搭建
- 大数据学习笔记-(二)启动集群&配置IDE环境
- Python 学习笔记12
- MyBatis For .NET学习笔记[2]:配置环境
- cocos2d-x学习笔记01:VS开发环境搭建
- Deep Learning-TensorFlow (12) CNN卷积神经网络_ Network in Network 学习笔记
- PHP学习笔记12----学习制作PHP+MYSQL留言板(下)+完整留言板代码
- Redis的安装和环境的搭建并设置服务(Redis学习笔记一)
- Cocos2d-x 3.x学习笔记:猩先生带你打飞机(一)环境与创建项目
- WCF学习笔记3-地址(Address)
- 黑马程序员——Objective-C程序设计(第4版)学习笔记之02-Objective-C 编程环境概述——黑马 IOS 技术博客
- Cesium 学习笔记 下载和环境配置