Unexpected Internal Storage Client Error when using Windows Azure Storage Client Library

Recently I was working on ASP.NET webrole to implement Sessions between multiple instance using ASP Provider sample, in which I was using Windows Azure Storage Table to store the session data. To achieve it, I have code to access Windows Azure Storage Tabled and I found that the following code was generating an exception:

 

try

{

             TableServiceContext tSvc = CreateDataServiceContext();

             SessionRow sessionRow = GetSession(id, tSvc);

             ReleaseItemExclusive(tSvc, sessionRow, lockId);

}

Catch()

{

 }

 

The exceptions details are as below:

 

System.Configuration.Provider.ProviderException was unhandled by user code

  Message=Error accessing storage.

  Source=AspProviders

  StackTrace:

       at Microsoft.Samples.ServiceHosting.AspProviders.TableStorageSessionStateProvider.GetSession(String id, DataServiceContext context)

       at Microsoft.Samples.ServiceHosting.AspProviders.TableStorageSessionStateProvider.<>c__DisplayClass4.<ResetItemTimeout>b__3()

       at Microsoft.Samples.ServiceHosting.AspProviders.ProviderRetryPolicies.RetryNImpl(Action action, Int32 numberOfRetries, TimeSpan minBackoff, TimeSpan maxBackoff, TimeSpan deltaBackoff)

       at Microsoft.Samples.ServiceHosting.AspProviders.ProviderRetryPolicies.<>c__DisplayClass1.<RetryN>b__0(Action action)

       at Microsoft.Samples.ServiceHosting.AspProviders.TableStorageSessionStateProvider.ResetItemTimeout(HttpContext context, String id)

       at System.Web.SessionState.SessionStateModule.BeginAcquireState(Object source, EventArgs e, AsyncCallback cb, Object extraData)

       at System.Web.HttpApplication.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()

       at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

  InnerException: Microsoft.WindowsAzure.StorageClient.StorageClientException

       HelpLink=https://go.microsoft.com/fwlink/?LinkID=182765

       Message=Unexpected internal storage client error.

       Source=Microsoft.WindowsAzure.StorageClient

       StackTrace:

            at Microsoft.WindowsAzure.StorageClient.Tasks.Task'1.get_Result()

            at Microsoft.WindowsAzure.StorageClient.Tasks.Task'1.ExecuteAndWait()

            at Microsoft.WindowsAzure.StorageClient.TaskImplHelper.ExecuteImplWithRetry[T](Func'2 impl, RetryPolicy policy)

           at Microsoft.WindowsAzure.StorageClient.CommonUtils.<LazyEnumerateSegmented>d__0'1.MoveNext()

            at System.Collections.Generic.List'1..ctor(IEnumerable'1 collection)

            at Microsoft.Samples.ServiceHosting.AspProviders.TableStorageSessionStateProvider.GetSession(String id, DataServiceContext context)

       InnerException:

 

Stack trace:

   at Microsoft.WindowsAzure.StorageClient.Tasks.Task'1.get_Result()

   at Microsoft.WindowsAzure.StorageClient.Tasks.Task'1.ExecuteAndWait()

   at Microsoft.WindowsAzure.StorageClient.TaskImplHelper.ExecuteImplWithRetry[T](Func'2 impl, RetryPolicy policy)

   at Microsoft.WindowsAzure.StorageClient.CommonUtils.<LazyEnumerateSegmented>d__0'1.MoveNext()

   at System.Collections.Generic.List'1..ctor(IEnumerable'1 collection)

   at Microsoft.Samples.ServiceHosting.AspProviders.TableStorageSessionStateProvider.GetSession(String id, DataServiceContext context)

 

 

Based on above error I found that it is caused by a known issue with ADO.NET. This error is due to a bug in the ADO.NET Client Library version 1.0. The bug will be potentially fixed in ADO.NET version 1.5. In this case you have following two options to solve this problem:

 

Option 1:

Re-create ADO.NET Context Object After Unexpected Internal Storage Client Error

If you are using the Windows Azure Storage Client Library to work with the Table service, your service may throw a StorageClientException with the error message "Unexpected Internal Storage Client Error" and the status code HttpStatusCode.Unused. If this error occurs, you must re-create yourTableServiceContext object. This error can happen when you are calling one of the following methods:

  • TableServiceContext.BeginSaveChangesWithRetries
  • TableServiceContext.SaveChangesWithRetries
  • CloudTableQuery.Execute
  • CloudTableQuery.BeginExecuteSegmented

If you continue to use the same TableServiceContext object, unpredictable behavior may result, including possible data corruption. You may wish to track the association between a given TableServiceContext object and any query executed against it, as that information is not provided automatically by the Storage Client Library.

 

The solution is described as below:

You must re-create your TableServiceContext object.

 

The issues here is that you have a code path which keeps retrying to connect to Azure Storage Tables if it did not connect first time and it is using the same TableServiceContent Object for subsequent retries. The suggestion is given to create a new TableServiceContent each time when the Azure Storage Table connection request is made so you will not reuse the old TableServiceContext.

 

 

Option 2:

Use ADO.NET 1.5 which is not RTM yet however not supported with .NET 4. So if you are using .NET 3.5 SP1, you can use the ADO.NET CTP2

ADO.NET Data Services v1.5 CTP2

This CTP is an early preview of the v1.5 features for ADO.NET Data Services. This CTP targets the Microsoft .NET Framework 3.5 SP1 and extends the functionality we provided in v1.0 of ADO.NET Data Services by providing additional features.

https://www.microsoft.com/downloads/en/details.aspx?FamilyID=a71060eb-454e-4475-81a6-e9552b1034fc&displaylang=en