带动态或DynamicTableEntity对象的ServiceStack发布请求
我正在构建一个ServiceStack服务作为Windows Azure云网络角色。 我正在尝试将data / DTO(具有Dynamic / ExpandoObject或DynamicTableEntity类的属性)发布到我的服务方法,以便我可以编写一个通用方法来从客户端保存/获取任何类型的对象。 另外我的服务不知道在客户端定义的对象的类类型,因为客户端可以创建任何新类并将其对象发送到我的服务。 我也使用了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;
}
有人能告诉我在这里错过了什么吗?
DynamicTableEntity和其他任何ExpandoObject默认情况下都不会使用servicestack.text.jsonserializer或任何其他序列化程序进行序列化。 您必须添加自定义序列化来处理这些动态对象。
链接地址: http://www.djcxy.com/p/68709.html上一篇: ServiceStack post request with dynamic or DynamicTableEntity object
下一篇: ServiceStack: Can I "Flatten" the structure of the post body?