I'm trying to exchange data between 2 .Net applications with c#.
To exchange communication I followed this tutorial which introduces basic HTTP service and it works perfectly with String exchange.
Now, i want to send my custom object but when I regenerate services, the new method aka Message chat(Message msg)
is not mapped.
Here's my code :
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);
}
}
}
if you are using WCF you should implement a data contract. You need to annotate your data class with annotations on the properties and the class itself so that WCF can correctly pass it.
Then in your service method, pass the objects which are implemented as data contracts.
WCF will then serialise this object in the most efficient way possible (e.g. SOAP would send XML, but NetTCP and NamedPipes would send an optimised binary format).
See this link: MSDN Data Contract Tutorial
[DataContract]
public class Message
{
[DataMember]
public string Info {get; set;}
//Other Properties.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With