Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning HttpStatus codes with message from WCF Rest service that IParameterInspector AfterCall can handle

So then, I've got a number of methods in a a couple of services that I've decorated with a custom attribute.

That attribute uses the ApplyDispatchBehavior from IOperationBehavior to attach to the method and the BeforeCall and AfterCall methods from IParameterInspector to do some processing before and after the service method is called.

All is good, well was good... The problem comes when the the services method needs to return a http status along with the a message. The only way I could find to do that was for the method to throw a WebFaultException<T> passing in our message and the appropriate http status.

The problem with this is the attached attribute's AfterCall is never called.

The long and short of it is:

  • Is there a way of returning a message and setting the http status without throwing an exception?
  • If I do have to throw a WebFaultException<T> is there a way my attribute can still get called with the returned message?
like image 246
Wilfred Knievel Avatar asked Dec 05 '25 07:12

Wilfred Knievel


1 Answers

Ok after a lot of searching I found this question and this answer

So to summarise: a call to a little method like:

public void SetResponseHttpStatus(HttpStatusCode statusCode)
{
    var context = WebOperationContext.Current;
    context.OutgoingResponse.StatusCode = statusCode;
}

...before each return that isn't a plain 200 response should do it.

like image 92
Wilfred Knievel Avatar answered Dec 07 '25 21:12

Wilfred Knievel