public abstract class Binding : IDefaultCommunicationTimeouts
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public virtual IChannelListener<TChannel> BuildChannelListener<TChannel>(Uri listenUriBaseAddress, string listenUriRelativeAddress, ListenUriMode listenUriMode, BindingParameterCollection parameters) where TChannel : class, IChannel 2 { 3 this.EnsureInvariants(); 4 BindingContext bindingContext = new BindingContext(new CustomBinding(this), parameters, listenUriBaseAddress, listenUriRelativeAddress, listenUriMode); 5 IChannelListener<TChannel> channelListener = bindingContext.BuildInnerChannelListener<TChannel>(); 6 bindingContext.ValidateBindingElementsConsumed(); 7 this.ValidateSecurityCapabilities(channelListener.GetProperty<ISecurityCapabilities>(), parameters); 8 return channelListener; 9 }
通过当前绑定创建CustomBinding, BindingContext bindingContext = new BindingContext()创建绑定上下文,bindingContext.BuildInnerChannelListener<TChannel>();创建信道监听器管道,返回最上层的信道监听器
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class BindingContext 2 { 3 private CustomBinding binding; 4 private BindingParameterCollection bindingParameters; 5 private Uri listenUriBaseAddress; 6 private ListenUriMode listenUriMode; 7 private string listenUriRelativeAddress; 8 private BindingElementCollection remainingBindingElements; 9 [__DynamicallyInvokable] 10 public CustomBinding Binding 11 { 12 [__DynamicallyInvokable, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] 13 get 14 { 15 return this.binding; 16 } 17 } 18 [__DynamicallyInvokable] 19 public BindingParameterCollection BindingParameters 20 { 21 [__DynamicallyInvokable, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] 22 get 23 { 24 return this.bindingParameters; 25 } 26 } 27 public Uri ListenUriBaseAddress 28 { 29 [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] 30 get 31 { 32 return this.listenUriBaseAddress; 33 } 34 [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] 35 set 36 { 37 this.listenUriBaseAddress = value; 38 } 39 } 40 public ListenUriMode ListenUriMode 41 { 42 [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] 43 get 44 { 45 return this.listenUriMode; 46 } 47 [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] 48 set 49 { 50 this.listenUriMode = value; 51 } 52 } 53 public string ListenUriRelativeAddress 54 { 55 [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] 56 get 57 { 58 return this.listenUriRelativeAddress; 59 } 60 [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] 61 set 62 { 63 this.listenUriRelativeAddress = value; 64 } 65 } 66 [__DynamicallyInvokable] 67 public BindingElementCollection RemainingBindingElements 68 { 69 [__DynamicallyInvokable, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] 70 get 71 { 72 return this.remainingBindingElements; 73 } 74 } 75 [__DynamicallyInvokable] 76 public BindingContext(CustomBinding binding, BindingParameterCollection parameters) : this(binding, parameters, null, string.Empty, ListenUriMode.Explicit) 77 { 78 } 79 public BindingContext(CustomBinding binding, BindingParameterCollection parameters, Uri listenUriBaseAddress, string listenUriRelativeAddress, ListenUriMode listenUriMode) 80 { 81 if (binding == null) 82 { 83 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("binding"); 84 } 85 if (listenUriRelativeAddress == null) 86 { 87 listenUriRelativeAddress = string.Empty; 88 } 89 if (!ListenUriModeHelper.IsDefined(listenUriMode)) 90 { 91 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("listenUriMode")); 92 } 93 this.Initialize(binding, binding.Elements, parameters, listenUriBaseAddress, listenUriRelativeAddress, listenUriMode); 94 } 95 [__DynamicallyInvokable] 96 public IChannelFactory<TChannel> BuildInnerChannelFactory<TChannel>() 97 { 98 return this.RemoveNextElement().BuildChannelFactory<TChannel>(this); 99 } 100 public IChannelListener<TChannel> BuildInnerChannelListener<TChannel>() where TChannel : class, IChannel 101 { 102 return this.RemoveNextElement().BuildChannelListener<TChannel>(this); 103 } 104 [__DynamicallyInvokable] 105 public bool CanBuildInnerChannelFactory<TChannel>() 106 { 107 BindingContext bindingContext = this.Clone(); 108 return bindingContext.RemoveNextElement().CanBuildChannelFactory<TChannel>(bindingContext); 109 } 110 public bool CanBuildInnerChannelListener<TChannel>() where TChannel : class, IChannel 111 { 112 BindingContext bindingContext = this.Clone(); 113 return bindingContext.RemoveNextElement().CanBuildChannelListener<TChannel>(bindingContext); 114 } 115 [__DynamicallyInvokable] 116 public T GetInnerProperty<T>() where T : class 117 { 118 if (this.remainingBindingElements.Count == 0) 119 { 120 return default(T); 121 } 122 BindingContext bindingContext = this.Clone(); 123 return bindingContext.RemoveNextElement().GetProperty<T>(bindingContext); 124 } 125 [__DynamicallyInvokable] 126 public BindingContext Clone() 127 { 128 return new BindingContext(this.binding, this.remainingBindingElements, this.bindingParameters, this.listenUriBaseAddress, this.listenUriRelativeAddress, this.listenUriMode); 129 } 130 private BindingElement RemoveNextElement() 131 { 132 BindingElement bindingElement = this.remainingBindingElements.Remove<BindingElement>(); 133 if (bindingElement != null) 134 { 135 return bindingElement; 136 } 137 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString("NoChannelBuilderAvailable", new object[] 138 { 139 this.binding.Name, 140 this.binding.Namespace 141 }))); 142 } 143 internal void ValidateBindingElementsConsumed() 144 { 145 if (this.RemainingBindingElements.Count != 0) 146 { 147 StringBuilder stringBuilder = new StringBuilder(); 148 foreach (BindingElement current in this.RemainingBindingElements) 149 { 150 if (stringBuilder.Length > 0) 151 { 152 stringBuilder.Append(CultureInfo.CurrentCulture.TextInfo.ListSeparator); 153 stringBuilder.Append(" "); 154 } 155 string text = current.GetType().ToString(); 156 stringBuilder.Append(text.Substring(text.LastIndexOf('.') + 1)); 157 } 158 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString("NotAllBindingElementsBuilt", new object[] 159 { 160 stringBuilder.ToString() 161 }))); 162 } 163 } 164 private BindingContext(CustomBinding binding, BindingElementCollection remainingBindingElements, BindingParameterCollection parameters, Uri listenUriBaseAddress, string listenUriRelativeAddress, ListenUriMode listenUriMode) 165 { 166 this.Initialize(binding, remainingBindingElements, parameters, listenUriBaseAddress, listenUriRelativeAddress, listenUriMode); 167 } 168 private void Initialize(CustomBinding binding, BindingElementCollection remainingBindingElements, BindingParameterCollection parameters, Uri listenUriBaseAddress, string listenUriRelativeAddress, ListenUriMode listenUriMode) 169 { 170 this.binding = binding; 171 this.remainingBindingElements = new BindingElementCollection(remainingBindingElements); 172 this.bindingParameters = new BindingParameterCollection(parameters); 173 this.listenUriBaseAddress = listenUriBaseAddress; 174 this.listenUriRelativeAddress = listenUriRelativeAddress; 175 this.listenUriMode = listenUriMode; 176 } 177 }