Note to Self - Generating HTTP 500 Internal Server Error Custom Response #

Returning HTTP 500 internal server error as your web service / asp.net REST responses in case of failures may  sound like an odd idea but it has its merits; with handlers and load balancing environments where you want your intelligent routing devices to redirect request to a different server based on an HTTP header response, this approach can come in handy.

In order to return an HTTP 500 server error custom response, there are two simple ways to achieve it.

By throwing an HTTP exception

throw new System.Web.HttpException(500, "Internal Server Error");

 

Or by modifying the headers.

       HttpContext.Current.Response.Status = "500 Internal Server Error";

       HttpContext.Current.Response.AddHeader("Status Code", "500");

       HttpContext.Current.Response.End();

Looking at the response from the HTTP headers, they are almost identical. The only difference is that due to termination of response stream in the later case, the content length is 0.

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Accept-Language: en-us,en;q=0.5

Accept-Encoding: gzip,deflate

Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7

Keep-Alive: 300

Connection: keep-alive

Referer: http://localhost:17109/HTTP500Test/Service.asmx?op=ThrowHTTPException

Content-Type: application/x-www-form-urlencoded

Content-Length: 0

 

HTTP/1.x 500 Internal Server Error

Server: ASP.NET Development Server/9.0.0.0

Date: Fri, 30 May 2008 15:58:24 GMT

X-AspNet-Version: 2.0.50727

Cache-Control: private

Content-Type: text/plain; charset=utf-8

Content-Length: 152

Connection: Close

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Accept-Language: en-us,en;q=0.5

Accept-Encoding: gzip,deflate

Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7

Keep-Alive: 300

Connection: keep-alive

Referer: http://localhost:17109/HTTP500Test/Service.asmx?op=ResponseHeader500Error

Content-Type: application/x-www-form-urlencoded

Content-Length: 0

 

HTTP/1.x 500 Internal Server Error

Server: ASP.NET Development Server/9.0.0.0

Date: Fri, 30 May 2008 15:57:10 GMT

X-AspNet-Version: 2.0.50727

Cache-Control: private, max-age=0

Content-Length: 0

Connection: Close

 

The similar result can be achieved by throwing an ApplicationException or custom exception as well because for the runtime, it still means an internal server error.

The source for the web service project can be downloaded from here. HTTP500Test-Src.zip (2.79 KB)


5/30/2008 8:04:48 AM (Pacific Standard Time, UTC-08:00) #    Comments [0]  |  Trackback

 

Review for UCertify M70-526 C# Exam. TS Microsoft.NET Framework 2.0 Windows based Client Development.#

With the rapidly changing horizon of upcoming tools and technologies, technical certification is an organized way of studying and keeping oneself up-to-date with the latest and greatest features which software frameworks have to offer.  Even though hands on experience is an absolute necessity, usually when using a particular framework in everyday job, a developer only gets exposed to a smaller set of features pertaining to the task at hand. On the contrary, certification study provides a breadth first approach to the general underlying framework, its mechanics and features. Since it covers a much broader area, studying for a certification exam becomes a daunting task; luckily not for those equipped with tools such as uCertify’s certification study products which allow you to focus your study in the key areas and provide you with instant feedback and help during the preparation process.

uCertify’s 70-526 – C# Exam. TS Microsoft.NET Framework 2.0 Windows based client preparation tool assists you to get ready for the test in a well organized and effective way. It consists of three practice tests with 35 well diversified questions, flash cards, adaptive tests, a detailed study notes section and final exam with 40 questions. This product lets you evaluate your readiness level, provides you a test history to ensure the progress and also allow you to create a custom test, based on the areas you find yourself not so strong. The product is customizable and allow you to create your own entries for test preparation but I believe the sections such as How To’s  could have been expanded and made more effective.  A test can be conducted in both “test” and “learning mode”; detailed explanation and references are provided in the later mode for follow up and understanding. The preparation engine is also equipped with features such as bookmarking, tagging and rating the questions.

Having taken the tests in the classical way, by using Microsoft courses, MSDN and books for prep, I find uCertify’s product quite relevant and helpful. Looking back if I’d have to prepare for the future exams, I’d use this tool in association with recommended text to help gauge the learning.

For details and trial downloads, please visit UCertify's website.


5/28/2008 5:42:47 AM (Pacific Standard Time, UTC-08:00) #    Comments [0]  |  Trackback

 

My Sessions @ Upcoming SoCal Code Camp in San Diego - 28, 29th. June#
Southern California Rock & Roll Code Camp is being held on June 28th and 29th at University California San Diego Extension. I'll be presenting following three sessions at the code camp.

  • Aspect Oriented Programming in .NET, an Introduction with ASPECT.NET

    Aspect Oriented Programming (AOP) deals with factorization in code i.e. separation of common concerns, specifically cross-cutting concerns, as an advance in modularization. AOSD has been a popular trend in development for quite some time in other programming environments and IDE’s however it’s scope and exposure is limited among .NET developers. This session is focused on getting developers a deeper understanding of what AOP is all about and how to use it in their everyday development. Aspect.NET is the framework used for this presentation.

  • Collaborative Filtering 101 – An Introduction with SQL Server 2008 BI

    "We have recommendations for you!". How do movies, social networking, books and e-commerce websites make recommendations? What algorithms and techniques are used behind the scenes?. In this session we will discuss collaborative filtering. Collaborative Filtering (CF) is defined as profiling or classification of information based on specific entity relationships i.e. making automatic predictions (filtering) about the interests of a user by collecting likelihood information from many users (collaborating). The underlying assumption of CF approach is that those who agreed in the past tend to agree again in the future. For example, a collaborative filtering or recommendation system for music tastes could make predictions about which music a user should like given a partial list of that user's tastes (likes or dislikes).

  • Using ASP.NET MVC to build a blogging engine in 60 minutes or less.

    MVC is a framework methodology that divides an application's implementation into three component roles: models, views, and controllers. ASP.NET now has built-in support for MVC style development and this session is an introduction to using this technique for building a sample application, a blogging engine. This session will elaborate on differences between traditional ASP.NET post-back style development versus the routes and REST architecture based thinking around MVC.


Hope to see you there.

5/3/2008 5:17:08 PM (Pacific Standard Time, UTC-08:00) #    Comments [0]  |  Trackback

 

All content © 2008, Adnan Masood
About the Author
On this page
Calendar
<July 2008>
SunMonTueWedThuFriSat
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789
Archives
Sitemap
Blogroll OPML
microsoft