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

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" %>


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

uploads/200704/07_223817_wcfiis1.gif

 

uploads/200704/07_223821_wcfiis2.gif



我个人建议你将服务放到一个单独的 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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值