Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 152643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:39:37+00:00 2026-05-11T09:39:37+00:00

I’m using the [System.Web.Script.Services.ScriptService] tag to use web services callable from client side javascript.

  • 0

I’m using the [System.Web.Script.Services.ScriptService] tag to use web services callable from client side javascript. What I need is a way of globally logging any unhandled exceptions in those methods. On the client side, I get the error callback and can proceed from there, but I need a server-side catch to log the exception.

The guy at this url: http://ayende.com/Blog/archive/2008/01/06/ASP.Net-Ajax-Error-Handling-and-WTF.aspx

suggests that this can’t be done.

Is that accurate? Do I seriously have to go to every single webmethod in the entire system and try/catch the method as a whole.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T09:39:38+00:00Added an answer on May 11, 2026 at 9:39 am

    You can use an HTTP module to capture the exception message, stack trace and exception type that is thrown by the web service method.

    First some background…

    • If a web service method throws an exception the HTTP response has a status code of 500.

    • If custom errors are off then the web service will return the exception message and stack trace to the client as JSON. For example:
      {'Message':'Exception message','StackTrace':' at WebApplication.HelloService.HelloWorld() in C:\Projects\Stackoverflow Examples\WebApplication\WebApplication\HelloService.asmx.cs:line 22','ExceptionType':'System.ApplicationException'}

    • When custom errors are on then the web service returns a default message to the client and removes the stack trace and exception type:
      {'Message':'There was an error processing the request.','StackTrace':'','ExceptionType':''}

    So what we need to do is set custom errors off for the web service and plug in an HTTP module that:

    1. Checks if the request is for a web service method
    2. Checks if an exception was thrown – that is, a status code of 500 is being returned
    3. If 1) and 2) are true then get the original JSON that would be sent to the client and replace it with the default JSON

    The code below is an example of an HTTP module that does this:

    using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Web;  public class ErrorHandlerModule : IHttpModule {    public void Init(HttpApplication context) {     context.PostRequestHandlerExecute += OnPostRequestHandlerExecute;     context.EndRequest += OnEndRequest;   }    static void OnPostRequestHandlerExecute(object sender, EventArgs e) {     HttpApplication context = (HttpApplication) sender;     // TODO: Update with the correct check for your application     if (context.Request.Path.StartsWith('/HelloService.asmx')          && context.Response.StatusCode == 500) {       context.Response.Filter =          new ErrorHandlerFilter(context.Response.Filter);       context.EndRequest += OnEndRequest;     }   }    static void OnEndRequest(object sender, EventArgs e) {     HttpApplication context = (HttpApplication) sender;     ErrorHandlerFilter errorHandlerFilter =        context.Response.Filter as ErrorHandlerFilter;     if (errorHandlerFilter == null) {       return;     }      string originalContent =       Encoding.UTF8.GetString(         errorHandlerFilter.OriginalBytesWritten.ToArray());      // If customErrors are Off then originalContent will contain JSON with     // the original exception message, stack trace and exception type.      // TODO: log the exception   }    public void Dispose() { } }

    This module uses the following filter to override the content sent to the client and to store the original bytes (which contain the exception message, stack trace and exception type):

    public class ErrorHandlerFilter : Stream {    private readonly Stream _responseFilter;    public List OriginalBytesWritten { get; private set; }    private const string Content =      '{\'Message\':\'There was an error processing the request.\'' +     ',\'StackTrace\':\'\',\'ExceptionType\':\'\'}';    public ErrorHandlerFilter(Stream responseFilter) {     _responseFilter = responseFilter;     OriginalBytesWritten = new List();   }    public override void Flush() {     byte[] bytes = Encoding.UTF8.GetBytes(Content);     _responseFilter.Write(bytes, 0, bytes.Length);     _responseFilter.Flush();   }    public override long Seek(long offset, SeekOrigin origin) {     return _responseFilter.Seek(offset, origin);   }    public override void SetLength(long value) {     _responseFilter.SetLength(value);   }    public override int Read(byte[] buffer, int offset, int count) {     return _responseFilter.Read(buffer, offset, count);   }    public override void Write(byte[] buffer, int offset, int count) {     for (int i = offset; i < offset + count; i++) {       OriginalBytesWritten.Add(buffer[i]);     }   }    public override bool CanRead {     get { return _responseFilter.CanRead; }   }    public override bool CanSeek {     get { return _responseFilter.CanSeek; }   }    public override bool CanWrite {     get { return _responseFilter.CanWrite; }   }    public override long Length {     get { return _responseFilter.Length; }   }    public override long Position {     get { return _responseFilter.Position; }     set { _responseFilter.Position = value; }   } }

    This method requires custom errors to be switched off for the web services. You would probably want to keep custom errors on for the rest of the application so the web services should be placed in a sub directory. Custom errors can be switched off in that directory only using a web.config that overrides the parent setting.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 91k
  • Answers 91k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The jQuery docs are actually a really good place to… May 11, 2026 at 6:20 pm
  • Editorial Team
    Editorial Team added an answer The solution for escaping the brackets characters is to prefix… May 11, 2026 at 6:20 pm
  • Editorial Team
    Editorial Team added an answer Try something like: window[foo + bar] = "whatever"; alert(varName); do… May 11, 2026 at 6:20 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.