Wednesday, September 30, 2009

404 Resource Not Found For Segment accessing Azure Table Storage

On Windows Azure Table Storage, when you are searching for items (or entities, as the Azure documation calls them) using LINQ on a table, under some circumstances you may get a 404 - Resource Not Found error. That's because Azure Table Storage is REST-based, and REST is about servers serving "resources" to clients. For example, HTTP is REST-based, and when you ask for a file that doesn't exist on a server, you get a "404 Page Not Found" error.

What is happening is pretty much the same. According to a comment by Andrew Conrad, from Microsoft LINQ To REST project team, if you search a list by its keys, you want a specific resource, and if the resource is not found, the 404 error is generated. This may make sense from a REST point of view, but if I'm accessing a table, if I don't find what I'm looking for, I expect a empty resultset, and not an exception. By Andrew's comment above, the LINQ to REST team is aware of this peculiarity on Azure, and plans on doing something more intuitive in the future.

For now you will have to use a ugly try/catch block around your code:
try
{
var element =
(for item in azureContext.MyTable
where item.PartitionKey = '...' and item.RowKey = '...'
select item).FirstOrDefault();
}
catch (DataServiceQueryException ex)
{
if(ex.Response.StatusCode == 404)
// element not found
}

No comments:

Post a Comment