• wcf中的Message类


    image

    image

    image

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

    客户端代码:

    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

  • 相关阅读:
    missing requires of libmysqlclient.so.18()(64bit)
    Ambari安装HDP问题:User root is not allowed to impersonate anonymous.User: hcat is not allowed to impersonate ambari-qa
    ambari2.6.50 openssl 版本问题:SSLError: Failed to connect. Please check openssl library versions. Openssl error upon host registration
    HDP 2.6 requires libtirpc-devel
    Kafka 如何读取offset topic内容 (__consumer_offsets)
    Linux集群时间同步方法
    centos7 ambari2.6.1.5+hdp2.6.4.0 大数据集群安装部署
    Centos7.3离线(rpm方式)安装mysql服务
    ubuntu安装rpm的方法
    kerberos环境storm配置:Running Apache Storm Securely
  • 原文地址:https://www.cnblogs.com/lzhp/p/3503358.html
Copyright © 2020-2023  润新知