How to return a List<object> in WCF
Asked Answered
C

3

0

I have my WCF service returning data both in XML and JSON format.

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:

[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.

I know the problem is the List, because if I comment it, it works. How can I use my List in my WCF output?

If you need more details, ask me without any problem.

Casablanca answered 24/5, 2016 at 13:44 Comment(5)
You can enumarate all possible types that can eventually be in the List, right?Caswell
@Caswell well, I think yes, the possible types are 5 or 6 in total, so yes. Why?Casablanca
you could define one basetype for your objects that should be placed into List - this object can be marked with KnownTypeAttribute - with all types listed as parameter.. - or the KnownTypes can be defined on the level of service/contractCaswell
@Caswell do you have an example? I didn't understood well..Casablanca
Haven't done WCF in a while, but do you need to add the [DataMember] to the public List<object> in the WrapHome class?Adviser
C
1

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.

and modify:

public class WrapHome
{
  ...
  public List<MyBaseObject> CHART { get;set; }
}
Caswell answered 24/5, 2016 at 13:58 Comment(2)
Now I get a conversion error, before compiling: var temp = new List<MyBaseObject>(); temp.Add(new TortaTipoPagE("aaa", "1111")); temp.Add(new TortaTipoPagE("bbb", "2222")); var tt = new WrapHome() { }; tt.TITOLO = "prova"; tt.CHART = temp; r.Add(tt); what am I doing wrong?Casablanca
You have to inherit TortaTipoPagE : MyBaseObjectCaswell
W
1

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. 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>

The solution is add on top Result class the complex types that object can be, like this:

[DataContract]
[KnownType(typeof(List<CustomEmployee>))]
[KnownType(typeof(CustomEmployee))]
public class Result
{
   ...
}
With answered 23/7, 2018 at 10:3 Comment(0)
M
0

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.

Annotate them to the top of your service class, if you list for example can have instances of foo, bar and baz:

[ServiceKnownType(typeof(foo))]
[ServiceKnownType(typeof(bar))]
[ServiceKnownType(typeof(baz))]
Mort answered 23/7, 2018 at 16:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.