2

我想通过 WCF Web Api 使用 Massive 进行数据访问,并从我的 Web api 返回动态或 ExpandoObject / IEnumerable<ExpandoObject>。

我基本上使用 JsonNetMediaTypeFormatter 工作,它使用 Json.NET 的 ExpandoObject 序列化,但所有内容都作为 Json 中的键值对返回,例如:

[
    {
        "Key":"ID",
        "Value":"1000"
    },
    {
        "Key":"FirstName",
        "Value":"John"
    },
    {
        "Key":"LastName",
        "Value":"Smith"
    }
]

但是,我想要的是:

[
    {
        "ID":"1000",
        "FirstName":"John",
        "LastName":"Smith",
    }
]

好像我使用的是具体类型,例如:

public class Customer
{
    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

当从 WCF Web Api 返回时,关于如何将动态/ExpandoObject 格式化为具体对象的任何想法?

4

2 回答 2

0

我认为您正在使用 Expando Query 并传递给 WCF。只需尝试进行迭代或将 ToList 提供给您的集合。这会将 ExpandoQuery 转换为 Expando 对象集合。如果你是 POCO 来映射,因为客户在你的问题中。使用您的 POCO 对象进行选择。

就像您的查询是

Dynamic CustomerTable = DynamicObject("ConnectionString","TableName","PrimeryKey");

CustomerTable.All() //This will be ExpandoQuery

CustomerTable.All().Select(c=> new Customer () {FistName = c.FirstName, LastName = c.LastName}); // This will give collection of customer object. Just pass this as DTO to your WCF service.

我希望这能帮到您。让我知道是否还有其他内容。

于 2012-09-03T04:46:22.340 回答
0

Web WCF 的自定义媒体类型格式化程序的一些详细说明:

http://geekswithblogs.net/michelotti/archive/2011/06/06/understanding-custom-wcf-web-api-media-type-processors-on-both.aspx

我猜他可能正在使用 json.net 或其他库进行动态对象序列化

http://blogs.clariusconsulting.net/kzu/using-json-net-for-text-and-binary-json-payloads-with-wcf-webapi/

网络 WCF

http://wcf.codeplex.com/wikipage?title=WCF%20HTTP

于 2011-11-22T22:03:31.190 回答