从整个基础架构的层次结构上将,WCF可以分为服务模型层(Service Model Layer)和信道层(Channel Layer)两个层次。服务模型层建立在信道层之上,提供了一个统一的。可扩展的编程模型;而信道层通过信道栈(Channel Stack)实现对消息的传输和处理。

第3章绑定

3.1 信道与信道栈

信道层由绑定创建的信道栈组成,WCF在信道层之上建立服务模型层以提供一个面向对象的应用编程接口。

WCF的服务模型层
信道层创建的信道栈

 

3.1.1 信道与信道栈

对于WCF的信道栈来说,也有两种必需的信道,即传输信道(Transport Channel)和消息编码信道(Message Encoding Channel)。

第3章绑定

WCF采用基于消息的通信方式,所有功能,无论是与业务相关还是与业务无关的,都是通过消息交换来实现的。除了嘴基本的消息编码和传输之外,其他的一些功能也需要通过在消息交换工程中添加一些相应的消息处理操作来实现,典型的功能包括:

事务流转(Transaction Flowing):将从客户端开始的事务流转到服务端,从而将服务的执行纳入该事务。

安全传输(Transfer Security):保证数据包或消息的安全,避免被恶意篡改与窥伺,同时解决客户端和服务身份认证问题

可靠传输(Reliable Messaging):在网络环境稳定的情况下保证数据包或消息的可靠、有序传输。

对绝大部分WS-*协议的支持都是通过在信道栈中添加相应的信道实现的。如图3-2所示,在传输信道和消息编码信道之上,基于WS-Security的信道确保了消息的传输安全;基于WS-RM(WS-Reliable Message)的信道实现了消息的可靠传输;基于WS-AT(WS-Atomic Transaction)的信道实现了分布式的事物支持。

第3章绑定

3.1.2 实例演示:直接通过绑定进行消息通信

 

Receiver代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace Receiver
{
    class Program
    {
        static void Main(string[] args)
        {
            Uri listeningUri = new Uri("http://127.0.0.1:3721/listener");
            Binding binding = new BasicHttpBinding();

            // 创建,开启监听器
            IChannelListener<IReplyChannel> channelListener = binding.BuildChannelListener<IReplyChannel>(listeningUri);
            channelListener.Open();

            // 创建,开启回复信道
            IReplyChannel channel = channelListener.AcceptChannel(TimeSpan.MaxValue);
            channel.Open();

            // 开始监听
            while (true)
            {
                // 接收输入消息
                RequestContext requestContext = channel.ReceiveRequest(TimeSpan.MaxValue);
                Console.WriteLine(requestContext.RequestMessage);

                // 消息回复
                requestContext.Reply(CreateRelyMessage(binding));
            }
        }

        static Message CreateRelyMessage(Binding binding)
        {
            string action = "http://www.artech.com/calculatorservice/AddResponse";
            XNamespace ns = "http://www.artech.com";
            XElement body = new XElement(new XElement(ns + "AddResult",3));
            return Message.CreateMessage(binding.MessageVersion, action, body);
        }
    }
}
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-19
  • 2021-11-10
猜你喜欢
  • 2021-05-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案