3

I'm using CouchDB as a data source for a C# web service.

Being RESTful, CouchDB passes back a status code of 404 when asked for a document that does not exist. The standard .NET web request wants to throw an exception at this but (to me, at least) communicating that a data source has returned "no results" via an exception is utterly horrible; and it's a stink I really don't want wafting around in my code...

Is there any replacement for WebRequest I can use that will allow me to deal with status codes as I see fit?

EDIT: Just to clarify, due to the responses I've had so far: I do not want to hide the exception that WebRequest throws. I am looking for an alternative to the standard WebRequest that does not throw exceptions based on status codes as .NET's interpretation of what constitutes an error doesn't seem in-line with REST principles.

EDIT #2 I really need a 3.5 compatible way of doing this; sorry for not being specific about that at the start.

jdoig
  • 1,472
  • 13
  • 27
  • Why don't you wrap the httpWebrequest in your own proxy so that you handle the exception in one place, the rest of your code can then deal with the status? – rene Mar 01 '11 at 09:26
  • http://stackoverflow.com/questions/2149208/how-to-properly-catch-a-404-error-in-net – Branimir Mar 01 '11 at 09:26
  • Catch the exception within the DAL layer so it's hidden from the rest of the application? – Duncan Watts Mar 01 '11 at 09:26
  • I don't want exceptions conveying information about totally valid responses in my code at all, if possible, hiding the exception does not fix that. – jdoig Mar 01 '11 at 09:52
  • @jdoig Sure it does, you catch the exception in your wrapper, and return something you can use instead. – nos Mar 02 '11 at 09:48
  • @nos, "0 rows" or "no data" is a valid response from a data source; via REST that manifests itself as a 404... Passing expected and perfectly valid responses via throwing exceptions is a MASSIVE no-no. Would you replace and if/else with a try/catch? would you be happy if ADO.NET threw every time a SQL query returned 0 rows? I'm not happy about the .NET framework deciding that an arbitrary range of predefined status codes are "Exceptional". I don't want to hide the problem because hiding it wont work; I don't want the problem in my code because it's lousy practice. – jdoig Mar 02 '11 at 12:24
  • @jdoig Actually, returning 404 from a query that returns no rows is not an ideal response. The query should really return 200 because the search resource does exist. As you say, returning 0 rows is a normal response so 200 is the right status code. – Darrel Miller Mar 02 '11 at 14:01

3 Answers3

2

The HttpClient library does not throw exceptions after the request. See this for usage examples.

Darrel Miller
  • 139,164
  • 32
  • 194
  • 243
  • I have just tried this (as explained in the above link): var client = new HttpClient(); var response = client.Get("myCouchDBSever/user/someUserID"); and it does throw upon getting a 404 response :( – jdoig Mar 01 '11 at 14:28
  • @jdoig Strange, it doesn't for me. – Darrel Miller Mar 01 '11 at 18:27
  • 1
    @jdoig I also just checked the newest version of HttpClient which is on http://wcf.codeplex.com and it doesn't throw an exception for me either. – Darrel Miller Mar 01 '11 at 18:31
  • +1, No idea what's going on. But I just hope this answer can defeat my own answer since I am philosophically opposed to using CouchDB client libraries (at least for simple things like document manipulation). – JasonSmith Mar 02 '11 at 05:18
  • Ahh I was using the HttpClient from the Rest Starter Kit ... the latest version is a replacement for that and is .NET 4.0 only :( – jdoig Mar 02 '11 at 09:12
  • @jdoig Yes I tried the one from the RSK too and for me it does not throw exceptions. – Darrel Miller Mar 02 '11 at 13:58
0

I have not used them but there are several dedicated C# CouchDB client libraries.

There is CouchOne's list of CouchDB drivers.

Also there is the CouchDB wiki list of C# clients.

My personal preference is to stick as closely to the HTTP layer as possible. HTTTP is very simple and the CouchDB API is very simple. There is no need for middleware to access it. (It is unfortunate that your WebRequest class apparently has this bug.)

JasonSmith
  • 72,674
  • 22
  • 123
  • 149
  • Thanks, but this is what I am doing. My code for accessing couch is minimal (a LOT less code than other available .NET couch libraries). The problem lies in .NET's standard way of making Http web requests; which throws errors on 4xx and 5xx status codes. It is not a "bug" in either my code or the .NET library, just a frustratingly inflexible implementation. – jdoig Mar 01 '11 at 10:26
-2

I'm not sure what's your problem. If you don't want to get 404 when asking doesn't exist documents, I think you just need to add a wildcard application maps in your IIS settings and uncheck the "Verify that file exists" box.

zpisgod
  • 167
  • 8