了解WCF定制的绑定类型(1)_信道范型支持

     WCF中信道中支持多种类型的信道范型,其中包括单向通信模式(IInputChannel、IOutputChannel)、双工通信模式(IDuplexChannel)和请求响应通信模式(IReplyChannel、IRequestChannel),各个通信模式还有相应的会话支持版本。在这些范型中,为了有效的进行通信,都会有信道监听器(一般是在服务器端,为各个访问的客户端打开一道通信之门)和信道工厂(一般在客户单端,负责建立到服务器的信息传输通道),两者存在一对一或是一对多的关系。

    WCF中本身已经根据不同的环境需求建立了多种定制的绑定类型,这些绑定类型并不是通用类型,其都各自适合某种环境。可以通过绑定实例的CanBuildChannelListener<T>()和CanBuildChannelFactory<T>()方法来判定绑定是否支持某种类型的信道范型。

   可以通过反射方法获取定制的绑定类型适用的信道范型,具体代码如下:

运行环境:VS2010 windows控制台程序 .Net v4.0.30319 【需先引用System.ServiceModel】

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Reflection;

namespace DuServer
{
    class Program
    {
        static void Main(string[] args)
        {            
            Module[] mods = Assembly.LoadWithPartialName("System.ServiceModel").GetModules();
            List<Binding> allBinding=new List<Binding>();
            Console.WriteLine("WCF定制的绑定类型:");
            foreach (Module mod in mods)
            {
                foreach (Type type in mod.GetTypes())
                {
                   if(type.Name.EndsWith("Binding"))
                   {
                       //此处需把CustomBinding排除在外,因为其建立之后必须制定信道范型
                       if (type.IsClass&&!type.IsAbstract&&type.Name!="CustomBinding")
                       {
                           //因要调用绑定的无参构造函数,所以必须先判定绑定类型是否存在无参构造函数
                           var ctor = type.GetConstructor(new Type[] { });
                           if (ctor != null)
                           {
                               allBinding.Add((Binding)Activator.CreateInstance(type));
                               Console.WriteLine("   " + type.Name);      
                           }                                             
                       }
                   }
                }
            }

            Console.WriteLine("各个定制绑定类型对信道范型的支持:");
            foreach (Binding binding in allBinding)
            {
                Console.WriteLine(" " + binding.Name);
                Dictionary<string, bool> result = ChannelsOfBinding(binding);
                foreach (KeyValuePair<string, bool> KeyValue in result)
                {
                    if (KeyValue.Value)
                    {
                        Console.WriteLine("   {0,-8}:  {1}","Listener",KeyValue.Key.ToString());
                    }
                }
                Dictionary<string, bool> factoryResult = ChannelsOfBindingFactory(binding);
                foreach (KeyValuePair<string, bool> KeyValue in factoryResult)
                {
                    if (KeyValue.Value)
                    {
                        Console.WriteLine("   {0,-8}:  {1}","Factory", KeyValue.Key.ToString());
                    }
                }
            }           
        }

        /// <summary>
        /// 获取绑定类型对各个信道监听器的支持
        /// </summary>
        /// <param name="binding"></param>
        /// <returns></returns>
        static Dictionary<string, bool> ChannelsOfBinding(Binding binding)
        {
            Dictionary<string, bool> channelsList = new Dictionary<string, bool>();
            channelsList.Add("IInputChannel", binding.CanBuildChannelListener<IInputChannel>());
            channelsList.Add("IInputSessionChannel", binding.CanBuildChannelListener<IInputSessionChannel>());
            channelsList.Add("IOutputChannel", binding.CanBuildChannelListener<IOutputChannel>());
            channelsList.Add("IOutputSessionChannel", binding.CanBuildChannelListener<IOutputSessionChannel>());
            channelsList.Add("IDuplexChannel", binding.CanBuildChannelListener<IDuplexChannel>());
            channelsList.Add("IDuplexSessionChannel",binding.CanBuildChannelListener<IDuplexSessionChannel>());
            channelsList.Add("IDuplexContextChannel", binding.CanBuildChannelListener<IDuplexContextChannel>());
            channelsList.Add("IReplyChannel",binding.CanBuildChannelListener<IReplyChannel>());
            channelsList.Add("IReplySessionChannel", binding.CanBuildChannelListener<IReplySessionChannel>());
            channelsList.Add("IRequestChannel", binding.CanBuildChannelListener<IRequestChannel>());
            channelsList.Add("IRequestSessionChannel", binding.CanBuildChannelListener<IRequestSessionChannel>());
            return channelsList;

        }

        /// <summary>
        /// 获取绑定类型对各个信道工厂的支持
        /// </summary>
        /// <param name="binding"></param>
        /// <returns></returns>
        static Dictionary<string, bool> ChannelsOfBindingFactory(Binding binding)
        {
            Dictionary<string, bool> channelsList = new Dictionary<string, bool>();
            channelsList.Add("IInputChannel", binding.CanBuildChannelFactory<IInputChannel>());
            channelsList.Add("IInputSessionChannel", binding.CanBuildChannelFactory<IInputSessionChannel>());
            channelsList.Add("IOutputChannel", binding.CanBuildChannelFactory<IOutputChannel>());
            channelsList.Add("IOutputSessionChannel", binding.CanBuildChannelFactory<IOutputSessionChannel>());
            channelsList.Add("IDuplexChannel", binding.CanBuildChannelFactory<IDuplexChannel>());
            channelsList.Add("IDuplexSessionChannel", binding.CanBuildChannelFactory<IDuplexSessionChannel>());
            channelsList.Add("IDuplexContextChannel", binding.CanBuildChannelFactory<IDuplexContextChannel>());
            channelsList.Add("IReplyChannel", binding.CanBuildChannelFactory<IReplyChannel>());
            channelsList.Add("IReplySessionChannel", binding.CanBuildChannelFactory<IReplySessionChannel>());
            channelsList.Add("IRequestChannel", binding.CanBuildChannelFactory<IRequestChannel>());
            channelsList.Add("IRequestSessionChannel", binding.CanBuildChannelFactory<IRequestSessionChannel>());
            return channelsList;

        }
    }
}


运行结果为:

WCF定制的绑定类型:
   BasicHttpBinding
   BasicHttpContextBinding
   NetTcpBinding
   NetTcpContextBinding
   WSHttpBinding
   WSHttpContextBinding
   MsmqIntegrationBinding
   NetMsmqBinding
   NetNamedPipeBinding
   NetPeerTcpBinding
   WSFederationHttpBinding
   WS2007FederationHttpBinding
   WS2007HttpBinding
   WSDualHttpBinding
各个定制绑定类型对信道范型的支持:
 BasicHttpBinding
   Listener:  IReplyChannel
   Factory :  IRequestChannel
 BasicHttpContextBinding
   Listener:  IReplyChannel
   Factory :  IRequestChannel
 NetTcpBinding
   Listener:  IDuplexSessionChannel
   Factory :  IDuplexSessionChannel
 NetTcpContextBinding
   Listener:  IDuplexSessionChannel
   Factory :  IDuplexSessionChannel
 WSHttpBinding
   Listener:  IReplySessionChannel
   Factory :  IRequestSessionChannel
 WSHttpContextBinding
   Listener:  IReplySessionChannel
   Factory :  IRequestSessionChannel
 MsmqIntegrationBinding
   Listener:  IInputChannel
   Factory :  IOutputChannel
 NetMsmqBinding
   Listener:  IInputChannel
   Listener:  IInputSessionChannel
   Factory :  IOutputChannel
   Factory :  IOutputSessionChannel
 NetNamedPipeBinding
   Listener:  IDuplexSessionChannel
   Factory :  IDuplexSessionChannel
 NetPeerTcpBinding
   Listener:  IInputChannel
   Listener:  IDuplexChannel
   Factory :  IOutputChannel
   Factory :  IDuplexChannel
 WSFederationHttpBinding
   Listener:  IReplySessionChannel
   Factory :  IRequestSessionChannel
 WS2007FederationHttpBinding
   Listener:  IReplySessionChannel
   Factory :  IRequestSessionChannel
 WS2007HttpBinding
   Listener:  IReplySessionChannel
   Factory :  IRequestSessionChannel
 WSDualHttpBinding
   Listener:  IInputSessionChannel
   Listener:  IDuplexSessionChannel
   Factory :  IOutputSessionChannel
   Factory :  IDuplexSessionChannel
请按任意键继续. . .


可以看到其中支持的信道范型都是成对出现的,每种绑定基本上支持1-2中通信模式。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章