简体   繁体   English

WCF REST JSON 返回动态列表

[英]WCF REST JSON return dynamic list

How can I return an object containing a dynamic list?如何返回包含动态列表的对象?

I've got a working REST service in which I want to return JSON data.我有一个可用的 REST 服务,我想在其中返回 JSON 数据。 That works pretty well in most cases - except for one:这在大多数情况下效果很好——除了一种:

In that specific case I've got a List<Bla> which can contain objects of type Bla and Bla1 (which inherits from Bla ).在那个特定情况下,我有一个List<Bla> ,它可以包含BlaBla1类型的对象(从Bla继承)。 As soon as I add a Bla1 to the list the result I get in my browser is an error.一旦我将Bla1添加到列表中,我在浏览器中得到的结果就是一个错误。

Firefox: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://.../DoSomething . Firefox:跨域请求被阻止:同源策略不允许读取https://.../DoSomething 上的远程资源。 (Reason: CORS request did not succeed). (原因:CORS 请求没有成功)。

Chrome: GET https://.../DoSomething net::ERR_SPDY_PROTOCOL_ERROR Chrome:获取https://.../DoSomething net::ERR_SPDY_PROTOCOL_ERROR

How can I return an object containing a dynamic list?如何返回包含动态列表的对象?

Classes班级

[DataContract]
public class Blibla
{
    [DataMember] public bool requestSuccess;
    [DataMember] List<Blubb> blubb;
    [DataMember] List<Bla> blas;

    public Blibla(bool success)
    {
        this.requestSuccess = success;
        blubb = new List<Blubb>() { new Blubb(11, "einser"), new Blubb(22, "zweier"), new Blubb(33, "dreier") };
        blas = new List<Bla>() { new Bla(11), new Bla1(22, 22) };
    }
}

[DataContract]
public class Bla
{
    [DataMember] public int id;

    public Bla(int id)
    {
        this.id = id;
    }
}

[DataContract]
public class Bla1 : Bla
{
    [DataMember] public int num;

    public Bla1(int id, int num) : base(id)
    {
        this.num = num;
    }
}


[DataContract]
public class Blubb
{
    [DataMember] public int ID;
    [DataMember] public string name;

    public Blubb(int id, string name)
    {
        this.ID = id;
        this.name = name;
    }
}

IService:服务:

[Description("returns service's details")]
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json)]
Blibla DoSomething();

Service:服务:

public Blibla DoSomething()
{
    Message msg;
    DoHttpMethodTypeSpecific();

    Blibla bb = new Blibla(true);
    return bb;
}

EDIT编辑

Abraham Qian's answer is exactly what I was looking for.亚伯拉罕·钱的回答正是我要找的。
My service responds with a correctly serialized Bla1 -object.我的服务以正确序列化的Bla1响应。

{
    "__type":"Bla1",
    "id":22,
    "num":22
}

If someone knows of a way to suppress the automatically added "__type":"Bla1"如果有人知道抑制自动添加的"__type":"Bla1"
please let me know...请告诉我...

According to your error and code, I have made a test and found that there is a serialization problem in the Bla class.根据你的错误和代码,我做了一个测试,发现Bla类存在序列化问题。 When transferring the Bla class, WCF could not recognize the subclass(Bla1) and how to serialize them, So we should add the KnowType attribute to the base class.在传递Bla类时,WCF无法识别子类(Bla1)以及如何序列化它们,因此我们应该将KnowType属性添加到基类中。

[DataContract]
[KnownType(typeof(Bla1))]
public class Bla
{
    [DataMember] public int id;
    public Bla(int id)
    {
        this.id = id;
    }
}

https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/data-contract-known-types https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/data-contract-known-types

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM