Question

I have a WCF Web Api Restful webservice.

I want every single service call to take an optional parameters: suppress_status_codes. Is there a way to do this wihtout adding the parameter and handling to every end point like:

[WebGet(UriTemplate = "{somearg1}/{somearg2}/?supress={suppressStatusCodes}")
public HttpResponseMessage<string> SomeEndPoint(string somearg1, long somearg2, bool suppressStatusCodes)
{    
     // handle suppress status codes
     // do rest of call

The purpose of suppress_status_code is for Flash integration. If a webservice returns anything other than a 200 flash has an impossible timie processing the message body so I need to be able to return a "200" with an error and error status code in the HttpResponseMessage if supress_status_codes is true.

Twitter's api has an identical optional parameter for this reason.

Was it helpful?

Solution

Creating a HttpOperationHandler would allow you to process the optional query parameter in a single place. However, if your Operation needs to know whether or not the SuppressStatusCodes has been set, then the easiest way to access it is via a parameter on the operation signature.

Depending on what you need to do, it may be possible to do all of the processing in your custom HttpOperationHandler. Can you describe the impact of SuppressStatusCodes on response?


Update: This could actually be done at a higher layer using a HttpMessageHandler. You could check the url for the query param and change the status code directly. Here is a completely untested example of how it could be done:

    public class StatusKillerMessageHandler : DelegatingChannel {
    public StatusKillerMessageHandler(HttpMessageChannel innerChannel)
        : base(innerChannel) {
    }

    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) {

        bool suppressStatusCode = (request.RequestUri.AbsoluteUri.ToLower().Contains("suppress=true"));

        return base.SendAsync(request, cancellationToken).ContinueWith<HttpResponseMessage>(task => {

                   var response = task.Result;
                   if (suppressStatusCode) {
                        response.StatusCode = HttpStatusCode.OK;
                   }
                   return response;
                });
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top