7

Is there a way to catch the HTTP 500 error returned by an .asmx web service from a .Net client?

Using .Net 4.5 (VS2015), the .Net client code consumes the .asmx web service and calls it using the code below:

var client = new WebserviceApi.MyServiceSoapClient();

var response = client.MyWebServiceMethod();

If the .asmx web service returns an HTTP 500 error with a SOAP message containing the error message details, the "response" variable is set to null.

Using Fiddler, the traffic shows the HTTP 500 response from the .asmx web service. The response contains a SOAP XML message with details on the error.

No exception is thrown or caught in the .Net client code, execution continues as normal.

This means there is no information for the client to pick up regarding the nature of the exception. All the client code can do is check if "response" is null, but the exception message is not available to the client code.

Is there any way to force the .Net client code to throw an exception if the .asmx web service returns an HTTP 500 response so that the error message can be checked/logged?

Lenny
  • 333
  • 5
  • 19
  • Can't you catch the Exception using `try cath` statement? – anmarti Oct 28 '15 at 11:20
  • No exception is thrown, execution continues as normal with a null "response" variable. – Lenny Oct 28 '15 at 11:36
  • So, how do you know you get a 500? – anmarti Oct 28 '15 at 11:46
  • Using Fiddler, monitoring the traffic from the client to server, the response is HTTP 500, and the response body contains SOAP XML with details on the cause of the error. – Lenny Oct 28 '15 at 11:49
  • maybe this could help you http://stackoverflow.com/questions/589762/how-to-check-errors-from-web-services – jLaw Oct 28 '15 at 12:16
  • @jLaw, thanks for the tip, the link may be able to help, it uses raw xml rather than the SOAP client proxy generated by Visual Studio. Hopefully there is a way to catch the HTTP 500 using the standard soap client, will use this as a fallback in case there is no other solution. Thx. – Lenny Oct 28 '15 at 12:30
  • and this one too http://stackoverflow.com/questions/1153710/how-to-return-errors-from-an-asmx-web-service?rq=1 – jLaw Oct 28 '15 at 12:40
  • Can you show the code in the `WebserviceApi.MyServiceSoapClient` class? Sounds like the function call is trapping the soap error and then returning null – Andrew Bonsall May 05 '16 at 00:09
  • This question is already half-year old. Did you find the answers satisfying? If yes, please consider marking one of them as an actual answer to your question. – Jakub Szumiato Nov 21 '16 at 08:54

3 Answers3

5

I had a similar problem for axis (java) web service. They were throwing exceptions that didn't appear on my side even though the response was really a HTTP 500.

I can't say for sure this will solve your case, but I solved mine overriding the GetWebResponse method and throwing the exception by myself when needed.

I did it by changing the web service client code generated by Visual Studio after adding Web Reference (generated file name is: Reference.cs, sometimes it's not visible in solution, you have to click 'Show All files' on top of the solution pane and then unfold your web service reference files.

internal class ChangedWebServiceClient : SomeSoapService
{

    protected override WebResponse GetWebResponse(WebRequest request)
    {
        var response = base.GetWebResponse(request);

        if (response != null)

        {
            var responseField = response.GetType().GetField("_base", BindingFlags.Instance | BindingFlags.NonPublic);
            if (responseField != null)
            {
                var webResp = responseField.GetValue(response) as HttpWebResponse;
                if (webResp != null)
                {
                    if (webResp.StatusCode.Equals(HttpStatusCode.InternalServerError))
                        throw new WebException(
                            "HTTP 500 - Internal Server Error happened here. Or any other message that fits here well :)");
                }
            }
        }

        return response;
    }
}
Jakub Szumiato
  • 1,318
  • 1
  • 14
  • 19
3

After researching this a bit, it appears that all errors thrown from ASMX are in the form of the SoapException class, so a try catch with that class should be enough to handle that error.

Further reading on ASMX exception handling

SoapException Class

nivlam also propsed a good solution to reading raw request data from ASMX and SOAP over on this answer and you could probably get error codes from there.

Community
  • 1
  • 1
Jordan LaPrise
  • 1,088
  • 1
  • 11
  • 20
  • unfortunately in this case an exception is not thrown, would be nice though. – Lenny Apr 29 '16 at 20:09
  • @Lenny try the I added a second section at the bottom, it has a link to an answer by nivlam, use his method to read the raw output, and determine the error you are getting – Jordan LaPrise Apr 29 '16 at 21:04
0

I had a similar problem some years ago. The problem with the HTTP 500 status code is that the service call doesn't reach the server. So you can't debug it. The solution that worked for me was making sure the parameters had the exact same schema as that of the web methods defined. Any mismatch in the data contracts may cause the HTTP 500 Internal Server Error.

Chaos Legion
  • 2,730
  • 1
  • 15
  • 14