首页 > 代码库 > WCF技术内幕 第二章 - 简单的Message

WCF技术内幕 第二章 - 简单的Message

1.契约 - 接口 (客户端和服务端都要认识Message)

1 namespace WCFService2 {3     [ServiceContract(Namespace = "http://wintellect.com/ProcessOrder")]4     public interface IOrderContract5     {6         [OperationContract(Action="urn:SubmitOrder")]7         void SubmitOrder(Message order);8     }9 }

2.服务端实现服务

 1 namespace WCFService 2 { 3     public sealed class MyService : IOrderContract 4     { 5         public void SubmitOrder(Message order) 6         { 7             string fileName = "Order" + order.Headers.MessageId.ToString() + ".xml"; 8             Console.WriteLine("Message ID {0} received", order.Headers.MessageId.ToString()); 9             System.Xml.XmlDictionaryWriter writer = System.Xml.XmlDictionaryWriter.CreateTextWriter(new FileStream(fileName, FileMode.Create));10 11             order.WriteMessage(writer);12             writer.Close();13         }14     }15 }

3.创建服务代理,对请求进行服务

 1 namespace WCFService 2 { 3     class Program 4     { 5         static void Main(string[] args) 6         { 7             WSHttpBinding binding = new WSHttpBinding(SecurityMode.None); 8  9             binding.MessageEncoding = WSMessageEncoding.Text;10 11             Uri addressURI = new Uri(@"http://localhost:4000/Order");12 13             ServiceHost svc = new ServiceHost(typeof(MyService));14             svc.AddServiceEndpoint(typeof(IOrderContract), binding, addressURI);15 16             svc.Open();17             Console.WriteLine("The receiver is ready...");18             Console.ReadLine();19 20             svc.Close();21         }22     }23 }

4.客户端构造Message,发送服务请求

4.1.构造消息体

        public static Message GenerateMessage(Int32 productId, Int32 qty)        {            MemoryStream stream = new MemoryStream();                XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(stream, Encoding.UTF8, false);                writer.WriteStartElement("SubmitOrder");            writer.WriteElementString("ProdID", productId.ToString());              writer.WriteElementString("Qty", qty.ToString());              writer.WriteEndElement();                writer.Flush();              stream.Position = 0;                XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(stream, XmlDictionaryReaderQuotas.Max);                return Message.CreateMessage(MessageVersion.Soap12WSAddressing10, string.Empty, reader);          }

4.2.构造消息头,并创建请求服务代理

namespace WCFClinet{    class Program    {        static void Main(string[] args)        {            EndpointAddress address = new EndpointAddress(@"http://localhost:4000/Order");            WSHttpBinding binding = new WSHttpBinding(SecurityMode.None);            binding.MessageEncoding = WSMessageEncoding.Text;            ChannelFactory<IOrderContract> channel = new ChannelFactory<IOrderContract>(binding, address);            IOrderContract proxy = channel.CreateChannel();            Message msg = null;            for (Int32 i = 0; i < 10; i++)            {                msg = GenerateMessage(i, i);                UniqueId uniqueId = new UniqueId(i.ToString());                msg.Headers.MessageId = uniqueId;                Console.WriteLine("Sending msg # {0}", uniqueId.ToString());                msg.Headers.Action = "urn:SubmitOrder";                proxy.SubmitOrder(msg);            }        }


5.查看服务端接收到的消息(第三个Message)

 

 1 <s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing"> 2     <s:Header> 3         <a:Action s:mustUnderstand="1">urn:SubmitOrder</a:Action> 4         <a:MessageID>3</a:MessageID> 5         <a:ReplyTo> 6             <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address> 7         </a:ReplyTo> 8         <a:To s:mustUnderstand="1">http://localhost:4000/Order</a:To> 9         </s:Header>10     <s:Body>11         <SubmitOrder>12             <ProdID>3</ProdID>13             <Qty>3</Qty>14         </SubmitOrder>15     </s:Body>16 </s:Envelope>

 

WCF技术内幕 第二章 - 简单的Message