我正在尝试使用 C# 在 2 个 .Net 应用程序之间交换数据。
\n\n为了交换通信,我遵循了本教程,该教程介绍了基本的 HTTP 服务,并且它与字符串交换完美配合。
\n\n现在,我想发送我的自定义对象,但是当我重新生成服务时,新方法(又名)Message chat(Message msg)未映射。
这是我的代码:
\n\nusing System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Text;\nusing System.Runtime.Serialization;\nusing System.Runtime.Serialization.Formatters.Binary;\n\nnamespace StairWays.Messaging\n{\n /// <summary>\n /// Classe permettant d\'\xc3\xa9changer des messages unifi\xc3\xa9s entre les diff\xc3\xa9rentes couches/fonctions\n /// </summary>\n [Serializable()]\n public class Message : ISerializable\n {\n /// <summary>\n /// Identification de l\'emetteur\n /// </summary>\n public string sender { get; private set; }\n /// <summary>\n /// Service demand\xc3\xa9\n /// </summary>\n public string invoke { get; private set; }\n /// <summary>\n /// Statut de l\'op\xc3\xa9ration demand\xc3\xa9e\n /// </summary>\n public bool status { get; private set; }\n /// <summary>\n /// Informations compl\xc3\xa9mentaires\n /// </summary>\n public string info { get; private set; }\n /// <summary>\n /// Donn\xc3\xa9es \xc3\xa0 transf\xc3\xa9rer\n /// </summary>\n public object[] data { get; private set; }\n /// <summary>\n /// Jeton de s\xc3\xa9curit\xc3\xa9 de l\'application\n /// </summary>\n public string token { get; private set; }\n /// <summary>\n /// Constructeur permettant de construire le message permettant l\'\xc3\xa9change uniformis\xc3\xa9\n /// </summary>\n /// <param name="sender">Identification de l\'emetteur du message</param>\n /// <param name="Invoke">Nommage du service demand\xc3\xa9</param>\n /// <param name="status">Permet au r\xc3\xa9cepteur d\'indiquer au destinataire si le traitement de l\'op\xc3\xa9ration demand\xc3\xa9 est un succ\xc3\xa8s ou un \xc3\xa9chec</param>\n /// <param name="info">Informations compl\xc3\xa9mentaires</param>\n /// <param name="data">Donn\xc3\xa9es \xc3\xa0 transf\xc3\xa9rer</param>\n /// <param name="token">Jeton de s\xc3\xa9curit\xc3\xa9 de l\'application</param>\n public Message(string sender, string invoke, bool status, string info, object[] data, string token)\n {\n this.sender = sender;\n this.invoke = invoke;\n this.status = status;\n this.info = info;\n this.data = data;\n this.token = token;\n }\n\n /// <summary>\n /// Deserialization constructor.\n /// </summary>\n /// <param name="info"></param>\n /// <param name="ctxt"></param>\n public Message(SerializationInfo info, StreamingContext ctxt)\n {\n this.sender = (String)info.GetValue("sender", typeof(string));\n this.invoke = (String)info.GetValue("invoke", typeof(string));\n this.status = (bool)info.GetValue("status", typeof(bool));\n this.info = (String)info.GetValue("info", typeof(string));\n this.data = (Object[])info.GetValue("data",typeof(Object[]));\n this.token = (String)info.GetValue("token", typeof(string)); ;\n }\n\n /// <summary>\n /// Serialization function.\n /// </summary>\n /// <param name="info"></param>\n /// <param name="ctxt"></param>\n public void GetObjectData(SerializationInfo info, StreamingContext ctxt)\n {\n //You can use any custom name for your name-value pair. But make sure you\n // read the values with the same name. For ex:- If you write EmpId as "EmployeeId"\n // then you should read the same with "EmployeeId"\n info.AddValue("sender", sender);\n info.AddValue("invoke", invoke);\n info.AddValue("status", status);\n info.AddValue("info", info);\n info.AddValue("data", data);\n info.AddValue("token", token);\n }\n\n\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
如果您使用 WCF,则应该实施数据契约。您需要使用属性和类本身的注释来注释您的数据类,以便 WCF 可以正确地传递它。
然后在您的服务方法中,传递作为数据契约实现的对象。
然后,WCF 将以最有效的方式序列化该对象(例如,SOAP 将发送 XML,但 NetTCP 和 NamedPipes 将发送优化的二进制格式)。
请参阅此链接:MSDN 数据契约教程
[DataContract]
public class Message
{
[DataMember]
public string Info {get; set;}
//Other Properties.
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
574 次 |
| 最近记录: |