当我在 VS2005 下编译我的项目时,以下代码帮助我配置我的 IIS 以设置 ApplicationPool 并成功地将我的 WebApplication 分配给它(使用 InstallerClass)。
现在我在VS2010下编译时遇到了麻烦!当我运行我的安装设置时,我现在变成了一个 DropDownList 来为站点选择一个应用程序池(我以前没有这个)并且在列表中没有我正在创建的 AppPool。
AppPool 将在安装后创建,我可以看到它,但我的应用程序没有绑定到它。
有人知道我错过了什么吗?非常感谢您的帮助。问候,胺
PS:代码:
Private Const _appPoolName As String = "MyPool001"
Public Overrides Sub Install(ByVal stateSaver As System.Collections.IDictionary)
MyBase.Install(stateSaver)
SetIISApplicationPool(_appPoolName, Me.Context)
End Sub
Public Shared Sub SetIISApplicationPool(ByVal AppPoolName As String, ByVal context As System.Configuration.Install.InstallContext)
Dim targetSite As String = context.Parameters("targetsite")
Dim targetVDir As String = context.Parameters("targetvdir")
Dim targetDirectory As String = context.Parameters("targetdir")
If targetSite Is DBNull.Value Then
Throw New System.Configuration.Install.InstallException("IIS Site Name Not Specified!")
End If
If targetSite.StartsWith("/LM/") Then
'// Sets the virtual directory to .NET 2.0
targetSite = targetSite.Substring(4)
Dim info As New ProcessStartInfo
info.FileName = IO.Path.Combine(System.Environment.GetEnvironmentVariable("SystemRoot"), "Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe")
info.Arguments = String.Format("-s {0}/ROOT/{1}", targetSite, targetVDir)
info.CreateNoWindow = True
info.UseShellExecute = False
Process.Start(info)
Try
CreateApplicationPool("IIS://localhost/W3SVC/AppPools", AppPoolName)
Catch : End Try
Try
AssignVDirToAppPool("IIS://localhost/" & targetSite & "/Root/" & targetVDir, AppPoolName)
Catch : End Try
End If
End Sub
Private Shared Sub CreateApplicationPool(ByVal strMetabasePath As String, ByVal strAppPoolName As String)
Try
If strMetabasePath.EndsWith("/W3SVC/AppPools") Then
Dim blExists As Boolean = AppPoolExists(strMetabasePath, strAppPoolName)
If blExists = False Then
Dim objNewPool As System.DirectoryServices.DirectoryEntry
Dim objAppPools As New System.DirectoryServices.DirectoryEntry(strMetabasePath)
objNewPool = objAppPools.Children.Add(strAppPoolName, "IIsApplicationPool")
objNewPool.CommitChanges()
End If
Else
Throw New Exception("Application pools can only be created in the */W3SVC/AppPools node.")
End If
Catch exError As Exception
Throw New Exception("Failed in CreateAppPool with the following exception: " & exError.Message)
End Try
End Sub
Private Shared Sub AssignVDirToAppPool(ByVal strMetabasePath As String, ByVal strAppPoolName As String)
Try
Dim objVdir As New System.DirectoryServices.DirectoryEntry(strMetabasePath)
Dim strClassName As String = objVdir.SchemaClassName.ToString()
If strClassName.EndsWith("VirtualDir") Then
Dim objParam As Object() = {0, strAppPoolName, True}
objVdir.Invoke("AppCreate3", objParam)
objVdir.Properties("AppIsolated")(0) = "2"
Else
Throw New Exception("Only virtual directories can be assigned to application pools")
End If
Catch exError As Exception
Throw New Exception("Failed in AssignVDirToAppPool with the following exception: " & exError.Message)
End Try
End Sub
Private Shared Function AppPoolExists(ByVal strMetabasePath As String, ByVal strAppPoolName As String) As Boolean
strMetabasePath += "/" & strAppPoolName
Dim blExists As Boolean = False
If System.DirectoryServices.DirectoryEntry.Exists(strMetabasePath) = True Then
blExists = True
End If
Return blExists
End Function