WCFテストクライアントによって正常に逆シリアル化されたデータコントラクトを実装しました。今、私が作成したカスタムクライアントでこれをどうやって行うことができますか?私は、次の遭遇の実行時にWCFテストクライアントはどのようにデータ契約をデシリアライズしますか? DataContractオブジェクトのリストを逆シリアル化する方法
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();
}
:クライアント側で
public List<StageContract> getAllStages()
{
return (new StageController())
.getAllStages()
.Values
.Select(s => new StageContract(s))
.ToList();
}
:
[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; }
}
}
}
サービスクラスには、次があります。
私が持っている契約書はこれですエラー:
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.
私はXmlNodeの[]の使用を検討してみました:http://geekswithblogs.net/TimH/archive/2006/02/09/68857.aspx
また、この:WCF: Serializing and Deserializing generic collections
この:WCF. IList is deserialized as array. How to make it be any writable list (ArrayList)?
そして、この:WCF/RESTful DataContract deserialization issue
は、事前にありがとうございます!
* UPDATE: 最後に、クライアント側で逆シリアル化した後、私は次のように発生しました:
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?).
は*解決しよう:答えは以下の掲示。
@jstreetこれを試してもうまくいきません。クライアントはStageContract型について知らず、Object型のオブジェクトを受け取っているようです。これは、デシリアライズするフォーマッタの問題であり、XmlNode []を使うことを示唆しています。 – Vestilios
@jstreet私はしましたが、使用できなくなりました。私は、System.Collections.Generic.Listを返すようにサービス参照を再構成しました。キャストしようとしましたが、まだ動作しません。 – Vestilios
@jstreet私はサービスのMCVEを投稿しました。 – Vestilios