Question

I am building a ServiceStack service as a Windows Azure Cloud web role. I am trying to POST data/DTO, having properties of type dynamic/ExpandoObject or DynamicTableEntity class, to my service method so that i cloud be able write a generic method to save/fetch any type of object from client side. Also my service doesn't know the class types of the object defined on client side as client can make any new class and send its object to my service. I have used ElasticTableEntity too but it didn't work. When I try to call HTTP POST method for service, it throws an exception of type System.InvalidOperationException saying Cannot return Binary type for a Int32 typed property. Probably a serialization issue.

I have tried the following code:

public class MyCustomEntity
{
    [PartitionKey]
    public string Partition { get; set; }

    [RowKey]
    public string Row { get; set; }

    public int Age { get; set; }

    public string Name { get; set; }

    public string CustomName { get; set; }
}

static void Main(string[] args)
{            
    var customObject = new MyCustomEntity
    {
        Partition = "MyTestEntity",
        Row = "1",
        Age = 36,
        Name = "Name1",
        CustomName = "CustomName1"
    };

    dynamic elasticEntity = new ElasticTableEntity();
    elasticEntity.PartitionKey = "Partition123";
    elasticEntity.RowKey = (DateTime.MaxValue.Ticks - DateTime.Now.Ticks).ToString();
    elasticEntity.Name = "Pascal";
    elasticEntity.Number = 34;
    elasticEntity.Bool = false;
    elasticEntity.Date = new DateTime(1912, 3, 4);
    elasticEntity.TokenId = Guid.NewGuid();
    elasticEntity["LastName"] = "Laurin";

    Save(customObject);
    SaveElastic(elasticEntity);
}

public static T Save<T>(T entity) where T : class,new()
{
    WindowsAzure.Table.EntityConverters.TableEntityConverter<T> entityConverter =
        new WindowsAzure.Table.EntityConverters.TableEntityConverter<T>();

    ITableEntity dynamicEntity = entityConverter.GetEntity(entity);

    SaveRequest request = new SaveRequest { Entity = dynamicEntity };

    var client = new JsonServiceClient(<ServiceURL>);        

    var response = client.Post(request);

    return (T)response.Entity;
}

public static ElasticTableEntity SaveElastic(ElasticTableEntity entity)
{
    SaveElasticRequest request = new SaveElasticRequest { Entity = entity };

    var client = new JsonServiceClient(<ServiceURL>);

    var response = client.Post(request);

    return response.Entity;
}

Can anybody tell what am I missing here?

Was it helpful?

Solution

DynamicTableEntity and any other ExpandoObject doesn't get serialized with servicestack.text.jsonserializer or any other serializer by default. You have to add your custom serialization to handle these dynamic objects.

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