Solved

How does Global.asax pick up exceptions from a C# web services method?

Posted on 2008-06-26
13
2,920 Views
Last Modified: 2012-05-05
I have a simple class (you can see it below) but it is a web services method and it does a division by zero and throws an exception.

I have a Global.asax that should get the error message?
How does Global.asax know to catch the error message? it isn't working for me. I put a breakpoint in my method

INSIDE Global.asax

protected virtual void Application_Error(Object sender, EventArgs e)
{
    BREAK POINT HERE but it never gets hit.
}
public class Birthday : System.Web.Services.WebService

    {
 

        [WebMethod]

        public int divideByZERO(int pInt, int pIntZero)

        {

            try

            {

                return pInt / pIntZero;

            }

            catch (Exception ex)

            {

                

                //Console.WriteLine("EXCEPTION");

                throw ex;

            }

            return 42;

        }

}

Open in new window

0
Comment
Question by:alexmac05
13 Comments
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 21878785
Are you using the Global.asax for the service, or the Global.asax for the consumer of the service?
0
 
LVL 33

Assisted Solution

by:raterus
raterus earned 500 total points
ID: 21878795
Try making your declaration in Global.asax look like this


    void Application_Error(object sender, EventArgs e) 

    { 

        // Code that runs when an unhandled error occurs
 

    }

Open in new window

0
 

Author Comment

by:alexmac05
ID: 21879103
It is for the service.

also, changing the function by removing protected and void didn't work.

Thanks,
alex
0
 
LVL 33

Assisted Solution

by:raterus
raterus earned 500 total points
ID: 21879114
Post the entire code of Global.asax, and tell me where it's located in your application?  Have you tried putting code in other events and verified they work?
0
 

Author Comment

by:alexmac05
ID: 21879238
I am a newbie to this. I'm used to coding but not .net web apps.

I haven't tried any other events.

What should I be googling to read about this? Just Global.asax events?

I have uploaded a picture of where it is located in the project
namespace BirthDayWS

{

    public class Global : System.Web.HttpApplication

    {
 

        protected void Application_Start(object sender, EventArgs e)

        {
 

        }
 

        protected void Application_End(object sender, EventArgs e)

        {
 

        }
 

        void Application_Error(Object sender, EventArgs e)

        {

            string errMsg, userText, pageData, versionInfo, eSource = "", eDetails = "", eStack = "";

            Exception outerE = null, innerE = null, testE;
 

            AssemblyName currentAss;
 

            try

            {

                outerE = Server.GetLastError();
 

                HttpException httpException = outerE as HttpException;
 

                if (httpException != null)

                {

                    if (httpException.GetHttpCode() == 500)

                    {

                        

                    }
 

                    Exception innerException = httpException.InnerException;
 

                    if (innerException != null)

                    {

                        if (innerException.Message == "Maximum request length exceeded.")

                            return;

                    }
 
 

                }

                
 

            }

            catch (Exception ie)

            {

                eSource = "<b>BABY JESUS</b>" + ie.Message + "</b>";
 
 
 

            }

            try

            {

                currentAss = Assembly.GetExecutingAssembly().GetName();

                versionInfo = "<b>Version Information:</b> " + currentAss.Name + " Version: " + currentAss.Version.ToString();
 

            }

            catch (Exception ie)

            {

                versionInfo = ie.Message + " Hello alex";

            }
 
 

        }

    }

}

Open in new window

LocationOfGlobalASAXFile.bmp
0
 
LVL 6

Expert Comment

by:cottsak
ID: 21880718
put a breakpoint on the 1st brace "{" in the Application_Error function.
then get rid of the try/catch in the WebMethod... it may be that the exception was 'caught' then re-thrown thats bypassing the global handler.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 33

Assisted Solution

by:raterus
raterus earned 500 total points
ID: 21883118
Why did you add a Namespace (BirthDayWS) to this Global class?  I think this needs to be removed, because you are probably pushing this class into a namespace that asp.net doesn't know how to find and thus throw your events into.
0
 

Author Comment

