catching request on exception in WCF - c#

I have few webservices in my solution, they use NLOG for logging into the database, I want to catch the Request whenever there is an Exception , currently all the request and response calls are being logged into the database and I want to change it log only the Requests upon exception ..I did some research but did not find any proper answer, if you could give me some example than it could be verymuch helpful

I think you should try Application_Error method in Global.asax. It can catch any unhandled exceptions of application.
Documentation here: http://msdn.microsoft.com/ru-ru/library/24395wz3%28v=vs.100%29.aspx

Related

Http 500 in IIS Log when i throw a c# exception

Is there any possibility to throw an exception in the code of a web application which would not cause error 500 in the IIS logs?
500 means internal server error, i.e. your application have failed.
I'm guessing that you are asking for some sort of indication when the client have done something wrong.
For MVC 5.x there is an HttpException which can be used to return other error codes. (500 is returned for all exceptions but HttpException)
However! Exceptions are application failures. Your application does not fail just because the client sent something invalid.
Instead, you should send back a HttpResponse with the correct HTTP code. For MVC applications you can use the HttpStatusCodeResult in the controller.

Why does a WCF IErrorHandler behave weird when an unhandled exception occurs in a endpoint dispacher's message inspector?

I have a WCF service that handles errors by implementing an IErrorHandler and attaching it to the ChannelDispatcher's ErrorHandlers.
The IErrorHandler has a method called
bool HandleError(Exception error);
Which I implement and simply logs the exception to my DB.
My problem is that some exceptions occur before the web service's method gets called.
I also have a MessageInspector that I attach to the EndpointDispatcher that logs the soap messages.
Now, assuming I have an sql error and get an unhandled exception on my MessageInspector, the HandleError method gets called twice with the same sql connection error exception.
The problem is that it happens once when the connection's status is opened and once when the connection's status is closed.
Also, the second method on the IErrorHandler:
void ProvideFault(Exception error, MessageVersion version, ref Message fault);
doesn't get called, and then the framework wraps my exception with a FaultException, without letting me format it.
Is there any reason why the IErrorHandler behaves that way? Is there any other module I need to implement in order to handle unhandled exceptions that occur as part of the request's pipeline?
Why does the IErrorHandler get called when the connection is Closed?
We had a similar implementation of MessageInspector and wanted to know how long each request took. Consequently we did logging for both the incoming and outgoing messages. Perhaps this could be the case for your issue?
I'm not sure that the ProvideFault method is called when inside an extension behavior, however I was able to get it to fire from within the constructor of my restful WCF service. I think the 'workaround' of putting a try/catch in the inspector is a good approach.
Below are a couple of articles with implementation details on this topic:
IErrorHandler returning wrong message body when HTTP status code is 401 Unauthorized
How can I create custom XML namespace attributes when consuming a legacy SOAP service?
Also here is an article on how to do something similar in WebApi:
http://www.asp.net/web-api/overview/error-handling/web-api-global-error-handling

Sending the exception message to the client if a web api action method errors

I have a try catch in my controller action method and if there is an error I would like to send the exception.message to the client using the proper method that implements IActionResult. How would I go about doing this?
You really only want to be catching exceptions in your try/catch that you can handle gracefully in the code. If a internal server error does occur, a 500 would be the appropriate response.
You should be validating the requests in order to prevent the 500 and when validation fails, then you can return the appropriate status code.
A very generic example might look like this in Web Api 2:
if (validationFailed)
{
return BadRequest("Your request failed because xyz..");
}

IdentityServer3 - Exception handling

Is there a way to handle exceptions ourselves. We have custom user service code inside our identityserver project and would like to know without diving through logs when errors occur.
We have a centralized error logger for all our apps. I'd imagine something similar needed if using 3rd party exception lib (airbrake etc).
Is there something possible at the moment or is this ticket solving my problem
https://github.com/IdentityServer/IdentityServer3/issues/2012
If you care about the error logging - the event service will log all unhandled exceptions.
The issue you mentioned would allow you to have more control over direct error handling (e.g. setting a custom http response) - but that's backlog right now.

Default exception handling in web service

In my application I am calling a web service.
try
{
theWebService.Method(parameter);
}
catch (Exception e)
{
//This catches and logs a SoapException
}
When I do this the a SoapException is caught and I'm only given a one line sentence on the issue. I'm assuming the SoapException is just relaying information from the actual exception that occurred in the web service (please correct me if I'm wrong about this). Does the web service log the full exception somewhere and if so where? Note that no additional logging has been added to the service besides what comes by default.
I'll assume you're still using legacy ASMX web services, and not WCF.
SoapException does not just relay the exception from the service. It can also be used to return a SOAP Fault message back to the caller.
By default, an unhandled exception in an ASMX web service will invoke ASP.NET Health Monitoring. By default, this will enter an event in the event log with details about the exception.

Categories