Extracting Detailed Exception information from a WebException


S

sprash

I'm a newbie trying to solve the following problem

Background
----------
I'm writing a generic tool which will test WebMethods of any given
WebService. This implies that I cannot bind to any WebService URI at
design-time.

Problem:
--------
- Say an exception (ex DivideByZeroException) occurs in a WebMethod
and the WebMethod throws that exception
- The consuming/client app catches a WebException
- The WebException has NO information of the TYPE of exception thrown
in the WebMethod and simply has "The remote server returned an error:
(500) Internal Server Error." message

Notes
------
1. When binding through Visual Studio at design time, I catch the
proper exception (typically SOAPException with all details.
2. Both System.Net.HttpWebRequest & System.Net.WebClient demonstrate
the problem


SAMPLE WEBSERVICE / WEBMETHOD [SERVER]
--------------------------------------
[WebMethod]
public int Add(int num1, int num2)
{
throw new DivideByZeroException("You divided by Zero!");
}


SAMPLE WEBSERVICE CLIENT
------------------------
try
{
System.Net.WebClient request = new WebClient();
request.Headers.Add("Content-Type: text/xml");
request.Headers.Add("SOAPAction:http://tempuri.org/Add");

byte[] byteArray = Encoding.UTF8.GetBytes(@"<soap:Envelope
xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
xmlns:xsd=""http://www.w3.org/2001/XMLSchema""
xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""> <soap:Body>
<Add xmlns=""http://tempuri.org/""> <num1>34</num1>
<num2>45</num2></Add></soap:Body></soap:Envelope>");

byte[] responseArray =
request.UploadData("http://localhost/Adder/adder.asmx","POST",byteArray);

Response.Write(Encoding.ASCII.GetString(responseArray));
}
catch(WebException ex)
{
Response.Write(ex.Message + "\n" + ex.StackTrace);
}
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top