I am using the approach below to handle 404 errors on my sites. This has worked for a long time but suddenly within the last month I am getting a "Handle is not initialized" exception with a number of sites on our dedicated server (some still work, and on development machine it works). Anyone have any thoughts?
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<httpErrors existingResponse="Replace">
<remove statusCode="500" subStatusCode="-1"/>
<remove statusCode="404" subStatusCode="-1"/>
<error statusCode="404" prefixLanguageFilePath="" path="/default.aspx" responseMode="ExecuteURL"/>
<error statusCode="500" prefixLanguageFilePath="" path="/error.aspx" responseMode="ExecuteURL"/>
</httpErrors>
</system.webServer>
On default.aspx page:
protected void Page_PreRender(object sender, EventArgs e)
{
if (!Page.IsPostBack && Request.Url.ToString().Contains("?404;"))
{
HttpContext.Current.RewritePath("~/");
Page.Header.Controls.AddAt(0, new LiteralControl("<base href='" + Request.Url.Scheme + "://" + Request.Url.Authority + "'/>"));
Response.StatusCode = 404;
Util.DisplayAlert("The page you are looking for no longer exists. If you navigated to this page by clicking a link within this site please <a href='/contact.aspx'>contact us</a> to let us know.");
}
}
Exception details:
Exception information:
Exception type: InvalidOperationException
Exception message: Handle is not initialized.
at System.Runtime.InteropServices.GCHandle.FromIntPtr(IntPtr value)
at System.Web.Hosting.PipelineRuntime.GetManagedPrincipalHandler(IntPtr pRootedObjects)
at System.Web.Hosting.UnsafeIISMethods.MgdGetPrincipal(IntPtr pHandler, IntPtr& pToken, IntPtr& ppAuthType, Int32& pcchAuthType, IntPtr& ppUserName, Int32& pcchUserName, IntPtr& pManagedPrincipal)
at System.Web.Hosting.IIS7WorkerRequest.GetUserPrincipal()
at System.Web.Hosting.IIS7WorkerRequest.SynchronizeVariables(HttpContext context)
at System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context)
I am having the same problem but haven't found a solution yet. I have confirmed that the problem is caused by setting the StatusCode or Status properties on the response, it affects IIS 7 and IIS Express but not Cassini which makes sense given the stack trace.
I'll keep investigating.
Edit: No luck in finding a solution. I posted the same question on the IIS forums: http://forums.iis.net/p/1187959/2016914.aspx#2016914
Edit 2: Confirmed as fixed in .NET 4.5 RC.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With