如何:配置 IIS 5.0 和 IIS 6.0 以部署 WPF 应用程序

只要使用适当的多用途 Internet 邮件扩展(MIME)类型配置 Windows Presentation Foundation(WPF)应用程序,就可以从大多数 Web 服务器部署应用程序。 默认情况下,Microsoft Internet Information Services (IIS) 7.0 配置了这些 MIME 类型,但 Microsoft Internet Information Services (IIS) 5.0 和 Microsoft Internet Information Services (IIS) 6.0 则没有。

本主题介绍如何配置 Microsoft Internet Information Services (IIS) 5.0 和 Microsoft Internet Information Services (IIS) 6.0 以部署 WPF 应用程序。

说明

可以在注册表中检查 UserAgent 字符串,以确定系统是否安装了 .NET Framework。 有关详细信息以及一个脚本,用于检查 UserAgent 字符串并确定系统上是否安装了 .NET Framework,请参阅 检测系统是否安装 .NET Framework 3.0

调整内容过期设置

应将内容过期设置调整为 1 分钟。 以下过程概述了如何使用 IIS 执行此操作。

  1. 单击 “开始”菜单,指向 管理工具,然后单击 Internet Information Services(IIS) 管理器。 还可以使用“%SystemRoot%\system32\inetsrv\iis.msc”从命令行启动此应用程序。

  2. 展开 IIS 树,直到找到 默认网站 节点。

  3. 右键单击 默认网站 并从上下文菜单中选择 属性

  4. 选择 HTTP 标头 选项卡,然后单击“启用内容过期功能”。

  5. 将内容设置为在 1 分钟后过期。

注册 MIME 类型和文件扩展名

必须注册多个 MIME 类型和文件扩展名,以便客户端系统上的浏览器可以加载正确的处理程序。 需要添加以下类型:

扩展名 MIME 类型
.manifest application/manifest
.xaml application/xaml+xml
.application application/x-ms-application
.xbap application/x-ms-xbap
.deploy application/octet-stream
.xps application/vnd.ms-xpsdocument

说明

无需在客户端系统上注册 MIME 类型或文件扩展名。 安装 Microsoft .NET Framework 时会自动注册它们。

以下Microsoft Visual Basic 脚本版本(VBScript)示例会自动将必要的 MIME 类型添加到 IIS。 若要使用该脚本,请将代码复制到服务器上的 .vbs 文件。 然后,从命令行运行文件或在 Microsoft Windows 资源管理器中双击文件以运行脚本。

' This script adds the necessary Windows Presentation Foundation MIME types
' to an IIS Server.
' To use this script, just double-click or execute it from a command line.
' Running this script multiple times results in multiple entries in the IIS MimeMap.

Dim MimeMapObj, MimeMapArray, MimeTypesToAddArray, WshShell, oExec
Const ADS_PROPERTY_UPDATE = 2

' Set the MIME types to be added
MimeTypesToAddArray = Array(".manifest", "application/manifest", ".xaml", _
    "application/xaml+xml", ".application", "application/x-ms-application", _
    ".deploy", "application/octet-stream", ".xbap", "application/x-ms-xbap", _
    ".xps", "application/vnd.ms-xpsdocument")

' Get the MimeMap object
Set MimeMapObj = GetObject("IIS://LocalHost/MimeMap")

' Call AddMimeType for every pair of extension/MIME type
For counter = 0 to UBound(MimeTypesToAddArray) Step 2
    AddMimeType MimeTypesToAddArray(counter), MimeTypesToAddArray(counter+1)
Next

' Create a Shell object
Set WshShell = CreateObject("WScript.Shell")

' Stop and Start the IIS Service
Set oExec = WshShell.Exec("net stop w3svc")
Do While oExec.Status = 0
    WScript.Sleep 100
Loop

Set oExec = WshShell.Exec("net start w3svc")
Do While oExec.Status = 0
    WScript.Sleep 100
Loop

Set oExec = Nothing

' Report status to user
WScript.Echo "Windows Presentation Foundation MIME types have been registered."

' AddMimeType Sub
Sub AddMimeType (Ext, MType)

    ' Get the mappings from the MimeMap property.
    MimeMapArray = MimeMapObj.GetEx("MimeMap")

    ' Add a new mapping.
    i = UBound(MimeMapArray) + 1
    ReDim Preserve MimeMapArray(i)
    Set MimeMapArray(i) = CreateObject("MimeMap")
    MimeMapArray(i).Extension = Ext
    MimeMapArray(i).MimeType = MType
    MimeMapObj.PutEx ADS_PROPERTY_UPDATE, "MimeMap", MimeMapArray
    MimeMapObj.SetInfo

End Sub

说明

多次运行此脚本会在 Microsoft Internet Information Services (IIS) 5.0 或 Microsoft Internet Information Services (IIS) 6.0 元数据库中创建多个 MIME 映射条目。

运行此脚本后,可能无法看到来自 Microsoft Internet Information Services (IIS) 5.0 或 Microsoft Internet Information Services (IIS) 6.0 Microsoft管理控制台(MMC)的其他 MIME 类型。 但是,这些 MIME 类型已添加到 Microsoft Internet Information Services (IIS) 5.0 或 Microsoft Internet Information Services (IIS) 6.0 元数据库。 以下脚本将显示 Microsoft Internet Information Services (IIS) 5.0 或 Microsoft Internet Information Services (IIS) 6.0 元数据库中的所有 MIME 类型。

' This script lists the MIME types for an IIS Server.
' To use this script, just double-click or execute it from a command line
' by calling cscript.exe

dim mimeMapEntry, allMimeMaps

' Get the MimeMap object.
Set mimeMapEntry = GetObject("IIS://localhost/MimeMap")
allMimeMaps = mimeMapEntry.GetEx("MimeMap")

' Display the mappings in the table.
For Each mimeMap In allMimeMaps
    WScript.Echo(mimeMap.MimeType & " (" & mimeMap.Extension + ")")
Next

将脚本另存为 .vbs 文件(例如,DiscoverIISMimeTypes.vbs),并使用以下命令从命令提示符运行该脚本:

cscript DiscoverIISMimeTypes.vbs