views:

99

answers:

4

As stated in the title my web application builds successfully, although every time I run it in debug mode I get the following .Net error:

img

If I hit refresh then the application gets no more errors until I next start it up again, any ideas?

Here is my global.asax file:

<%@ Application Language="C#" Inherits="MyCompany.Web.MyApp.Shell.CustomWebClientApplication" %>

<script runat="server">

    void Session_End(Object Sender, EventArgs e)
    {
        FormsAuthentication.SignOut();
    }

    protected void Session_Start(Object sender, EventArgs e)
    {
        if (Session.IsNewSession)
        {
            Response.Redirect(System.Web.Configuration.WebConfigurationManager.AppSettings["HomePage"]);
        }
    }

    protected void Application_Error(Object sender, EventArgs e)
    {
        System.Exception oops = Server.GetLastError();

        //Injection attack error handling
        if (oops.GetBaseException() is System.Web.HttpRequestValidationException)
        {
            Response.Redirect("ErrorPage.aspx");
        }
    }

</script>
A: 

Sounds like an initialization issue. You're probably trying to use a resource before it's initialized, but by the time you refresh it's had time to be initialized.

ho1
I've edited my question with the global.asax file
moorcroft
+2  A: 

You have something which is trying to access a variable which is set to null (or hasn't been initialized). Do you have anything in the Global.asax or anything that fires on the start of the application? Do you have any asynchronous operations that fire on the start of the application?

Check on your Home.aspx page to see what is happening there. It looks like your application redirects to that page, so I would guess that on either the init or page_load event there is something which is executing that it causing the problem.

Kevin
I've edited my question with the global.asax file
moorcroft
A: 

Since the error is being raised during the Application_Start event the error only shows when the web application is hit the first time and the Application_Start event fires.

Once the start up has run all subsequent requests do not cause the Application_Start to execute since the application has already started event though an exception was thrown.

So you need identify what in the Application_Start cycle is causing the exception. Looking at the stack trace it seems like it might be related to the configuration of the composite modules?

Chris Taylor
I've edited my question with the global.asax file
moorcroft
+1  A: 
System.Exception oops

I think that this is source of problems. When there is no object returned from

Server.GetLastError();

then you will get NullReferenceException on line

oops.GetBaseException()

It makes perfect sense. On first run, oops is null (because no error occured before), thus throwing NullReferenceException. On second page refresh, GetLastError() returns object reffering previous errror (NullReferenceException) and page is displayed. Always check objects for null before accessing them.

Anyway, you can always try catching all runtime exceptions (Debug->Exceptions->Common Language runtime) and see where a problem is.

Tomas Voracek
Nope this wasn't the problem. I commented out everything within Application_Error then put a breakpoint on Application_Error. When I first run the application it automatically hits this breakpoint then when I step further into it, the error occurs within the following method:protected override void DefaultProcessBrowsers(bool ignoreApplicationBrowsers, System.Collections.Specialized.NameValueCollection headers, System.Web.HttpBrowserCapabilities browserCaps) {this.DefaultrefbrowseridAProcess(headers, browserCaps);if (ignoreApplicationBrowsers) {}...}
moorcroft