客户端->服务端—>客户端
客户端代码:
using (new OperationContextScope(client.InnerChannel))
{
Message request = Message.CreateMessage(OperationContext.Current.OutgoingMessageHeaders.MessageVersion,
"http://Microsoft.ServiceModel.Samples/IService1/Sum", values);
请求
Message reply = client.Sum(request);
获取应答结果:
int sum = reply.GetBody<int>();
Console.WriteLine("Sum(1,2,3,4,5) = {0}", sum);
服务端代码:
public Message Sum(Message request)
{
int sum = 0;
string text = "";
//获取消息的body节点
XmlReader body = request.GetReaderAtBodyContents();
while (body.Read())
{
text = body.ReadString().Trim();
if (text.Length > 0)
{
sum += Convert.ToInt32(text, CultureInfo.InvariantCulture);
}
}
body.Close();
Message response = Message.CreateMessage(request.Version, "http://Microsoft.ServiceModel.Samples/IService1/SumResponse", sum);
return response;
}
2.所有服务端代码
public Message Sum(Message request)
{
int sum = 0;
string text = "";
//The body of the message contains a list of numbers which will be read directly using an XmlReader
XmlReader body = request.GetReaderAtBodyContents();
while (body.Read())
{
text = body.ReadString().Trim();
if (text.Length > 0)
{
sum += Convert.ToInt32(text, CultureInfo.InvariantCulture);
}
}
body.Close();
Message response = Message.CreateMessage(request.Version, "http://Microsoft.ServiceModel.Samples/IService1/SumResponse", sum);
return response;
}
public Message GetFirst()
{
MessageVersion ver = OperationContext.Current.IncomingMessageVersion;
return Message.CreateMessage(ver, "http://Microsoft.ServiceModel.Samples/IService1/GetFirstResponse");
}
public Message GetData()
{
Person p = new Person();
p.name = "wang";
p.age = 20;
MessageVersion ver = OperationContext.Current.IncomingMessageVersion;
return Message.CreateMessage(ver, "http://Microsoft.ServiceModel.Samples/IService1/GetDataResponse", p);
}
public Message GetDataStream()
{
FileStream stream = new FileStream(@"myfile.xml", FileMode.Open);
XmlDictionaryReader xdr =
XmlDictionaryReader.CreateTextReader(stream,
new XmlDictionaryReaderQuotas());
MessageVersion ver =
OperationContext.Current.IncomingMessageVersion;
return Message.CreateMessage(ver, "http://Microsoft.ServiceModel.Samples/IService1/GetDataStreamResponse", xdr);
}
public Message GetDataFault()
{
FaultCode fc = new FaultCode("Receiver");
MessageVersion ver = OperationContext.Current.IncomingMessageVersion;
return Message.CreateMessage(ver, fc, "Bad data", "http://Microsoft.ServiceModel.Samples/IService1/GetDataFaultResponse");
}
}
[DataContract(Namespace = "http://schemas.datacontract.org/2004/07/WcfServiceLibrary1")]
public class Person
{
[DataMember]
public string name;
[DataMember]
public int age;
}
所有客户端代码:
int[] values = { 1, 2, 3, 4, 5 };
using (new OperationContextScope(client.InnerChannel))
{
Message request = Message.CreateMessage(OperationContext.Current.OutgoingMessageHeaders.MessageVersion,
"http://Microsoft.ServiceModel.Samples/IService1/Sum", values);
//Console.WriteLine(request.ToString());
Message reply = client.Sum(request);
//Console.WriteLine(reply.ToString());
int sum = reply.GetBody<int>();
Console.WriteLine("Sum(1,2,3,4,5) = {0}", sum);
// demo 1
Message Reply1 = client.GetFirst();
Console.WriteLine(Reply1.ToString());
// demo 2
Message reply2 = client.GetData();
Console.WriteLine(reply2.ToString());
Person p = reply2.GetBody<Person>();
Console.WriteLine(p.name + " " + p.age.ToString());
// demo 3
Message reply3 = client.GetDataStream();
Console.WriteLine(reply3.ToString());
FileStream stream = new FileStream(@"c:log.xml", FileMode.Create);
XmlDictionaryWriter xdw =
XmlDictionaryWriter.CreateTextWriter(stream);
//reply1.WriteBodyContents(xdw);
//reply1.WriteBody(xdw);
reply3.WriteMessage(xdw);
xdw.Flush();
// demo 4
try
{
Message reply1 = client.GetDataFault();
Console.WriteLine(reply1.ToString());
}
catch (FaultException e)
{
Console.WriteLine(e.ToString());
}
Console.WriteLine("=====================Demo5========================");
// demo 5
Message reply5 = client.GetDataStream();
//Copy the message to a buffer.
MessageBuffer mb = reply5.CreateBufferedCopy(65536);
//Log to a file.
FileStream stream1 = new FileStream(@"C:log.xml", FileMode.Append);
mb.WriteMessage(stream1);
stream1.Flush();
Console.WriteLine("=====================Demo6========================");
Message reply6 = client.GetData();
Console.WriteLine(reply6.ToString());
foreach (MessageHeaderInfo mhi in reply6.Headers)
{
Console.WriteLine(mhi.Name);
}
}
注意消息的版本
artech的文章
http://www.cnblogs.com/artech/archive/2009/07/27/1532438.html