如何在 WCF 消息中发送自定义对象

发布于 2024-12-12 01:46:42 字数 568 浏览 0 评论 0原文

我想在 system.servicemodel.Channels.Message 中发送一个自定义对象。就像

public class person
{
    string Id;
    string Name;
}

MessageVersion mv = MessageVersion.Create(Soap12);
String action = "Msg";

Message msg = Message.Create(mv, action, new person());

serviceref.ProcessMsg(msg) // this is my service reference in client

//when i tried to access this in Service like 
person p = msg.GetBody<person>()
//I am getting an serialization exception
//I have the Person class on both client and service side

有人可以帮我找出我的错误吗?

I wanted to send one custom object in system.servicemodel.Channels.Message. Like

public class person
{
    string Id;
    string Name;
}

MessageVersion mv = MessageVersion.Create(Soap12);
String action = "Msg";

Message msg = Message.Create(mv, action, new person());

serviceref.ProcessMsg(msg) // this is my service reference in client

//when i tried to access this in Service like 
person p = msg.GetBody<person>()
//I am getting an serialization exception
//I have the Person class on both client and service side

Can some one please help me in figure out my error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怼怹恏 2024-12-19 01:46:42

看起来您正在寻找 DataContract

using System.Runtime.Serialization;

[DataContract]
public class person
{
    [DataMember]
    string Id;

    [DataMember]
    string Name; 
}

查看使用数据协定了解更多信息有关 DataContracts 和 WCF 的信息。

编辑

不确定这是否能解决问题,但正如我在对您的评论的回复中指出的那样,采用 XmlObjectSerializer 的 CreateMessage 方法存在重载。关于它的 MSDN 文档相当薄弱,但我认为类似的东西可能会做到这一点:

Message msg = Message.Create(mv, action, new person(), new DataContractSerializer(typeof(person)));

我还没有测试过这个,但至少它可以让你指出正确的方向。

DataContractSerializer 需要提供一个 DataContract(person 在我的答案的第一部分)。

Looks like you're looking for a DataContract:

using System.Runtime.Serialization;

[DataContract]
public class person
{
    [DataMember]
    string Id;

    [DataMember]
    string Name; 
}

Check out Using Data Contracts for more information on DataContracts and WCF.

EDIT

Not sure if this will do the trick or not, but as I noted in my response to your comment, there's an overload of the CreateMessage method that takes an XmlObjectSerializer. MSDN docs on it are rather thin, but I think something like this might do it:

Message msg = Message.Create(mv, action, new person(), new DataContractSerializer(typeof(person)));

I haven't tested this, but at the least it may get you pointed in the right direction.

The DataContractSerializer will need to be supplied a DataContract (person in the first part of my answer).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文