Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用System.Web.Hosting在ASP.Net应用程序中托管页面_.net_Asp.net_Iis_Hosting - Fatal编程技术网

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

使用System.Web.Hosting在ASP.Net应用程序中托管页面,.net,asp.net,iis,hosting,.net,Asp.net,Iis,Hosting,我想知道是否有可能在当前运行的web应用程序的目录中托管页面。我想要这个功能,这样我就可以动态添加页面/程序集,而无需回收应用程序池。我认为这样做是可能的,而且我想我已经接近了,但是当我尝试加载页面时,我一直收到一个服务器错误:路径“/App\u GlobalResources/”映射到该应用程序之外的目录,这是不受支持的 我正在将所有当前程序集和几个其他程序集加载到新的appDomain中,然后尝试处理httpWorkerRequest。 我知道,我提供的代码很差,但我只是想让这东西呼吸一下

我想知道是否有可能在当前运行的web应用程序的目录中托管页面。我想要这个功能,这样我就可以动态添加页面/程序集,而无需回收应用程序池。我认为这样做是可能的,而且我想我已经接近了,但是当我尝试加载页面时,我一直收到一个服务器错误:路径“/App\u 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
公共类ApplicationHostHelper
私有\u appDomain作为appDomain
公共函数ConstructNewAppDomain()作为AppDomain
Dim testDirectory As String=“\TestDomains\TestDomain”
作为新AppDomainSetup()的Dim参数
args.ApplicationBase=testDirectory
args.ConfigurationFile=“Web.Config”
将程序集名称设置为字符串
将程序集名称作为新列表(字符串)()
对于AppDomain.CurrentDomain.GetAssemblys()中的每个ass作为System.Reflection.Assembly
assemblyName=String.Empty
如果不是ass.GetType().Namespace.Equals(“System.Reflection.Emit”、System.StringComparison.InvariantCultureIgnoreCase),则assemblyName=System.IO.Path.GetFileName(ass.Location)
如果不是IsSystemDependencyName(assemblyName),也不是assemblyName=String.Empty,则
如果System.IO.File.Exists(testDirectory&“\”&assemblyName)不存在,则
System.IO.File.Copy(ass.Location,testDirectory.TrimEnd(“\”c)&“\”assemblyName,True)
如果不是assemblyNames.Contains(assemblyName),则是assemblyNames.Add(assemblyName)
如果结束
如果结束
下一个
Dim splitPath()格式为字符串=System.Reflection.Assembly.GetExecutionGassembly.CodeBase.Replace(“文件:///”,“”)。拆分(“/”c)
Dim basePath作为字符串=无
对于x,作为Int32=0到splitPath.Length-3
基本路径+=拆分路径(x)和“\”
下一个
basePath&=“Web.config”
Dim newWebConfigPath As String=testDirectory.TrimEnd(“\”c)&“\”和“Web.config”
如果System.IO.File.Exists(basePath)和System.IO.File.Exists(newWebConfigPath)都不存在,则System.IO.File.Copy(basePath,newWebConfigPath)
Dim retval为AppDomain=AppDomain.CreateDomain(“CustomPagesDomain”,Nothing,args)
retval.SetData(“.appId”,“CustomPagesDomain”)
retval.SetData(“.appDomain”和“*”)
SetData(“.appPath”,testDirectory)
retval.SetData(“.appVPath”和“/”)
retval.SetData(“.domainId”、“CustomPagesDomain”)
retval.SetData(“.hostingVirtualPath”,“/”)
retval.SetData(“.hostingInstallDir”,testDirectory)
_appDomain=retval
返回返回
端函数
作为AppDomain的公共函数LoadPage(ByVal textWriter作为System.IO.textWriter)
将myHost设置为CustomPagesHost=CType(CreateApplicationHost(),CustomPagesHost)
ProcessRequest(“StatsReport.aspx”,textWriter)
返回appDomain
端函数
公共函数CreateApplicationHost()作为对象
Dim tempAppDomain As AppDomain=ConstructNewAppDomain()
作为System.Runtime.Remoting.ObjectHandle=tempAppDomain.CreateInstance(GetType(CustomPagesHost).Module.Assembly.FullName,GetType(CustomPagesHost).FullName)
返回oh.Unwrap()
端函数
公共共享函数IsSystemDependencyName(ByVal potentialSystemAssemblyName为字符串)为布尔值
如果potentialSystemAssemblyName.ToLower=“system”,则返回True
如果potentialSystemAssemblyName.ToLower=“mscorlib”,则返回True
如果potentialsystemassemblemblyName.Split(“.”c)(0)。ToLower=“system”则返回True
如果potentialSystemAssemblyName.Split(“.c)(0).ToLower=“microsoft”,则返回True
返回错误
端函数
末级
公共类CustomPagesHost
继承MarshalByRefObject
公共子进程请求(ByVal页面作为字符串,ByVal textWriter作为System.IO.textWriter)
Dim hwr As HttpWorkerRequest=New System.Web.Hosting.SimpleWorkerRequest(页面,无,textWriter)
HttpRuntime.ProcessRequest(hwr)
端接头
末级
私有子\u loadAppDomain\u Click(ByVal sender作为对象,ByVal e作为System.EventArgs)处理\u loadAppDomain.Click
Dim cHelper作为新的应用程序HostHelper()
将testDomain设置为AppDomain=Nothing
将textWriter设置为System.IO.textWriter
将ms作为新系统进行Dim.IO.MemoryStream()
textWriter=新系统.IO.StreamWriter(毫秒)
尝试
testDomain=cHelper.LoadPage(textWriter)
textWriter.Flush()
女士职位=0
_status.Text=New System.IO.StreamReader(ms.ReadToEnd)()
特例
_status.Text=ex.ToString()
最后
如果不是testDomain,那么它什么都不是
卸载(testDomain)
转储文件(“\TestDomains\TestDomain\”)
如果结束
结束尝试
端接头
我非常感谢在这个话题上的任何帮助。我似乎再也不能进步了


谢谢

您试图解决/避免的问题到底是什么?你为什么不想回收应用程序池?我支持Ek0nomik:没有什么理由避免应用程序池回收。这似乎让事情变得比他们需要的复杂得多be@Justin,您试图做的事情要复杂得多-ASP.NET宿主代码涉及很多,除非有明确的公共API(我不知道),否则您可能不想走这条路。应用程序池回收对您有何影响?例如,如果您想避免由于进程内会话状态的丢失,那么您可以考虑将其转换为