博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
wcf中的Message类
阅读量:5160 次
发布时间:2019-06-13

本文共 3933 字,大约阅读时间需要 13 分钟。

 

 

客户端->服务端—>客户端

客户端代码:

using (new OperationContextScope(client.InnerChannel))

           {
               Message request = Message.CreateMessage(OperationContext.Current.OutgoingMessageHeaders.MessageVersion,
                   ", 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, ", 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, ", sum);

           return response;

       }

       public Message GetFirst()

       {
           MessageVersion ver = OperationContext.Current.IncomingMessageVersion;
           return Message.CreateMessage(ver, "
       }

       public Message GetData()

       {
           Person p = new Person();
           p.name = "wang";
           p.age = 20;
           MessageVersion ver = OperationContext.Current.IncomingMessageVersion;
           return Message.CreateMessage(ver, ", 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, ", xdr);
       }

       public Message GetDataFault()
       {
           FaultCode fc = new FaultCode("Receiver");
           MessageVersion ver = OperationContext.Current.IncomingMessageVersion;
           return Message.CreateMessage(ver, fc, "Bad data", "
       }

   }

   [DataContract(Namespace = "

   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,
                    ", 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的文章

源码:

转载于:https://www.cnblogs.com/lzhp/p/3503358.html

你可能感兴趣的文章
Pycharm Error loading package list:Status: 403错误解决方法
查看>>
steps/train_sat.sh
查看>>
转:Linux设备树(Device Tree)机制
查看>>
iOS 组件化
查看>>
(转)Tomcat 8 安装和配置、优化
查看>>
(转)Linxu磁盘体系知识介绍及磁盘介绍
查看>>
tkinter布局
查看>>
命令ord
查看>>
Sharepoint 2013搜索服务配置总结(实战)
查看>>
博客盈利请先考虑这七点
查看>>
使用 XMLBeans 进行编程
查看>>
写接口请求类型为get或post的时,参数定义的几种方式,如何用注解(原创)--雷锋...
查看>>
【OpenJ_Bailian - 2287】Tian Ji -- The Horse Racing (贪心)
查看>>
Java网络编程--socket服务器端与客户端讲解
查看>>
List_统计输入数值的各种值
查看>>
学习笔记-KMP算法
查看>>
Timer-triggered memory-to-memory DMA transfer demonstrator
查看>>
跨域问题整理
查看>>
[Linux]文件浏览
查看>>
64位主机64位oracle下装32位客户端ODAC(NFPACS版)
查看>>