by:alexmac05
ID: 21884205
THe namespace was created by default when I created the file. It has to be there, it is the namespace of the application.

Console.WriteLine("Application_Start was hit"); that I will see these lines if the callbacks are fired?



 protected void Application_Start(object sender, EventArgs e)

        {

                Console.WriteLine("Application_Start was hit");

        }
 

        protected void Application_End(object sender, EventArgs e)

        {

                    Console.WriteLine("Application_Start was hit");

        }

Open in new window

0
 
LVL 33

Accepted Solution

by:
raterus earned 500 total points
ID: 21884278
No, there is no ASP.Net console to view.  I'd output to a file, that's pretty much guaranteed.  Just remember to write to a file you have permissions to (like right next to the global.asax)!

        StreamWriter output = new StreamWriter("c:\\my\\www\\root\\log.txt",true);
        output.WriteLine("Doing something at: " & DateTime.Now.ToString());
        output.Close();

0
 

Author Comment

by:alexmac05
ID: 21886212
There are two files associated with this file.

1. Global.asax

2. Global.asax.cs

My question now is where should I write to a file? Global.asax.cs?

Also, I wonder if the Global.asax needs more code in it? I only see one line in it:

<%@ Application Codebehind="Global.asax.cs" Inherits="BirthDayWS.Global" Language="C#" %>

but in searching the net some files seem to look like the attached bit map

IsThisWhatMineShouldLookLIke.jpg
0
 

Author Closing Comment

by:alexmac05
ID: 31471164
Thanks a lot for your help.
0
 

Author Comment

by:alexmac05
ID: 21886430
Problem: How do you get callbacks to fire in the Global.aspx.cs code behind file in VS2005 if theyre not firing automatically?

Answer:

So, as we know, the Global.aspx file is two files

1.      Global.aspx  the script
2.      Global.aspx.cs  the code behind file

Using Visual Studio 2005, one must manually create this file.  

Then one must go to the script part and add the callbacks manually.
<%@ Application Codebehind="Global.asax.cs" Inherits="BirthDayWS.Global" Language="C#" %>

<script RunAt="server">
   
    void Application_Start(Object sender, EventArgs e)
    {
        //Code that runs on applicaton startup
       
    }


    void Application_End(Object sender, EventArgs e)
    {

    }

    void Application_Error(Object sender, EventArgs e)
    {

    }

    void Session_End(Object sender, EventArgs e)
    {

    }
   
   
</script>


If the Global.aspx file has these callbacks just as I have listed them here (BLANK) then this causes the callbacks to fire such that inside the Global.asax.cs file you can code whatever you want or set breakpoints.

Yeah.


Cheers,
Alex



 
0
 

Expert Comment

by:Metafuse
ID: 22674119
The Application_OnError in the Global.asax works for ASP.NET but does not work for web services.   The reason for this is that the HTTP handler for XML Web services consumes any exception that occurs while an XML Web service is executing and turns it into a SOAP fault prior to the Application_Error event is called.

To create global exception handling for your web services you have to build a SOAP extension. The SOAP extension can check for the existence of an exception in the ProcessMessage method and will handle the global error handling in a similar way that the ASP.NET Application_OnError does.

Obviously

public class SoapExceptionHandler : System.Web.Services.Protocols.SoapExtension 

{ 

      public override void ProcessMessage(System.Web.Services.Protocols.SoapMessage message) 

     { 

         if (message.Stage == SoapMessageStage.AfterSerialize) 

         { 

             if (message.Exception != null) 

             { 

                   //log your error here

             } 

        } 

    } 

    public override object GetInitializer(Type serviceType) 

    { 

        return null; 

    } 

     public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute) 

    { 

        return null; 

    } 

    public override void Initialize(object initializer)

    { 

    } 

}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Export import database 4 39
Turn on intranet settings 1 35
Angular - break the dataset in chunks? 7 38
Error on link 14 36
New Relic: Our company recently started researching several products to figure out what were the best ways for us to increase our web page speed and to quickly identify performance problems that we may be having. One of the products we evaluated wa…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now