Type 'MyClass' cannot inherit from a type that is not marked with DataContractAttribute or SerializableAttribute. Consider marking the base type

StackOverflow https://stackoverflow.com/questions/18446246

Question

I get this error message when I try to serialize an entity which derives from TableEntity:

Type 'MyClass' cannot inherit from a type that is not marked with DataContractAttribute or SerializableAttribute. Consider marking the base type 'Microsoft.WindowsAzure.Storage.Table.TableEntity' with DataContractAttribute or SerializableAttribute, or removing them from the derived type.

The error message is pretty clear about what's going wrong.

So my question is, how can I work around DataContractAttribute not being decorated in the TableEntity class?

Code:

[DataContract]
public class MyClass : MyOwnTableEntity 
{
    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public string Email { get; set; }
}

I need to serialize MyClass as a byte[] and save that into the table storage.

public class MyClassAsByteArray : MyOwnTableEntity 
{
   public byte[] SomeByteArray { get; set; }
}

If anyone has a suggestion on how to serialize this as a byte[], please let me know.

[Edit]

I decided to create my own serializable TableEntity:

[DataContract]
public class MyOwnTableEntity : ITableEntity
{
    private TableEntity te = new TableEntity();

    public MyOwnTableEntity ()
    {
    }

    public MyOwnTableEntity (string partitionKey, string rowKey)
    {
        this.PartitionKey = partitionKey;
        this.RowKey = rowKey;
    }

    public string PartitionKey { get; set; }

    public string RowKey { get; set; }

    public DateTimeOffset Timestamp { get; set; }

    public string ETag { get; set; }

    public virtual void ReadEntity(IDictionary<string, EntityProperty> properties, OperationContext operationContext)
    {
        te.ReadEntity(properties, operationContext);
    }

    public virtual IDictionary<string, EntityProperty> WriteEntity(OperationContext operationContext)
    {
        return te.WriteEntity(operationContext);
    }
}

And then I derive from this class, but it fails writing the properties of MyClass and MyClassAsByteArray to the storage table. And that's because I created a new object of TableEntity.

How can I forward the parameters passed to ReadEntity and WriteEntity in MyOwnTableEntity to ReadEntity and WriteEntity methods in the actual TableEntity class?

Microsoft already wrote the code, so i'd like to prevent reinventing the wheel here.

EDIT See TableEntity.cs

Was it helpful?

Solution

Went back to MSSQL and dropped Azure Table Storage as a solution. Simply because it has too many limitations.

OTHER TIPS

One approach would be to override it:

public new byte[] SomeByteArray { get; set; }

and then you could ignore it with the IgnoreDataMember attribute.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top