3

I am trying to throw an HttpResponseException(HttpStatusCode.NotFound) and I am getting the following error

The response message returned by the Response property of this exception should be immediately returned to the client. No further handling of the request message is required.

I have removed all of the code in my method and I am just throwing the exception like this

[WebGet]
public MyData Get()
{
    throw new HttpResponseException(HttpStatusCode.NotFound);
}

If I change my method to return a HttpResponseMessage I can get it to work correctly, however it does not solve the issue and I am unable to get my authentication operation handler to work without being able to throw a HttpResponseException.

SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
Stuart
  • 201
  • 1
  • 11

1 Answers1

0

Try using a WebFaultException for returning HTTP Status codes in WCF...

throw new WebFaultException(HttpStatusCode.NotFound);
SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
  • that worked great but for some reason if I use a status code of unauthorized I still get a 404? – Stuart Feb 18 '12 at 09:36
  • @Stuart do you have any operation/message handler which might be causing the 404? – Antony Scott Feb 18 '12 at 09:40
  • @AntonyScott the only handler I have is an authorization handler that I have copied from a Haack post. It only throws unauthorized exceptions. – Stuart Feb 18 '12 at 09:52
  • i used that and had the same issues, i changed to using a message handler and found that works much better – Antony Scott Feb 18 '12 at 10:55
  • Do you know of an example of using the message handler for authorization – Stuart Feb 18 '12 at 13:46
  • I have written one myself. I will be writing a blog article about it tomorrow (maybe later today), I'll post a link when I've written it. – Antony Scott Feb 19 '12 at 15:16
  • I've been migrating my project to ASP.NET Web Api over the last few days. The task for Monday (when I return to work) is to get authentication working. I suspect this will turn into a non-issue as I'm hoping I can just use the authentication code built in to ASP.NET :) – Antony Scott Feb 24 '12 at 23:38
  • 1
    I am now moving over to ASP.NET Web Api as well, so this should now be a breeze. Thanks for your help @AntonyScott. Now just need to be patient until the RTM of MVC4 – Stuart Feb 29 '12 at 11:31