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
throw new WebFaultException<string> ("bad digest", HttpStatusCode.Forbidden);
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With