1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.ServiceModel.Channels
{
using System.ServiceModel.Description;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Text;
public class BindingContext
{
CustomBinding binding;
BindingParameterCollection bindingParameters;
Uri listenUriBaseAddress;
ListenUriMode listenUriMode;
string listenUriRelativeAddress;
BindingElementCollection remainingBindingElements; // kept to ensure each BE builds itself once
public BindingContext(CustomBinding binding, BindingParameterCollection parameters)
: this(binding, parameters, null, string.Empty, ListenUriMode.Explicit)
{
}
public BindingContext(CustomBinding binding, BindingParameterCollection parameters, Uri listenUriBaseAddress, string listenUriRelativeAddress, ListenUriMode listenUriMode)
{
if (binding == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("binding");
}
if (listenUriRelativeAddress == null)
{
listenUriRelativeAddress = string.Empty;
}
if (!ListenUriModeHelper.IsDefined(listenUriMode))
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("listenUriMode"));
}
Initialize(binding, binding.Elements, parameters, listenUriBaseAddress, listenUriRelativeAddress, listenUriMode);
}
BindingContext(CustomBinding binding,
BindingElementCollection remainingBindingElements,
BindingParameterCollection parameters,
Uri listenUriBaseAddress,
string listenUriRelativeAddress,
ListenUriMode listenUriMode)
{
Initialize(binding, remainingBindingElements, parameters, listenUriBaseAddress, listenUriRelativeAddress, listenUriMode);
}
void Initialize(CustomBinding binding,
BindingElementCollection remainingBindingElements,
BindingParameterCollection parameters,
Uri listenUriBaseAddress,
string listenUriRelativeAddress,
ListenUriMode listenUriMode)
{
this.binding = binding;
this.remainingBindingElements = new BindingElementCollection(remainingBindingElements);
this.bindingParameters = new BindingParameterCollection(parameters);
this.listenUriBaseAddress = listenUriBaseAddress;
this.listenUriRelativeAddress = listenUriRelativeAddress;
this.listenUriMode = listenUriMode;
}
public CustomBinding Binding
{
get { return this.binding; }
}
public BindingParameterCollection BindingParameters
{
get { return this.bindingParameters; }
}
public Uri ListenUriBaseAddress
{
get { return this.listenUriBaseAddress; }
set { this.listenUriBaseAddress = value; }
}
public ListenUriMode ListenUriMode
{
get { return this.listenUriMode; }
set { this.listenUriMode = value; }
}
public string ListenUriRelativeAddress
{
get { return this.listenUriRelativeAddress; }
set { this.listenUriRelativeAddress = value; }
}
public BindingElementCollection RemainingBindingElements
{
get { return this.remainingBindingElements; }
}
public IChannelFactory<TChannel> BuildInnerChannelFactory<TChannel>()
{
return this.RemoveNextElement().BuildChannelFactory<TChannel>(this);
}
public IChannelListener<TChannel> BuildInnerChannelListener<TChannel>()
where TChannel : class, IChannel
{
return this.RemoveNextElement().BuildChannelListener<TChannel>(this);
}
public bool CanBuildInnerChannelFactory<TChannel>()
{
BindingContext clone = this.Clone();
return clone.RemoveNextElement().CanBuildChannelFactory<TChannel>(clone);
}
public bool CanBuildInnerChannelListener<TChannel>()
where TChannel : class, IChannel
{
BindingContext clone = this.Clone();
return clone.RemoveNextElement().CanBuildChannelListener<TChannel>(clone);
}
public T GetInnerProperty<T>()
where T : class
{
if (this.remainingBindingElements.Count == 0)
{
return null;
}
else
{
BindingContext clone = this.Clone();
return clone.RemoveNextElement().GetProperty<T>(clone);
}
}
public BindingContext Clone()
{
return new BindingContext(this.binding, this.remainingBindingElements, this.bindingParameters,
this.listenUriBaseAddress, this.listenUriRelativeAddress, this.listenUriMode);
}
BindingElement RemoveNextElement()
{
BindingElement element = this.remainingBindingElements.Remove<BindingElement>();
if (element != null)
return element;
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(
SR.NoChannelBuilderAvailable, this.binding.Name, this.binding.Namespace)));
}
internal void ValidateBindingElementsConsumed()
{
if (this.RemainingBindingElements.Count != 0)
{
StringBuilder builder = new StringBuilder();
foreach (BindingElement bindingElement in this.RemainingBindingElements)
{
if (builder.Length > 0)
{
builder.Append(CultureInfo.CurrentCulture.TextInfo.ListSeparator);
builder.Append(" ");
}
string typeString = bindingElement.GetType().ToString();
builder.Append(typeString.Substring(typeString.LastIndexOf('.') + 1));
}
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.NotAllBindingElementsBuilt, builder.ToString())));
}
}
}
}
|