Windows Azure Table で PartitionKey と RowKey を指定して、テーブルが見つからない場合は、デフォルトだと ResourceNotFound が返ってきて、例外(DataServiceQueryException) が発生します。

例外を発生させたくない場合は、TableServiceContext の IgnoreResourceNotFoundException を True に設定にします。

また、IgnoreResourceNotFoundException の設定をせずに、例外を発生させて ResourceNotFound を処理する場合は、StatusCodeを見て判断することが可能です。

サンプルコード

        protected void Page_Load(object sender, EventArgs e)
        {
            CloudStorageAccount account = CloudStorageAccount.FromConfigurationSetting("StorageConnectionString");

            CloudTableClient tableClient = account.CreateCloudTableClient();
            tableClient.RetryPolicy = RetryPolicies.Retry(
                2,
                TimeSpan.FromMilliseconds(100));

            try
            {
                TableServiceContext svc = tableClient.GetDataServiceContext();
                var q = from t in svc.CreateQuery<Table1>("SampleTable")
                        where (t.PartitionKey == "Sample") && (t.RowKey == "key")
                        select t;

                return q.FirstOrDefault();

            }
            catch (DataServiceQueryException ex)
            {
                if (((OperationResponse)ex.Response).StatusCode == (int)HttpStatusCode.NotFound)
                {
                    // 何か処理;
                    return null;
                }
                throw;
            }
        }

        private class Table1 : TableServiceEntity
        {
            public Table1(string name)
            {
                base.PartitionKey = "Sample";
                base.RowKey = name;
            }

            public Table1() : base() { }
        }

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading