wcf中的Message类
客户端->服务端—>客户端
客户端代码:
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
源码:http://files.cnblogs.com/lzhp/WcfMessageService.zip
wcf中的Message类的更多相关文章
- WCF初探-22:WCF中使用Message类(上)
前言 从我们学习WCF以来,就一直强调WCF是基于消息的通信机制.但是由于WCF给我们做了高级封装,以至于我们在使用WCF的时候很少了解到消息的内部机制.由于WCF的架构的可扩展性,针对一些特殊情况, ...
- WCF初探-23:WCF中使用Message类(下)
前言 在上一篇WCF中使用Message类(上)中,文章介绍了WCF中使用Message类的基本知识和怎样创建消息,本文是承接上一篇文章,如果想要更好的阅读本文,请先阅读上一篇文章.在这篇文章中,我将 ...
- WCF初探-25:WCF中使用XmlSerializer类
前言 在上一篇WCF序列化和反序列化中,文章介绍了WCF序列化和反序列化的机制,虽然WCF针对序列化提供了默认的DataContractSerializer序列化引擎,但是WCF还支持其他的序列化引擎 ...
- WCF基础之Message类
客户端和服务端的通信都是通过接收和发送的Message实例建立起来的,大多数情况我们通过服务协定.数据协定和消息协定来构造传入和传出消息的. 一般什么时候使用Message类呢?不需要将消息序列化或者 ...
- WCF中,通过C#代码或App.config配置文件创建ServiceHost类
C# static void Main(string[] args) { //创建宿主的基地址 Uri baseAddress = new Uri("http://localhost:808 ...
- 在 WCF 中使用高效的 BinaryFormatter 序列化
本文将定义一个 WCF 终结点行为扩展,以在 WCF 中使用更高效的 BinaryFormatter 进行二进制序列化,并实现对是否使用传统二进制序列化功能的可配置. 介绍 实现步骤 使用方法 效果 ...
- WCF初探-28:WCF中的并发
理解WCF中的并发机制 在对WCF并发机制进行理解时,必须对WCF初探-27:WCF中的实例化进行理解,因为WCF中的并发特点是伴随着服务实例上下文实现的.WCF的实例上下文模型可以通过Instanc ...
- WCF初探-27:WCF中的实例化
理解WCF中的实例化机制 “实例化”是指对用户定义的服务对象以及与其相关的 InstanceContext 对象的生存期的控制.也就是说我们的客户端程序在调用服务端方法时,需要实例化一个服务端代理类对 ...
- WCF初探-26:WCF中的会话
理解WCF中的会话机制 在WCF应用程序中,会话将一组消息相互关联,从而形成对话.会话”是在两个终结点之间发送的所有消息的一种相互关系.当某个服务协定指定它需要会话时,该协定会指定所有调用(即,支持调 ...
随机推荐
- 【HTML】网页中如何让DIV在网页滚动到特定位置时出现
用js或者jquery比较好实现.但你要知道,滚动到哪个特定位置,例如滚动到一个标题h3那显示这个div,那么可以用jquery算这个h3距离网页顶部的距离:$("h3").off ...
- struts2 标签变形和 样式class无效 问题解决方法
在jsp使用Struts2标签的时候会发现,出现严重变形问题. <s:textfield type="text" name="username" labe ...
- C# Log4Net level优先级别
原文地址:https://blog.csdn.net/pukuimin1226/article/details/51819388?locationNum=2&fps=1 Level定义记录的日 ...
- 测试Js权限
'12222' 测试一下 刚兴趣的可以参考: http://www.cnblogs.com/littledu/archive/2011/05/08/2040298.html http://www.cn ...
- 高效的MySQL分页——利用子查询分页
——先抄回来~~~ 首先看一下分页的基本原理: mysql> explain SELECT * FROM message ORDER BY id DESC LIMIT 10000, 20G*** ...
- SimpleAdapter真不简单!
作为一名编程初学者,我总是认为自己什么都不会,什么都不行,就算实现了文档指定的功能,我永远都是觉得自己写过的代码实在是太烂了,它只是恰巧能够运行而已!它只是在运行的时候恰巧没有发现错误而已!!一直都是 ...
- Python 文件 fileno() 方法
描述 Python 文件 fileno() 方法返回一个整型的文件描述符(file descriptor FD 整型),可用于底层操作系统的 I/O 操作. 语法 fileno() 方法语法如下: f ...
- CustomValidator控件用法
虽然大部分时间一直从事asp.net的开发,对于一些常用的asp.net服务器端验证控件及它们的组合使用比较熟悉,如:CompareValidator ——比较验证控件RangeValidator — ...
- 并发测试 JavaDemo
https://github.com/oldratlee/fucking-java-concurrency /** * @author Jerry Lee */ public class Testee ...
- python 2 python3 共存
步骤: 1.安装python3 并添加环境变量2.修改python3 目录下文件名:修改python.exe 为python3.exe, 修改pythonw.exe 为pythonw3.exe C:\ ...