.Net to catch HTTP 500 response from .asmx web service

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

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

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 data, the response variable is set to null.

Using Fiddler, traffic shows an HTTP 500 response from the .asmx web service. The response contains an XML SOAP message with detailed error information.

No exception is thrown or gets into the client code .Net, execution continues as usual.

This means that the client cannot obtain information about the nature of the exception. All client code can check if the "response" is null, but the exception message is not available for the client code.

Is there a way to make the .Net client code throw an exception if the .asmx web service returns an HTTP 500 response so that I can check / log the error message?

+5
source share
3 answers

I had a similar problem for axis web service (java). They threw exceptions that did not appear on my side, although the response was really HTTP 500.

I cannot say for sure that this will solve your case, but I decided that I would override the GetWebResponse method and throw an exception on my own when necessary.

I did this by changing the web service client code generated by Visual Studio after adding the web link (the generated file name: Reference.cs, sometimes it does not appear in the solution, you need to click "Show all files" on top of the solution panel, and then expand Web service help 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; } } 
+4
source

After examining this a bit, it seems that all errors thrown from ASMX are presented in the form of the SoapException class, so to resolve it, it should be enough to catch an attempt with this class.

Further reading of ASMX exception help

Class SoapException

nivlam also provided a good solution for reading raw request data from ASMX and SOAP on top of this answer , and you could get error codes from there.

+2
source

I had a similar problem a few years ago. The problem with the HTTP 500 status code is that the service call does not reach the server. Therefore, you cannot debug it. The solution that worked for me was to make sure that the parameters have the exact same scheme as the described web methods . Any inconsistency in the data contracts may result in an internal HTTP 500 server error.

0
source

Source: https://habr.com/ru/post/1234689/


All Articles