I'm trying to create s stand alone Web Server using System.Web.Hosting but I'm stuck in with an error that I can't find an answer.
I use this code to create the ApplicationHost
Private Function CreateApplicationHost(ByVal hostType As Type, _
ByVal virtualPath As String, ByVal physicalPath As String) As Object
If (Not physicalPath.EndsWith("\")) Then
physicalPath &= "\"
End If
Dim aspDir = System.Web.HttpRuntime.AspInstallDirectory
Dim domainId = DateTime.Now.ToString(Globalization.DateTimeFormatInfo.InvariantInfo).GetHashCode().ToString("x")
Dim appName = (virtualPath & physicalPath).GetHashCode().ToString("x")
Dim setup = New AppDomainSetup()
setup.ApplicationName = appName
setup.ConfigurationFile = physicalPath & "web.config"
Dim ad = AppDomain.CreateDomain(domainId, Nothing, setup)
ad.SetData(".appDomain", "*")
ad.SetData(".appPath", physicalPath)
ad.SetData(".appVPath", virtualPath)
ad.SetData(".domainId", domainId)
ad.SetData(".hostingVirtualPath", virtualPath)
ad.SetData(".hostingInstallDir", aspDir)
Debug.WriteLine(ad.BaseDirectory)
Return ad.CreateInstance(hostType.Module.Assembly.FullName, hostType.FullName).Unwrap
End Function
The function that process the reques is as follows:
Public Sub ProcessRequest(ByVal page As String, _
ByVal query As String, ByVal output As IO.TextWriter)
If (String.IsNullOrEmpty(page)) Then
Throw New ArgumentNullException("page")
End If
If (output Is Nothing) Then
Throw New ArgumentNullException("output")
End If
'*
'* Fix the parameters
'*
If page.StartsWith("/") Then page = page.Substring(1)
If query.StartsWith("?") Then query = query.Substring(1)
'*
'* Process the default request
'*
Try
Dim swr = New System.Web.Hosting.SimpleWorkerRequest(page, query, output)
System.Web.HttpRuntime.ProcessRequest(swr)
Catch ex As Exception
My.Application.Log.WriteException(ex, TraceEventType.Warning, ex.StackTrace)
End Try
End Sub
I get the error below when I try to access any file hosted, and I don't find any reason why this shouldn't work.
Server Error in '/' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. |
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.Compilation.CompilationLock..cctor() +48
[TypeInitializationException: The type initializer for 'System.Web.Compilation.CompilationLock' threw an exception.]
System.Web.Compilation.CompilationLock.GetLock(Boolean& gotLock) +0
System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled() +144
[HttpException (0x80004005): The type initializer for 'System.Web.Compilation.CompilationLock' threw an exception.]
System.Web.Compilation.BuildManager.ReportTopLevelCompilationException() +81
System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled() +525
System.Web.Compilation.BuildManager.GetGlobalAsaxTypeInternal() +25
System.Web.Compilation.BuildManager.GetGlobalAsaxType() +28
System.Web.HttpApplicationFactory.CompileApplication() +29
System.Web.HttpApplicationFactory.Init() +128
System.Web.HttpApplicationFactory.EnsureInited() +81
System.Web.HttpApplicationFactory.GetApplicationInstance(HttpContext context) +100
System.Web.HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr) +353
|
Version Information: Microsoft .NET Framework Version:2.0.50727.1433; ASP.NET Version:2.0.50727.1433