Quantcast
Channel: Software Development and Infrastructure in the Cloud » Azure
Viewing all articles
Browse latest Browse all 9

Azure Table Storage and the FirstOrDefault

$
0
0

Today I’ve lost the whole afternoon fighting against this damn issue so I want to share this with everyone to save your time!

Problem: FirstOrDefault query returns 404 when no entities are found

I was performing a simple query against Azure Table Storage using PartitionKey and RowKey in order to retrieve a specific entity in a table.

Example:

  • http://127.0.0.1:10002/devstoreaccount1/Spaceship(PartitionKey=’380′,RowKey=’1234′)

During tests, the first scenario was really simple: no entities with given PK and RK present in the table and I was using a simple query ending with FirstOrDefault operator like the following:

[cc lang=”csharp”]
TableRepository.FindWhere(p => p.PartitionKey == entity.Spaceship.CountryCode.ToString()
p.RowKey == entity.SerialNumber).FirstOrDefault();
[/cc]
and I was expecting null but in reality I started to get 404 error with detailed message below:

[cc lang=”xml”]
ResourceNotFound
[/cc]

Solution:

After several hours struggling with various tests I discovered following Pearls of Azure Wisdom:

  • Internet Explorer can’t be used to query directly table storage account, it does not return results at all, because
  • You have to set IgnoreResourceNotFoundException = true in the TableServiceContext in order to receive null when an entity is not found instead of error 404

Viewing all articles
Browse latest Browse all 9

Trending Articles