Suppression of WebException - c#

Is there a way to suppress the occurrence of WebException when the http status code is 500 in C # WebClient? WebException is created by StackTrace, which slows down the system. This is especially serious if WebExceptions occur frequently.

There are many http clients, one of which is WebClient. WebClient is designed to throw exceptions. There is no way to have it not throw exceptions, as much as people would like.
Its predecessor, HttpWebRequest/HttpWebResponse, also does this.
If you don't want to catch the exception, you'll need to use another client.
HttpClient doesn't throw exceptions based on the returned status code, so that's an option.
You could use Flurl as an alternative which uses HttpClient under the hood. Add the error handling method .AllowHttpStatus("500").

Related

Any C# library to do webrequest similar to httpwebrequest but not throwing exceptions

I 'm looking for a similar library or class like httpwebrequest which does not throw exceptions on http codes. (c# language)
Problem with httpwebrequest is that it behaves the http error codes (like 401,403 and ...) as exceptions and throws an exception in case of these. You may know that exception throwing and handling is expensive. for example a successful request takes about 30 ms on my system,but another request which causes exception takes about 250!
I 'm making lots of request that causes this http error codes and it affects my code performance alot.
I have tried webclient,webrequest,httpclient and all are the same in this.
Any opinion on a alternative library or how to avoid these exceptions ?
HttpClient.GetAsync() won't throw an exception on a 400-level error. Though it will throw an exception for "an underlying issue such as network connectivity, DNS failure, server certificate validation or timeout."
Also, the HttpReponseMessage it produces has a handy IsSuccessStatusCode property.
Check out RestSharp. It's a great tool for creating web requests

Exception not thrown in HttpRequest.GetResponse

Below is C# code snippet to get Http Web response from Internet URI source.
It will throw exception when the client is not able to get response.
I am curious to know whether there is any condition where no exception
is thrown but HttpStatusCode is not OK (i.e. not is range 200-299).
Basically I need to know what are the bad status code where exception is not thrown.
try{
HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
catch{}
Any status code in the 4xx range is defined as a client error and will result in a ProtocolException. (400-Bad Request, 401-Unauthorized, etc..)
Any result in the 5xx range is defined as a server error and will result in CommunicationException. (500-Internal Server Error, 502-Bad Gateway, etc..)
This is useful for example if you want to implement a retry mechanism. It would make sense to retry if the exception is a CommunicationException since this may be a transient error (connect failure, DNS resolution failure), but if the exception is ProtocolException then retrying is redundant as it should always fail.
Whether any other status code is considered an error case or not depends on the contract between the client and the server but the above are the ones defined by the HTTP standard
List of HTTP status codes

HttpRequestException vs WebException

This is a general question that I'm confused about. I thought once a REST request was made, an error would come back via a WebException. In one case I have I'm getting a HttpRequestException, which doesn't allow me to get the HTTP status code.
I'm new to this stuff, but what is the difference between these? Why are there two types? When does one get used as opposed to another?
WebException seems to work well. HttpRequestException seems like a very weak version of it, where it knows the status code (in it's message) but it won't tell me explicitly what it was.
EDIT:
I'm using a HttpClient. Specifically calling client.GetStreamAsync().
There are three distinct failure scenarios:
a) You could not connect to the server or proxy, in which case a HttpRequestException is thrown. Be aware if your server is down and you are running fiddler, you will never see this exception, you will get a 5XX status code.
b) When reading/writing a network stream, there is some kind of interruption you will get an IOException.
c) You will get back a response with a HttpStatusCode with a 4XX/5XX status code. If your client application chooses to call response.EnsureSuccessStatusCode() then a HttpRequestException will be thrown.
If you decide to call EnsureSuccessStatusCode you are making an explicit decision that you don't care about status codes other than the fact that it was success/fail.
If you really need to bubble up an exception and then later handle the status code then I suggest you create your own extension method to replace EnsureSuccessStatusCode and create your own exception that can store the status code. Or preferably, translate the status code into one of a few different exceptions based on the corrective action you wish to take.
WebException Class: The exception that is thrown when an error occurs while accessing the network through a pluggable protocol.
HttpRequestException Class: A base class for exceptions thrown by the HttpClient and HttpMessageHandler classes.
I think the inner exception of a HttpRequestException could be a WebException however I'm not sure it ever is.
Note, a 404, 302 or whatever response other than a 200 (OK) is not an exception. Those responses are perfectly valid HTTP responses.
https://github.com/dotnet/runtime/pull/32455 adds StatusCode to HttpRequestException in .NET 5.

Best practice to handle WCF service errors?

