我正在构建ServiceStack服务作为Windows Azure云网络角色。我正在尝试将具有类型为dynamic / ExpandoObject或DynamicTableEntity类的属性的数据/ DTO发布到我的服务方法,以便我可以编写一个通用方法来保存/从客户端获取任何类型的对象。此外,我的服务不知道客户端定义的对象的类类型,因为客户端可以创建任何新类并将其对象发送到我的服务。我也使用了ElasticTableEntity但它没有用。当我尝试为服务调用HTTP POST方法时,它会抛出System.InvalidOperationException类型的异常,说不能返回Int32类型属性的二进制类型。可能是序列化问题。
我尝试过以下代码:
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;
}
有谁可以告诉我在这里缺少什么?
答案 0 :(得分:0)
DynamicTableEntity和任何其他ExpandoObject默认情况下不会使用servicestack.text.jsonserializer或任何其他序列化程序进行序列化。您必须添加自定义序列化来处理这些动态对象。