将DynamicTableEntity转换为Json [英] Convert DynamicTableEntity to Json

查看:56
本文介绍了将DynamicTableEntity转换为Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC应用程序,并且我执行查询以获取表内容.但是,当我尝试将结果返回给客户端时,我得到了 ExceptionMessage:无法为String类型的属性返回二进制类型."

i have an MVC application and i execute a query to get a table content. But when i try to return the result back to the client i get an ExceptionMessage: "Cannot return Binary type for a String typed property."

控制器代码:

public IEnumerable<DynamicTableEntity> Get(string table)
{
    var storageAccount = CloudStorageAccount.Parse(<StorageConnectionString>);
    CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
    CloudTable table = tableClient.GetTableReference(table);

    TableQuery<DynamicTableEntity> query = new TableQuery<DynamicTableEntity>()
       .Where("")
       .Take(50);

    return table.ExecuteQuery(query);
}

当我执行Get table时,我得到 500 Internal Server Error

When i execute Get table i get 500 Internal Server Error

ExceptionMessage: "The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'."

InnerException:

ExceptionMessage: "Cannot return Binary type for a String typed property."

但是当我调试"Get"功能时,我看到所有数据都按预期接收,这是一个 Json 序列化问题.我尝试使用 CloudTableClient PayloadFormat DefaultRequestOptions 没有成功.

But when i debug the "Get" function i see that all data received as expected and this is a Json serialization issue. I tried to use CloudTableClient PayloadFormat and DefaultRequestOptions with no success.

有什么建议吗?

堆栈跟踪:

{
Message: "An error has occurred."
ExceptionMessage: "The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'."
ExceptionType: "System.InvalidOperationException"
StackTrace: null
InnerException: {
Message: "An error has occurred."
ExceptionMessage: "Error getting value from 'BinaryValue' on 'Microsoft.WindowsAzure.Storage.Table.EntityProperty'."
ExceptionType: "Newtonsoft.Json.JsonSerializationException"
StackTrace: " at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeDictionary(JsonWriter writer, IDictionary values, JsonDictionaryContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType) at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType) at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value) at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding) at System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding) at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content) at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at System.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__1b.MoveNext()"
InnerException: {
Message: "An error has occurred."
ExceptionMessage: "Cannot return Binary type for a Boolean typed property."
ExceptionType: "System.InvalidOperationException"
StackTrace: " at Microsoft.WindowsAzure.Storage.Table.EntityProperty.EnforceType(EdmType requestedType) at Microsoft.WindowsAzure.Storage.Table.EntityProperty.get_BinaryValue() at GetBinaryValue(Object ) at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target)"
}-
}-
}

推荐答案

不幸的是,DynamicTableEntity和EntityProperty当前无法序列化.另一方面,POCO实体是可序列化的.如果将序列化为从TableEntity派生的对象,则该序列应该开箱即用地可序列化.如果要进行真正的异构查询,必须处理存储在单个表中并作为查询结果返回的不同类型,则可以使用EntityResolver解析为适当的类型.

Unfortunately, DynamicTableEntity and EntityProperty are not serializable currently. POCO entities on the other hand are serializable. If you deserialize into an object that derives from TableEntity then this should be serializable out of the box. If you want to do a truly heterogeneous query where you have to deal with different types being stored in a single table and returned as the query result, you can use the EntityResolver to resolve to the appropriate type.

这篇关于将DynamicTableEntity转换为Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