60

I'm implementing an API using WCF and the specification says to return HTTP 429 in certain circumstances.

Normally I'd simply write:

throw new WebFaultException(HttpStatusCode.NotFound);

However the HttpStatusCode enum does not contain a 429.

I can obviously cast to the enum

throw new WebFaultException((HttpStatusCode)429);

However I'm worried that this will not produce the correct result to the application calling my API.

What's the best way to create extend the HttpStatusCode and send valid (but unsupported) HTTP statuses?

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Liath
  • 9,913
  • 9
  • 51
  • 81
  • 2
    Found this loosely-related link. It doesn't answer the question but might provide background: http://stackoverflow.com/questions/2022887/how-can-i-createa-custom-http-status-code-from-a-wcf-rest-method – TypeIA Mar 25 '14 at 14:05
  • 1
    What kind of specification requires to send an unregistered status code? – Julian Reschke Mar 25 '14 at 15:21
  • @JulianReschke if you check the HTTP spec 429 is indeed a valid code - it's just .NET doesn't support have it in the enum – Liath Mar 25 '14 at 15:26
  • @Liath: Julian works on the HTTP spec. :-) http://tools.ietf.org/html/rfc6585#section-4 is the proposal for HTTP/429. – EricLaw Mar 25 '14 at 20:25
  • @JulianReschke while 429 Too Many Requests isn't mentioned in RFC 2616, it is mentioned in RFC 6585. FWIW, while I haven't used it with WCF, I have found that in ASP.NET setting `HttpResponse.StatusCode` to 429 resulted in the correct number, but not the correct string, so it's necessary to also set `StatusDescription`, or to just set `Status`. I'd worry WCF could end up with a similar partially-correct result due it hitting on the same bit of code that ASP.NET does. – Jon Hanna Mar 25 '14 at 20:26
  • everybody: sorry, I forgot that 429 is indeed are registered code! – Julian Reschke Mar 26 '14 at 06:46

4 Answers4

56

From the C# Language Specification 5.0:

The set of values that an enum type can take on is not limited by its enum members. In particular, any value of the underlying type of an enum can be cast to the enum type and is a distinct valid value of that enum type.

So this is completely alright to do and would be your best bet:

throw new WebFaultException((System.Net.HttpStatusCode)429);
Derek W
  • 9,708
  • 5
  • 58
  • 67
2

In case somebody is using the webApi template, in which the controller is returning an IActionResult object.

return new StatusCodeResult(429);

That worked for me.

diegocl02
  • 2,688
  • 2
  • 9
  • 10
0

If you are hosting the WCF service with IIS you can turn on ASP.Net Compatibility Mode. With that done you can set the status of HttpContext.Current.Response.StatusCode to 429.

With that said. I think your best bet is to try casting the 429 to HttpStatusCode and seeing what happens. If that works then you can save yourself the headache.

Josh
  • 1,724
  • 13
  • 15
-3

You can use HttpStatusCode directly

HttpStatusCode.TooManyRequests

Teepi
  • 1
  • 2