0

I am new to WCF and I am trying to serialize a list of custom objects. I have already tried the solutions in other articles.

Here is the service interface code:

[ServiceContract]
[ServiceKnownType(typeof(List<Stage>))]
public interface IUserService
{
    [OperationContract]
    void CreateUser(string name, string pwd, bool admin);

    [OperationContract]
    bool LogInUser(string name, string pwd);

    [OperationContract]
    List<Stage> getAllStages();

}

    [DataContract]
public class StageListGetter
{
    private List<Stage> stageList;

    [DataMember]
    public List<Stage> StageList
    {
        get { return stageList; }
        set { stageList = value; }
    }


}

In the service class I have tried this:

public List<Stage> getAllStages()
{
  StageController sctrl = new StageController();
  Dictionary<int, Stage> sdict = sctrl.getAllStages();
  List<Stage> StageValueList = sdict.Values.ToList();
  StageListGetter sg = new StageListGetter();
  sg.StageList = StageValueList;
  return sg.StageList;
} 

And have the following error on invocation:

This could be due to the service endpoint binding not using the HTTP protocol. 
This could also be due to an HTTP request context being aborted by the server (possibly due 
to the service shutting down). See server logs for more details.

What am I missing?

4
  • Please provide code for your Stage class. Commented May 2, 2016 at 14:51
  • @RiadBaghbanli The Stage class is generated by the Entity Framework and represents a table. Commented May 2, 2016 at 14:53
  • @RiadBaghbanli I posted below an updated answer with the outcome. Commented May 2, 2016 at 15:42
  • @RiadBaghbanli Thank you very much! That did it! Commented May 2, 2016 at 16:08

2 Answers 2

1

Simple solution would be for your Stage class to be [DataContract] too. If you have no control over Stage class, then create your own [DataContract] class with public constructor taking instance of the Stage class and copying its content into [DataMember] elements.

[ServiceContract]
public interface IUserService
{
    [OperationContract]
    void CreateUser(string name, string pwd, bool admin);

    [OperationContract]
    bool LogInUser(string name, string pwd);

    [OperationContract]
    List<StageContract> getAllStages();
}

public List<StageContract> getAllStages()
{
  return (new StageController())
     .getAllStages()
     .Values
     .Select(s => new StageContract(s))
     .ToList();
}
Sign up to request clarification or add additional context in comments.

1 Comment

I will try this and come back with feedback. Thank you.
0

*UPDATE: Here is the full working implementation:

I updated the code in the following manner:

[ServiceContract]
[ServiceKnownType(typeof(List<StageContract>))]
public interface IUserService
{
    [OperationContract]
    void CreateUser(string name, string pwd, bool admin);

    [OperationContract]
    bool LogInUser(string name, string pwd);

    [OperationContract]
    List<StageContract> getAllStages();
}

I created the data contract as following:

[DataContract]
public class StageContract
{
    private int _stageID;
    private int _projectID;
    private int _wip;
    private string _name;
    private int _position;

    public StageContract(Stage s)
    {
        StageID = s.ID;
        ProjectID = s.ProjectID;
        WIP = s.WIP;
        Name = s.Name;
        Position = s.Position;
    }

    [DataMember]

    int StageID
    {
        get { return _stageID; }
        set { _stageID = value; }
    }

    [DataMember]
    int ProjectID
    {
        get { return _projectID; }
        set { _projectID = value; }
    }

    [DataMember]
    int WIP
    {
        get { return _wip; }
        set { _wip = value; }
    }

    [DataMember]
    string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    int Position
    {
        get { return _position; }
        set { _position = value; }
    }
}

The service class has this method:

    public List<StageContract> getAllStages() {
        return (new StageController())
           .getAllStages()
           .Values
           .Select(s => new StageContract())
           .ToList();
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.