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

可以从大多数 Web 服务器部署 Windows Presentation Foundation (WPF) 应用程序,前提是这些服务器配置了相应的多用途 Internet 邮件扩展 (MIME) 类型。 默认情况下,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/x-ms-application
.xbap application/x-ms-xbap
.deploy application/octet-stream
.xps application/vnd.ms-xpsdocument

注意

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

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

' 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