سؤال

I'm using Azure Table Storage. When I query a table that is empty with parameters other than PartitionKey and RowKey involved, I get an exception. When I have at least one row, the exception doesn't appear. If I query the empty table with just PartitionKey and RowKey, it is OK.

I certainly do not want to make an extra round trip to test if the table is empty. How do people normally solve this problem? Is there a performant way to quickly check if the table is empty?

I am using the development storage, as I just saw there are reported errors in this scenario with the development storage and the error goes away in production. However, I do not want to keep customized code just for development storage, is there a good way to get around this, so I could have the same code running local as well as in production cloud environment?

هل كانت مفيدة؟

المحلول

I've got this around by setting DataServiceContext.IgnoreResoureNotFoundException property to true. Hope this helps others too.

نصائح أخرى

I couldn't ever get the IgnoreResourceNotFoundException to work and punted it. Took a 'naughty' route and just exception trapped for an empty table. Snip below, enjoy...

CloudTableClient _tableClient = OurStorageAccount.CreateCloudTableClient();
CloudTable _table = _tableClient.GetTableReference( "customers" );

TableQuery<CustomerEntity> _query = new TableQuery<CustomerEntity>();
var _result = _table.ExecuteQuery( _query );

StringBuilder _sb = new StringBuilder(1024);
try
{ 
	_sb.AppendLine("Begin listing customers....<br/>");
	foreach ( CustomerEntity _customer in _result )
	{
		_sb.AppendFormat( "{0} {1} - {2} - {3}<br/>", _customer.PartitionKey, _customer.RowKey, _customer.Email, _customer.PhoneNumber );
	}
	_sb.AppendLine("End listing customers....<br/>");
}
catch ( System.NullReferenceException _nullEx )
{ 
	_sb.Append( System.DateTime.Now.ToString() );
	_sb.AppendLine( ": no customer entries found<br/>" );
	System.Diagnostics.Debug.WriteLine( _nullEx.ToString());
	// _sb.AppendLine( _nullEx.ToString() );
}
catch (Exception _ex)
{
	_sb.AppendLine("unkown exception thrown, good luck<br/>");
	_sb.AppendLine( _ex.ToString() );
}

Label_Output.Text = _sb.ToString();

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top