kseeker
kseeker

Reputation: 21

Migrate from Microsoft.WindowsAzure.Storage.Table to Azure.Data.Tables - deserialization issues

I'm in the process of migrating code from Microsoft.WindowsAzure.Storage.Table to the Azure.Data.Tables. The issue described appears on querying entities with content that don't match the datatype. Consider the following snippet of migrated code:

public class WorkstationInfoTableEntity : Azure.Data.Tables.ITableEntity
{
    //ITableEntity
    public string PartitionKey { get; set; }
    public string RowKey { get; set; }
    public DateTimeOffset? Timestamp { get; set; }
    public ETag ETag { get; set; }

    //
    public byte[] Documents { get; set; }
    public DateTime? MyDocumentsLastRestoreDate { get; set; }
    public DateTime? MyDocumentsModifiedOn { get; set; }    
    ....
    //
}
var table = tableServiceClient.GetTableClient(CloudTableNames.Workstations);
var workstations = table.Query<WorkstationInfoTableEntity>(c => c.PartitionKey == customerId);
var results = workstations.OrderBy(c => c.Name).ToArray();

If the table contains an entity that holds some string: "[RME]" (any non base64 string) in the Documents column then the Query will fail complaining that : "System.FormatException: 'The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters." because it expects a base64 string but the value is just some string.

Or if the MyDocumentsModifiedOn column contains some empty string inside then the query crashes with the error: "System.FormatException: 'String was not recognized as a valid DateTime."

The Microsoft.WindowsAzure.Storage.Table library would have populated the property with a null value if a deserialization problem had occurred.

Expected are a list of entities whose properties that failed being deserialized are set to null. Actual behavior: the code crashes

Is there any way to prevent crashing if any deserialization issue appeared ?

Right now, it is imposible to correct those entities.

Upvotes: 1

Views: 640

Answers (1)

Kyle W XTR
Kyle W XTR

Reputation: 11

So this is more of a workaround since you can't update update all of the empty MyDocumentsModifiedOn to DateTime.MinValue.

    public class WorkstationInfoTableEntity : Azure.Data.Tables.ITableEntity
    {
        //ITableEntity
        public string PartitionKey { get; set; }
        public string RowKey { get; set; }
        public DateTimeOffset? Timestamp { get; set; }
        public ETag ETag { get; set; }

        //
        public byte[] Documents { get; set; }
        public DateTime? MyDocumentsLastRestoreDate { get; set; }
        public DateTime? _MyDocumentsModifiedOn { get; set; }    
        public string? MyDocumentsModifiedOn 
        { 
            get
            {
                return _MyDocumentsModifiedOn.ToString() // Or whatever format you need
            }
            set
            {
                if(!Value.HasValue)
                {
                    _MyDocumentsModifiedOn = DateTime.MinValue;
                }
                else if(Value.Value == "")
                {
                    _MyDocumentsModifiedOn = DateTime.MinValue;
                }
                else
                {
                    DateTime TempDate;
                    if(DateTime.TryParse(Value.Value, out TempDate))
                    {
                        _MyDocumentsModifiedOn = TempDate
                    }
                    else
                    {
                        _MyDocumentsModifiedOn = DateTime.MinValue;
                    }
                }
            }
        }  
        ....
        //
    }

You will have to use _MyDocumentsModifiedOn to get the datetime. Not an amazing solution but it'll work.

Upvotes: 0

Related Questions