I am coding some kind of a WCF service. most exceptions are caught in the BL implementation and handled there. Each of my API's return type is a class (named - "result") containing error code, error message and success boolean.
When exceptions are handled, this class is updated accordingly and in the end is sent back to the client.
Some of the exceptions are off-course, unhandled. Currently, I am wrapping each of my BL calls from the service layer with a generic try-catch so I can catch every unhandled exception and create a generic "result" class with a generic failure message, error code and success=false.
Is it a good way to handle exceptions or should I let unhandled exception to be thrown by the service to the client?
You can assume that the client can't use the data from the exception so it won't benefit from the extra information contained in the exception.
Check out Exception Shielding.
This is a process where exceptions raised by the service, are mapped to fault contracts according to rules you specify in a configuration file. This saves a lot of donkey work with try/catch blocks.
Here is one post to help you out:
In general though - faults will fall into 3 categories:
1) Client error - the client has tried to do something not permissable, so it needs to know about it. E.g. Failed to set a mandatory field. - Return specific message explaining fault.
2) Business error that doesn't affect the client. An error that is considered normal operation, e.g. Payment Authorization check failure. Either hide from client completely, or return some message: "Error performing request: Please try again later..."
3) System error - Unexpected - not normal operation: Replace with generic message: "System Error: Call Support"
In all cases though, the key thing is you remove the stack trace, especially if it's a public facing service.
With shielding you would have 3 Fault Contracts covering the above scenarios, and set the text appropriately in the Shielding configuration.
Be advised, you generally want shielding turned off during development as it makes it a right pain to debug the system!
I differ with the others. I think that in the same way HTTP methods GET, POST, PUT, DELETE thereby support CRUD operations, HTTP response codes 200, 500, etc., support success/fail and this is, in my opinion, appropriate to make use of. A 500 result still has an HTTP response body, and such a body is fully readable (so long as IIS isn't spitting out HTML; you have control over this). Meanwhile, the XML protocol implementations as with Microsoft SOAP from WCF already wrap exceptions with a faulting protocol.
If you're going to throw exceptions, throw them. Just document them while doing so, so that the consumers can plan accordingly.
I think both approaches are viable.
I personally prefer not throwing exceptions over WCF, so that the client can easily distinguish between error in server-side processing and connectivity/protocol issue: in the first case the response will indicate the failure, and in the second case exception will be thrown.
Personally I wouldn't expose the unhandled exceptions and propagate them to the client. I would define those exceptions the client might be interested in and only propagate those. Exceptions not directly related to what the clients want to do (ArgumentException could set reason to "CustomerId cannot be more than 20 chars" etc.) I'd deal with in the service and only indicate that some sort of internal server error has occurred on the service side which broke the execution and meant that the operation the client tried to run failed to complete. This I would do because the client can't really take any action based on internal server errors. They can fix their inparams in the case of an ArgumentException being thrown by validating the parameters again and retry the operation.
Not sure if this is really what you're asking, but hope it gives you some ideas at least.
If you let unhandled exceptions out of your WCF service, this may have undesirable effects such as communication channel being in faulted state where in a sessionful scenario, client can no longer use the same client proxy instance but is forced to create a new one and start a new session. In general, I think it is good to have control over the errors that surface out of your WCF service and provide clients helpful information. Take a look at IErrorHandler.This interface gives you control over the SOAP fault generated, unhandled exceptions, and allows you to do extra tasks like logging, and lets you decide whether you want to keep the session or not in case of a sessionful binding. You add your custom error handler via WCF extensibility such as service, endpoint, contract, operation behaviors.
Note that IErrorHandler is called before sending a response message. So there is still a chance of an unhandled exception occurring down in the channel stack during serialization, encoding, etc.

Is it Ok to throw exceptions back to a client from my service?

I am writing a Java based service with WSDL for a .Net client to consume, and I thought that when I receive an invalid value from the client that I would throw an exception that my client could then catch and display in a message box or something to the user (the client is a desktop app).
I was wondering if it would be ok to use this approach or if there's a better way of doing it.
I would say "no". Error messages, etc., but I wouldn't serialize an exception. You have no idea who the client is or what language they're written in.
The service should handle the exception: catch it, log it, and create a sensible error message(s) and status codes. This is not the place for exceptions, in my opinion.
And when I say "sensible error messages", I don't mean anything resembling a stack trace. These are likely to be business clients, who should not be reading such things. A business-meaningful message is the ticket here, not a stack trace.
.NET in general deals in FaultExceptions (wrapped SOAP faults) in WCF. I would assume that if you throw a proper SOAP Fault, that WCF would wrap this up into a FaultException by the time the client consumes the response, so they can have a try catch FaultException statement. This would still allow other non .NET clients to consume the service without breaking standards..
Just an idea anyway...
What you should probably do is use SOAP Faults, since these should be supported for any clients. Be sure to set the extra descriptive fields too.
Conceptually this is fine, but I don't think you can literally throw a Java Exception over HTTP back to a .NET client.
You can use HTTP 500 to signal a server error; you should also be able to attach a meaningful message to the response that will help the .NET developers figure out how to use your service better. This may or may not include a serialized Java stack trace.
The first thing to tackle is preventing against exceptions being thrown by doing validation on data before it gets manipulated. IE
string func(string cat)
if(cat == null || cat.length == 0){
//set errorLabelText to "bad data"
return;
}
//else code
That being said only throw Exceptions in Exceptional cases.

Categories

Resources