如何在 WCF 消息中发送自定义对象
我想在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您正在寻找 DataContract :
查看使用数据协定了解更多信息有关 DataContracts 和 WCF 的信息。
编辑
不确定这是否能解决问题,但正如我在对您的评论的回复中指出的那样,采用 XmlObjectSerializer 的 CreateMessage 方法存在重载。关于它的 MSDN 文档相当薄弱,但我认为类似的东西可能会做到这一点:
我还没有测试过这个,但至少它可以让你指出正确的方向。
DataContractSerializer
需要提供一个 DataContract(person
在我的答案的第一部分)。Looks like you're looking for a DataContract:
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:
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).