使用System.Web.Hosting来托管ASP.Net应用程序中的页面

时间:2011-08-25 21:39:20

标签: .net asp.net iis hosting

我想知道是否可以在当前运行的Web应用程序中的目录中托管页面。我希望这个功能,以便我可以动态添加页面/程序集,而无需回收应用程序池。我认为有可能这样做,我想我越来越接近,但是当我尝试加载页面时,我一直收到服务器错误:路径'/ App_GlobalResources /'映射到此应用程序外的目录,这不是支撑。

我正在将所有当前程序集和其他几个程序集加载到新的appDomain中,然后尝试处理httpWorkerRequest。 我知道,我提供的代码很差,但我只是想让这个东西呼吸。

Public Class ApplicationHostHelper
    Private _appDomain As AppDomain
    Public Function ConstructNewAppDomain() As AppDomain
        Dim testDirectory As String = "<appDirectory>\TestDomains\TestDomain"

        Dim args As New AppDomainSetup()
        args.ApplicationBase = testDirectory
        args.ConfigurationFile = "Web.Config"


        Dim assemblyName As String
        Dim assemblyNames As New List(Of String)()
        For Each ass As System.Reflection.Assembly In AppDomain.CurrentDomain.GetAssemblies()
            assemblyName = String.Empty
            If Not ass.GetType().Namespace.Equals("System.Reflection.Emit", System.StringComparison.InvariantCultureIgnoreCase) Then assemblyName = System.IO.Path.GetFileName(ass.Location)
            If Not IsSystemDependencyName(assemblyName) AndAlso Not assemblyName = String.Empty Then
                If Not System.IO.File.Exists(testDirectory & "\" & assemblyName) Then
                    System.IO.File.Copy(ass.Location, testDirectory.TrimEnd("\"c) & "\" & assemblyName, True)
                    If Not assemblyNames.Contains(assemblyName) Then assemblyNames.Add(assemblyName)
                End If

            End If
        Next
        Dim splitPath() As String = System.Reflection.Assembly.GetExecutingAssembly.CodeBase.Replace("file:///", "").Split("/"c)
        Dim basePath As String = Nothing
        For x As Int32 = 0 To splitPath.Length - 3
            basePath += splitPath(x) & "\"
        Next
        basePath &= "Web.config"
        Dim newWebConfigPath As String = testDirectory.TrimEnd("\"c) & "\" & "Web.config"
        If System.IO.File.Exists(basePath) AndAlso Not System.IO.File.Exists(newWebConfigPath) Then System.IO.File.Copy(basePath, newWebConfigPath)

        Dim retval As AppDomain = AppDomain.CreateDomain("CustomPagesDomain", Nothing, args)
        retval.SetData(".appId", "CustomPagesDomain")
        retval.SetData(".appDomain", "*")
        retval.SetData(".appPath", testDirectory)
        retval.SetData(".appVPath", "/")
        retval.SetData(".domainId", "CustomPagesDomain")
        retval.SetData(".hostingVirtualPath", "/")
        retval.SetData(".hostingInstallDir", testDirectory)
        _appDomain = retval
        Return retval
    End Function

    Public Function LoadPage(ByVal textWriter As System.IO.TextWriter) As AppDomain
        Dim myHost As CustomPagesHost = CType(CreateApplicationHost(), CustomPagesHost)
        myHost.ProcessRequest("StatsReport.aspx", textWriter)
        Return _appDomain
    End Function

    Public Function CreateApplicationHost() As Object
        Dim tempAppDomain As AppDomain = ConstructNewAppDomain()
        Dim oh As System.Runtime.Remoting.ObjectHandle = tempAppDomain.CreateInstance(GetType(CustomPagesHost).Module.Assembly.FullName, GetType(CustomPagesHost).FullName)
        Return oh.Unwrap()
    End Function


    Public Shared Function IsSystemDependencyName(ByVal potentialSystemAssemblyName As String) As Boolean
        If potentialSystemAssemblyName.ToLower = "system" Then Return True
        If potentialSystemAssemblyName.ToLower = "mscorlib" Then Return True
        If potentialSystemAssemblyName.Split("."c)(0).ToLower = "system" Then Return True
        If potentialSystemAssemblyName.Split("."c)(0).ToLower = "microsoft" Then Return True
        Return False
    End Function

End Class

Public Class CustomPagesHost
    Inherits MarshalByRefObject
    Public Sub ProcessRequest(ByVal page As String, ByVal textWriter As System.IO.TextWriter)

        Dim hwr As HttpWorkerRequest = New System.Web.Hosting.SimpleWorkerRequest(page, Nothing, textWriter)
        HttpRuntime.ProcessRequest(hwr)
    End Sub
End Class


Private Sub _loadAppDomain_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles _loadAppDomain.Click
Dim cHelper As New ApplicationHostHelper()
        Dim testDomain As AppDomain = Nothing
        Dim textWriter As System.IO.TextWriter
        Dim ms As New System.IO.MemoryStream()
        textWriter = New System.IO.StreamWriter(ms)
        Try

            testDomain = cHelper.LoadPage(textWriter)
            textWriter.Flush()
            ms.Position = 0
            _status.Text = New System.IO.StreamReader(ms).ReadToEnd()

        Catch ex As Exception
            _status.Text = ex.ToString()
        Finally
            If Not testDomain Is Nothing Then
                AppDomain.Unload(testDomain)
                DumpFiles("<appDirectory>\TestDomains\TestDomain\")
            End If

        End Try
End Sub

我真的很感激有关这个主题的任何帮助。我似乎无法取得进展。

谢谢!

0 个答案:

没有答案