简体   繁体   English

如何返回列表 <object> 在WCF中

[英]How to return a List<object> in WCF

I have my WCF service returning data both in XML and JSON format. 我的WCF服务以XML和JSON格式返回数据。

One functios has to return a List, because I don't know which class will be used to fill this list. 一个函数必须返回一个列表,因为我不知道将使用哪个类来填充此列表。

So, I have my class: 所以,我有我的课:

public class WrapHome
{
    public WrapHome() { }

    private string p_TITOLO { get; set; }
    public string TITOLO { get { return p_TITOLO.ToString(); } set { p_TITOLO = value; } }

    private List<object> p_CHART { get; set; }
    public List<object> CHART { get { return p_CHART; } set { p_CHART = value; } }
}

and my WCF declaration: 和我的WCF声明:

[OperationContract]
[WebGet(UriTemplate = "datiHome.xml?token={token}&p1={p1}&p2={p2}", ResponseFormat = WebMessageFormat.Xml)]
List<WrapHome> GetDatiHomeXML(string token, string p1, string p2);

The output is correctly set, but, when it has to return it converted in XML (or JSON), it re-calls the method and finally give the err_connection_reset error. 正确设置了输出,但是当必须将其返回以XML(或JSON)格式转换时,它会重新调用该方法并最终出现err_connection_reset错误。

I know the problem is the List, because if I comment it, it works. 我知道问题出在清单上,因为如果我发表评论,它就可以工作。 How can I use my List in my WCF output? 如何在WCF输出中使用列表?

If you need more details, ask me without any problem. 如果您需要更多详细信息,请没有任何问题。

You could define 您可以定义

[KnownType(typeof(MyChildObject0))]
...
[KnownType(typeof(MyChildObjectM))]
public class MyBaseObject { ... }

public class MyChildObject0 : MyBaseObject { ... }
...
public class MyChildObjectM : MyBaseObject { ... }

Or you could add the attribute only once and define static method that returns all M+1 types at once. 或者,您可以仅添加一次属性,然后定义一次返回所有M + 1类型的静态方法。

and modify: 并修改:

public class WrapHome
{
  ...
  public List<MyBaseObject> CHART { get;set; }
}

In my case the solution is more simple. 就我而言,解决方案更为简单。

I have a class that I return in all my mehotds, like this: 我有一门课,我全都回来了,就像这样:

[DataContract]
public class Result
    {

        [DataMember]
        public string KeyMensaje;
        [DataMember]
        public string ErrorMessage;        
        [DataMember]        
        public object Objeto;

        ...
   }

The problem is due to Objeto object inside this class. 问题是由于此类中的Objeto object引起的。 This object is used to return variables types and it can't be serialized if is a complex object, for example: 该对象用于返回变量类型,如果是复杂对象,则不能序列化,例如:

res = new Result(list, "ok", "", "", "", null);  
return res;

In this case object (list) is a list of other custom object, for example List<CustomEmployee> 在这种情况下, object (list)是其他自定义对象的列表,例如List<CustomEmployee>

The solution is add on top Result class the complex types that object can be, like this: 解决方案是在Result类的顶部添加object可以是的复杂类型,如下所示:

[DataContract]
[KnownType(typeof(List<CustomEmployee>))]
[KnownType(typeof(CustomEmployee))]
public class Result
{
   ...
}

To make it work, your service has to know the types it needs to serialize. 为了使其工作,您的服务必须知道需要序列化的类型。 If they cannot be found out by looking at your method's signature (for example because one type is object ), you need to list all types that could possibly be in that object . 如果无法通过查看方法的签名来找到它们(例如,因为一种类型是object ),则需要列出该object可能存在的所有类型。

Annotate them to the top of your service class, if you list for example can have instances of foo , bar and baz : 将它们注释到服务类的顶部,例如,如果您列出可以包含foobarbaz实例:

[ServiceKnownType(typeof(foo))]
[ServiceKnownType(typeof(bar))]
[ServiceKnownType(typeof(baz))]

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

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