Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

c# - How to return a List<object> in WCF

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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; }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...