简体   繁体   English

WCF序列化/反序列化List <>

[英]Wcf serialization/deserialization of List<>

Here's the code I did first (which is not working), my question will follow the code. 这是我首先执行的代码(不起作用),我的问题将跟随代码。

[DataContract]
public class ValidationData
{
   [DataMember]
   public bool IsValid { get; set; }

   [DataMember]
   public List<string> Messages { get; set; }
}


[DataContract]
public class Parameters
{
   [DataMember]
   public string Name { get; set; }

   [DataMember]
   public ValidationData ValidationData { get; set; }
}


[ServiceContract]
public interface IValidation
{
   [OperationContract]
   void Validate(Parameters parameters);
}

public class Validation : IValidation
{
   public void Validate(Parameters parameters)
   {
      bool valid = someClass.DoValidation(parameters.Name);

      parameters.ValidationData.IsValid = valid;

      if(!valid)
         parameters.ValidationData.Messages.Add("some validation message");
   }
}

On the client side I called the service that way : 在客户端,我这样调用服务:

var service = new WcfService.ValidationService();
var parameters = new WcfService.Parameters();

parameters.Name = "some name to validate";
parameters.ValidationData = new WcfService.ValidationData();
parameters.ValidationData.Messages = new List<string>();

service.SynchronizeDatabase(parameters);

if(!parameters.ValidationData.IsValid)
   ShowMessage(string.join(Environnement.NewLine, parameters.ValidationData.Messages));

Using that code, when I look at parameters.ValidationData on the client side, I have the good value for the field IsValid , but the list is always empty no matter the value of IsValid . 使用该代码,当我在客户端上查看parameters.ValidationData时,我具有很好的IsValid字段值,但是无论IsValid的值如何,列表始终为空。

I know it wasn't the best way of doing it, that's why I changed it. 我知道这不是最好的方法,这就是为什么我更改了它。 Now it's working and the service contract looks like : 现在它正在工作,服务合同如下所示:

[ServiceContract]
public interface IValidation
{
   [OperationContract]
   ValidationData Validate(Parameters parameters); // changed void to ValidationData
}

and the parameters data contracts is now : 现在参数数据合约为:

[DataContract]
public class Parameters // Removed the ValidationData member
{
   [DataMember]
   public string Name { get; set; }
}

And finally the implementation of the operation contract : 最后执行运营合同:

public class Validation : IValidation
{
   public ValidationData Validate(Parameters parameters)
   {
      var validations = new ValidationData(); // in the constructor I initialize the list

      validations.IsValid = someClass.DoValidation(parameters.Name);

      if(!validations.IsValid)
         validations.Messages.Add("some validation message");

      return validations;
   }

} }

My question is why the list is always empty if it's included in the parameters list sent by the client (first way I did this). 我的问题是,如果列表包含在客户端发送的参数列表中,为什么列表始终为空(我这样做的第一种方式)。 And it's not when the ValidationData object is created server side and returned by the operation contract? 而且不是在服务器端创建ValidationData对象并由操作合同将其返回时吗? I'm just looking for an explanation here, as I already solved my problem. 我已经在解决问题了,所以我只是在这里寻求解释。

(Feel free to close it if it don't belongs here...) (如果它不属于这里,请随时关闭它...)

If I understand correctly, you are modifying the list on the server, and you expect that you can read those changes on the client side. 如果我理解正确,那么您正在修改服务器上的列表,并且希望您可以在客户端读取这些更改。 You need to specify the parameter as ref . 您需要将参数指定为ref This way, changes made on the server will come back to the client. 这样,在服务器上所做的更改将返回给客户端。 Otherwise they won't, you will only see on the client side what you originally put in the list, so it will be empty in your case. 否则,它们将不会,您只会在客户端看到最初放入列表的内容,因此在您的情况下将为空。 Ie. 就是

void Validate(ref Parameters parameters);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM