问题描述
我本来应该是一个简单问题的地方有点麻烦.
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消息的正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!