WCF技术内幕 第5章 (11)

EndpointAddress类型

EndpointAddress类型有两种功能:它可以方便地保护目标地址的信息,它是一种序列化WS-Addressing终结点到Message中的方式。

            EndpointAddress address1 = new EndpointAddress("http://www.andersoft.com/anders");
            EndpointAddress address2 = new EndpointAddress(new Uri("http://www.andersoft.com/anders"));

            Console.WriteLine(address1.ToString());            //http://www.andersoft.com/anders
            Console.WriteLine(address2.ToString());            //http://www.andersoft.com/anders

AddressHeader类型

AddressHeader类型是CLR对于WS-Addressing参数的抽象,它简化了在序列化之前,添加参数到Message的工作,同样也包括反序列化之后获得引用参数的值。


序列化EndpointAddress对象

            string urlValue = "http://www.andersoft.com/anders";
            AddressHeader addressHeader = AddressHeader.CreateAddressHeader("ref param");
            EndpointAddress address = new EndpointAddress(new Uri(urlValue), addressHeader);
            Message message = Message.CreateMessage(MessageVersion.Soap12WSAddressingAugust2004, "SomeAction");
            message.Headers.FaultTo = address;
            
            //<s:Envelope xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:s="
            //http://www.w3.org/2003/05/soap-envelope">
            //  <s:Header>
            //    <a:Action s:mustUnderstand="1">SomeAction</a:Action>
            //    <a:FaultTo>
            //      <a:Address>http://www.andersoft.com/anders</a:Address>
            //      <a:ReferenceParameters>
            //        <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">ref
            //param</string>
            //      </a:ReferenceParameters>
            //    </a:FaultTo>
            //  </s:Header>
            //  <s:Body />
            //</s:Envelope>

可以用EndpointAddress的ApplyTo方法把EndpointAddress的值赋给Message。

            string urlValue = "http://www.andersoft.com/anders";
            AddressHeader addressHeader = AddressHeader.CreateAddressHeader("ref param");
            EndpointAddress address = new EndpointAddress(new Uri(urlValue), addressHeader);
            Message message = Message.CreateMessage(MessageVersion.Soap12WSAddressingAugust2004, "SomeAction");
            address.ApplyTo(message);
            
            //<s:Envelope xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:s="
            //http://www.w3.org/2003/05/soap-envelope">
            //  <s:Header>
            //    <a:Action s:mustUnderstand="1">SomeAction</a:Action>
            //    <a:To s:mustUnderstand="1">http://www.andersoft.com/anders</a:To>
            //    <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">ref para
            //m</string>
            //  </s:Header>
            //  <s:Body />
            //</s:Envelope>

复制消息

Message.CreateBufferedCopy为消息实例创建一个缓存模式的消息拷贝。

            Message message = Message.CreateMessage(MessageVersion.Soap12WSAddressingAugust2004, "SomeAction");
            Console.WriteLine(message.State.ToString()); //Created

            MessageBuffer messageBuffer = message.CreateBufferedCopy(Int32.MaxValue); //Copied
            Console.WriteLine(message.State.ToString());

            Message newMessage = messageBuffer.CreateMessage();
            Console.WriteLine(newMessage.State.ToString()); //Created


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值