如何在.net应用程序之间交换对象

本文关键字:之间 交换 对象 应用程序 net | 更新日期: 2023-09-27 18:14:42

我正在尝试用c#在两个。net应用程序之间交换数据。

为了交换通信,我遵循了这个教程,它介绍了基本的HTTP服务,它可以完美地与字符串交换。

现在,我想发送我的自定义对象,但当我重新生成服务时,新方法aka 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);
        }

    }
}

如何在.net应用程序之间交换对象

如果你正在使用WCF,你应该实现一个数据契约。您需要用属性和类本身的注释来注释您的数据类,以便WCF可以正确地传递它。

然后在你的服务方法中,传递作为数据契约实现的对象。

WCF将以最有效的方式序列化该对象(例如SOAP将发送XML,但netcp和NamedPipes将发送优化的二进制格式)。

查看此链接:MSDN数据合同教程

[DataContract]
public class Message
{
    [DataMember]
    public string Info {get; set;}
    //Other Properties.
}