简体   繁体   中英

.Net code to catch an HTTP 500 error response from an .asmx web service

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?

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;
    }
}

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM