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中通信模式。