WCF宿主模式主要概念總結
WCF開發工具的應用在一定程度上給開發人員帶來了非常大的好處。在這里我們將會針對WCF宿主模式的相關概念進行一個詳細的介紹。WCF宿主模式比較靈活,我們可以依據服務的使用目的從多種宿主中選擇一個最適合的。#t#
可用的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 的一部分。這應該是所有WCF宿主模式中最適合企業級部署應用的。除了 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" %>
以上就是對WCF宿主模式的相關介紹。