I have implemented a data contract which is successfully deserialized by the WCF test client. Now, how can I do this in a custom client I have made?
The contract I have is this:
[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();
[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; }
}
[DataMember]
int Position
{
get { return _position; }
set { _position = value; }
}
}
}
The service class has the following:
public List<StageContract> getAllStages()
{
return (new StageController())
.getAllStages()
.Values
.Select(s => new StageContract(s))
.ToList();
}
In the client side:
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using KanbanProject.ServiceReference1;
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
var client = new UserServiceClient();
List<StageContract> myList = client.getAllStages();
}
At execution I encounter the following error:
An unhandled exception of type 'System.ServiceModel.Dispatcher.NetDispatcherFaultException'
occurred in mscorlib.dll
Additional information: The formatter
threw an exception while trying to deserialize
the message: There was an error while trying to
deserialize parameter http://tempuri.org/:getAllStagesResult.
The InnerException message was 'Element getAllStagesResult from
namespace http://tempuri.org/ cannot have child contents to be
deserialized as an object. Please use XmlNode[] to deserialize
this pattern of XML.'. Please see InnerException for more details.
I tried looking into XmlNode[] usage: http://geekswithblogs.net/TimH/archive/2006/02/09/68857.aspx
Also this: WCF: Serializing and Deserializing generic collections
This: WCF. IList is deserialized as array. How to make it be any writable list (ArrayList)?
And this: WCF/RESTful DataContract deserialization issue
Thank you in advance!
*UPDATE: After finally deserializing on the client side I encountered the following:
For List<StageContract> myList = client.getAllStages(); I have Cannot implicitly convert type "object" to "System.Colections.Generic.List<StageContract>" An explicit conversion exists(are you missing a cast?).
*SOLVED: answer posted below.