This past week I’ve been working on a little project – amazing how less email equates to more time for other endeavors – and I was surprised when I received a DataServiceQueryException when querying table storage in the local storage emulator. I was querying based on partition and row keys and, if no data matched the statement, I received an HTTP 404: Resource Not Found exception.

I was initially puzzled. Shouldn’t I receive an empty set or null instead?

Of course, I had forgotten that this is by design. The DataServiceContext will throw a DataServiceQueryException if there’s no data to return. To receive an empty set it’s necessary to set the IgnoreResourceNotFoundException property to true.

Here’s a simplified version of the code:

    string connectionString = "UseDevelopmentStorage=true";

    var context = CloudStorageAccount.Parse(connectionString)
        .CreateCloudTableClient().GetDataServiceContext();

    context.IgnoreResourceNotFoundException = true;

    var results = context.CreateQuery<TableEntity>("tableName")
        .Where(e => e.PartitionKey == partitionKey && e.RowKey == rowKey).AsTableServiceQuery();

    var key = results.FirstOrDefault();

Problem solved. No DataServiceQueryException!

Something to keep in mind when working with the Windows Azure table storage service. I almost didn’t blog about but decided that it was worth a few minutes effort. Probably something to add to your Windows Azure development checklist (you have one, right?).

  • Steve Moir

    This simple property allowed me to remove lots of extra try/catch blocks when it was introduced. Though I think you might have mixed up your properties in your code sample (IgnoreResourceNotFoundException vs. IgnoreMissingProperties)

    • Wade

      Thanks. Yes, I used the wrong property when I cleaned up the code snippet. Fixed. Thanks for the catch!

  • http://www.lucifure.com Lucifure

    I would think that returning an empty an empty set would be the default behavior, instead of throwing an exception.

    I have written an alternate Azure table storage client in F#, Lucifure Stash, which does just that. Lucifure Stash supports large data columns > 64K, arrays & lists, enumerations, out of the box serialization, user defined morphing, public and private properties and fields and more.

    I am looking for reviewers and feedback on the product.
    It is available free for personal use at http://www.lucifure.com or via NuGet.com.

  • Rasmus Christensen

    Using this property and writing unittest against local storage emulator I still get the ResourceNotFound exception. Is this a “Does not work in Storage Emulator”?

  • http://www.servicosnanuvem.com/ Fernando Correia

    Thanks for the tip; it helped me today. I think the “DataServiceQueryException” exception is too generic and I wanted to replace it for a more specific one that clearly indicates the error (resource not found).

  • http://twitter.com/irhetoric Karsten Januszewski

    Thanks Wade — this saved me too!  You are da man!