我正在尝试用c#在.Net应用程序之间交换数据。
为了交流沟通,我跟着引入基本HTTP服务的this tutorial,它与字符串交换完美配合。
现在,我想发送自定义对象,但是当我重新生成服务时,未映射新方法Message chat(Message msg)
。
这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace StairWays.Messaging
{
/// <summary>
/// Classe permettant d'échanger des messages unifiés entre les différentes couches/fonctions
/// </summary>
[Serializable()]
public class Message : ISerializable
{
/// <summary>
/// Identification de l'emetteur
/// </summary>
public string sender { get; private set; }
/// <summary>
/// Service demandé
/// </summary>
public string invoke { get; private set; }
/// <summary>
/// Statut de l'opération demandée
/// </summary>
public bool status { get; private set; }
/// <summary>
/// Informations complémentaires
/// </summary>
public string info { get; private set; }
/// <summary>
/// Données à transférer
/// </summary>
public object[] data { get; private set; }
/// <summary>
/// Jeton de sécurité de l'application
/// </summary>
public string token { get; private set; }
/// <summary>
/// Constructeur permettant de construire le message permettant l'échange uniformisé
/// </summary>
/// <param name="sender">Identification de l'emetteur du message</param>
/// <param name="Invoke">Nommage du service demandé</param>
/// <param name="status">Permet au récepteur d'indiquer au destinataire si le traitement de l'opération demandé est un succès ou un échec</param>
/// <param name="info">Informations complémentaires</param>
/// <param name="data">Données à transférer</param>
/// <param name="token">Jeton de sécurité de l'application</param>
public Message(string sender, string invoke, bool status, string info, object[] data, string token)
{
this.sender = sender;
this.invoke = invoke;
this.status = status;
this.info = info;
this.data = data;
this.token = token;
}
/// <summary>
/// Deserialization constructor.
/// </summary>
/// <param name="info"></param>
/// <param name="ctxt"></param>
public Message(SerializationInfo info, StreamingContext ctxt)
{
this.sender = (String)info.GetValue("sender", typeof(string));
this.invoke = (String)info.GetValue("invoke", typeof(string));
this.status = (bool)info.GetValue("status", typeof(bool));
this.info = (String)info.GetValue("info", typeof(string));
this.data = (Object[])info.GetValue("data",typeof(Object[]));
this.token = (String)info.GetValue("token", typeof(string)); ;
}
/// <summary>
/// Serialization function.
/// </summary>
/// <param name="info"></param>
/// <param name="ctxt"></param>
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
//You can use any custom name for your name-value pair. But make sure you
// read the values with the same name. For ex:- If you write EmpId as "EmployeeId"
// then you should read the same with "EmployeeId"
info.AddValue("sender", sender);
info.AddValue("invoke", invoke);
info.AddValue("status", status);
info.AddValue("info", info);
info.AddValue("data", data);
info.AddValue("token", token);
}
}
}
答案 0 :(得分:4)
如果您使用的是WCF,则应实施数据合同。您需要使用属性和类本身的注释来注释数据类,以便WCF可以正确地传递它。
然后在您的服务方法中,传递作为数据协定实现的对象。
然后,WCF将以最有效的方式序列化该对象(例如,SOAP将发送XML,但NetTCP和NamedPipes将发送优化的二进制格式)。请参阅此链接:MSDN Data Contract Tutorial
[DataContract]
public class Message
{
[DataMember]
public string Info {get; set;}
//Other Properties.
}