仅获取 WCf 消息的正文 [英] Get Just the Body of a WCf Message
问题描述
我遇到了一个应该很简单的问题.
I'm having a bit of trouble with what should be a simple problem.
我有一个接受 c# 消息类型的服务方法,我只想提取该肥皂消息的正文并使用它来构造一个全新的消息.我不能在 Message 类上使用 GetBody<>()
方法,因为我不知道将正文序列化为什么类型.
I have a service method that takes in a c# Message type and i want to just extract the body of that soap message and use it to construct a completely new message. I can't use the GetBody<>()
method on the Message class as i would not know what type to serialise the body into.
有谁知道如何从消息中提取正文?或者构造一个具有相同正文的新消息,即没有原始消息头等?
Does any one know how to just extract the body from the message? Or construct a new message which has the same body i.e. without the orginal messages header etc?
推荐答案
不是抢占 Yann 的答案,而是为了它的价值,这里有一个完整的示例,将消息正文复制到具有不同操作标头的新消息中.您也可以添加或自定义其他标题作为示例的一部分.我花了太多时间写这篇文章,只是把它扔掉.=)
Not to preempt Yann's answer, but for what it's worth, here's a full example of copying a message body into a new message with a different action header. You could add or customize other headers as a part of the example as well. I spent too much time writing this up to just throw it away. =)
class Program
{
[DataContract]
public class Person
{
[DataMember]
public string FirstName { get; set; }
[DataMember]
public string LastName { get; set; }
public override string ToString()
{
return string.Format("{0}, {1}", LastName, FirstName);
}
}
static void Main(string[] args)
{
var person = new Person { FirstName = "Joe", LastName = "Schmo" };
var message = System.ServiceModel.Channels.Message.CreateMessage(MessageVersion.Default, "action", person);
var reader = message.GetReaderAtBodyContents();
var newMessage = System.ServiceModel.Channels.Message.CreateMessage(MessageVersion.Default, "newAction", reader);
Console.WriteLine(message);
Console.WriteLine();
Console.WriteLine(newMessage);
Console.WriteLine();
Console.WriteLine(newMessage.GetBody<Person>());
Console.ReadLine();
}
}
这篇关于仅获取 WCf 消息的正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!