Azure table storage service context continues to throw up the same error if it encounters one error

When i try to update or create an entity on azure table storage, it sometimes throws up an error like "Entity already exists" or "One of the request inputs is out of range", and after that if i try to create or update some other entity on that table also ,it continues to throw up the same error. There is no problem with the input as if I restart the iis server, it starts working again. I have no clue why thats happening.

I tried different SaveChangesOptions including "ContinueOnError" but the table service context continues to throw up error on all further updates/creates after it encounters one error.

Below is my code on how am creating the table storage service context.

Please let me know what the issue could be, this has been a huge blocker for me as major functionalities in my app stop working if the table service context encounters just one error.

public class AudioRecordRepository : Repository<PersistedAudioRecord>, IAudioRecordRepository
{
    private TableStorageServiceContext<PersistedAudioRecord> audioRecordServiceContext;
    private CloudStorageAccount cloudStorageAccount;

    public AudioRecordRepository(IServiceContext<PersistedAudioRecord> serviceContext)
        : base(serviceContext)
    {
        if (RoleEnvironment.IsAvailable)
            cloudStorageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString"));
        else
            cloudStorageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]);

        audioRecordServiceContext = new TableStorageServiceContext<PersistedAudioRecord>(TableNames.AudioRecord, cloudStorageAccount.TableEndpoint.ToString(), cloudStorageAccount.Credentials) { IgnoreResourceNotFoundException = true };

    }

    public bool CreateRecord(PersistedAudioRecord record)
    {
        this.audioRecordServiceContext.Create(record);
        this.audioRecordServiceContext.SaveChangesWithRetries(SaveChangesOptions.ContinueOnError);

        return true;
    }
}

Answers


It appears that the context that contains your entities (audioRecordServiceContext) is declared at the class level and it is not clear if/when it is cleared out.

Table Storage Contexts follow a "Unit of Work" design pattern. They are designed to track entities. If you've added a "bad" entity to your context and tried to persist, you'd obviously get an error. However, what you need to be aware of, is that your context did not throw away the bad entity. It is still tracking it and it still thinks you want to save it. So, next call to SaveChanges will attempt to save it again.

Suggestion: put a "using" statement around your context and declare it inside a function only when you need to save entities - do not let it sit on a class level - unless you have a specific use case that requires it. But if you do, then make sure to yank the failing entity out of the context object (you can do so manually by examining Entities collection of the context object)

HTH


Need Your Help

Formatting How Resultset is Returned & Displayed in PHP

php mysql sql

I apologize in advance for the title, I really didn't know how else to word it.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.