WCF 4 REST service can't return a StatusDescription, only StatusCode

4.7k views Asked by At

I'm currently migrating my WCF RESTful service from .NET 3.5 (Starter Kit) to .NET 4. I started my project using a WCF Rest service template from Visual Studio 2010. I had to figure out how to keep my authorization scheme (formely done with RequestInterceptor) using ServiceAuthorizationManager. After some work and researching I got it done. But now I have a collateral problem. My service used to feedback my client of any processing errors using HTTP status code and a brief description. I was using WebOperationContext at many points of my service method to describe to clients what went wrong, like this:

protected void returnCode(HttpStatusCode code, string description)
{
    WebOperationContext ctx = WebOperationContext.Current;
    ctx.OutgoingResponse.StatusDescription = description;
    ctx.OutgoingResponse.StatusCode = code;
}

But in WCF 4, only StatusCode works - StatusDescription silently fails. I can't figure out why. My only guess is that WebOperationContext doesn't work in this new WCF 4 scenario, and I should be using OperationContext instead, but that also doesn't work. The following method is used in my custom class extending ServiceAuthorizationManager, informing clients a request couldn't be access because auth digest was malformed:

private void GenerateBadDigestMessage(ref OperationContext operationContext)
{
    Message reply = Message.CreateMessage(MessageVersion.None, null, null, new DataContractJsonSerializer(typeof(object)));

    HttpResponseMessageProperty hrp = new HttpResponseMessageProperty();
    hrp.StatusCode = HttpStatusCode.Forbidden;
    hrp.StatusDescription = "bad digest";
    reply.Properties[HttpResponseMessageProperty.Name] = hrp;

    operationContext.RequestContext.Reply(reply);
    operationContext.RequestContext = null;
}

Even by using OperationContext direclty here (insted of WebOperationContext), StatusDescription doesn't work.

What I'm missing here? Why such a small thing can break from .NET 3.5 to 4?

4

There are 4 answers

4
Oleg On

I recommend you to use WebFaultException in .NET 4.0. Read for example "Introducing WCF WebHttp Services in .NET 4". Try

throw new WebFaultException<string> ("bad digest", HttpStatusCode.Forbidden);
0
Shiraz Bhaiji On

One potential problem is that you are setting the RequestContext to null:

operationContext.RequestContext.Reply(reply);     
operationContext.RequestContext = null; 

Another possibility is that the parameter "description" is not set.

Also on the client side are you checking:

WebOperationContext.Current.IncomingResponse.StatusDescription

One more possibility, could the values have been overwritten after returnCode was called?

1
Hemant On

OK! Here is what I found out. There is nothing wrong with my code. There is nothing wrong with .NET framework 3.5 or 4.0.

The problem is asp.net development server. When you are debugging your service application, it is likely to be hosted on asp.net development server and it completely ignores the status description given by application. Refer this question.

Awarding the bounty to @Oleg who at least tried to help me.

0
Hydtechie On

Make sure you return from the Service Method NULL object...so that Status code description is visible in Response Headers, it worked for me.