WCF REST Services - General Exception Handling

I have a lot of legacy code, which is now the backend for the WCF REST service - previously this was the regular WCF service backend, if that matters. I want to implement a mechanism that would catch any exception in any method and analyze it. If this proves to be a known bug, it will be processed and turned into a friendly visual bug.

I know that instead of β€œregular” exceptions, I can use FaultException or WebProtocolException , but there are many places where exceptions are excluded from the code, and finding all of them is a rather painful option.

I tried to add an endpoint behavior extension that creates a new behavior that overrides the standard WebHttpBehavior.AddServerErrorHandlers method and adds my error handlers ( IErrorHandler implementations) to the endpoint manager error handler collector. Inside the error handlers, I analyze the exception and create (or not create) the desired error based on this exception.

I expected this mechanism to return user data for any known exception, but I made a mistake. The good old Microsoft has implemented the wonderful inevitable WebHttpBehavior2 , which unconditionally adds an internal Microsoft.ServiceModel.Web.WebErrorHandler to the end of the collection of endpoint manager error handlers. This handler ignores all previously handled handlers and recognizes only a small set of exceptions, while most are interpreted as "Internal Server Error" and nothing more.

The question is whether I am on the right track, and is there a way to disable this handler in the WCF REST mechanism or introduce it with a new Exception (for example, when an exception is caught, it is first handled by my handlers and if they throw / return, for example, FaultException, then this new exception is provided instead of Microsoft.ServiceModel.Web.WebErrorHandler instead of the original). If all my experiments with IErrorHandler and behavior extensions are useless, what is the alternative? Again, I really do not want to change the logic of throwing exceptions, I want the exceptions to be detected in one place and processed.

Thank you so much!

+8
rest wcf
source share
2 answers

When changing WCF SOAP services to REST, the whole set of error messages and change processing is.

In SOAP, errors are part of your contract. In REST, they simply become the codes that you output in the code and description of the HTTP response.

Here is a catch snippet:

 catch (Exception e) { Trace.WriteLine(e.ToString()); OutgoingWebResponseContext response = WebOperationContext.Current.OutgoingResponse; response.StatusCode = System.Net.HttpStatusCode.UnsupportedMediaType; // or anything you want response.StatusDescription = e.Message; return null; // I was returning a class } 

So, I suggest you create a helper code that creates the appropriate error codes for you and inserts the answer.

+7
source share

This is what I have done in the past.

  public class MyServerBehavior : IServiceBehavior { public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) { } public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) { foreach (ChannelDispatcher chDisp in serviceHostBase.ChannelDispatchers) { chDisp.IncludeExceptionDetailInFaults = true; if (chDisp.ErrorHandlers.Count > 0) { // Remove the System.ServiceModel.Web errorHandler chDisp.ErrorHandlers.Remove(chDisp.ErrorHandlers[0]); } // Add new custom error handler chDisp.ErrorHandlers.Add(new MyErrorHandler()); } } public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) { } } 

MyErrorHandler was my class that implemented IErrorHandler.

+2
source share

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


All Articles