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?
I recommend you to use
WebFaultException
in .NET 4.0. Read for example "Introducing WCF WebHttp Services in .NET 4". Try