开发者

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