8

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?

  • Self-hosted or IIS? Which server version? I tested this on 4.0 with Server 2008R2 self-hosted, and it works fine (returns the Status Description as set). – nitzmahone Aug 09 '10 at 22:31
  • Did you ever find a solution? I am facing the same problem. – Hemant Jan 13 '11 at 08:03

4 Answers4

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);
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Sorry but using .NET 4.0 is not an option. Any luck with .NET 3.5? – Hemant Jan 17 '11 at 14:51
  • @Hemant: Sorry for misunderstanding, but you start your question with the word, that you are currently migrating your service from .NET 3.5 (Starter Kit) to .NET 4 and you have some problems. So I understand you so, that you have already working solution in .NET 3.5 and you want have it working in .NET 4.0. I had the same problem. In .NET 3.5 I used `WebProtocolException` to report the errors. After migration to .NET 4.0 I decide **not use** deprecated WCF REST Starter Kit. .NET 4.0 has all features needed for WCF REST development. Instead of `WebProtocolException` I use `WebFaultException`. – Oleg Jan 17 '11 at 15:51
  • Thanks for reply. I didn't originally ask this question. I was just struggling with the same problem and seeing that above already asked question has no answers, put a bounty on it. – Hemant Jan 17 '11 at 17:33
  • @Hemant: Now I see this. Do you use WCF REST Starter Kit Preview 2 under Visual Studio 2008 or 2010? Under VS2008 I had no problem with the usage of WCF REST Starter Kit Preview 2 inclusive error reporting with respect of `WebProtocolException`. I had only migration some problems to .NET 4.0 and Visual Studio 2010. What problem do you have? – Oleg Jan 17 '11 at 18:22
2

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.

Community
  • 1
  • 1
Hemant
  • 19,486
  • 24
  • 91
  • 127
  • Incidentally, this is also the reason why it doesn't work in Web Api. I found your answer and as soon as I ran my code under IIS, the ReasonPhrase changed the status description. – Richard Nienaber Jun 18 '12 at 10:25
1

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?

Shiraz Bhaiji
  • 64,065
  • 34
  • 143
  • 252
1

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

Hydtechie
  • 41
  • 1