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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace System.ServiceModel
{
using System.ComponentModel;
using System.Configuration;
using System.Runtime;
using System.ServiceModel.Channels;
using System.Xml;
using Config = System.ServiceModel.Configuration;
public class NetMsmqBinding : MsmqBindingBase
{
// private BindingElements
BinaryMessageEncodingBindingElement encoding;
NetMsmqSecurity security;
public NetMsmqBinding()
{
Initialize();
this.security = new NetMsmqSecurity();
}
public NetMsmqBinding(string configurationName)
{
Initialize();
this.security = new NetMsmqSecurity();
ApplyConfiguration(configurationName);
}
public NetMsmqBinding(NetMsmqSecurityMode securityMode)
{
if (!NetMsmqSecurityModeHelper.IsDefined(securityMode))
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidEnumArgumentException("mode", (int)securityMode, typeof(NetMsmqSecurityMode)));
Initialize();
this.security = new NetMsmqSecurity(securityMode);
}
NetMsmqBinding(NetMsmqSecurity security)
{
Initialize();
Fx.Assert(security != null, "Invalid (null) NetMsmqSecurity value");
this.security = security;
}
[DefaultValue(MsmqDefaults.QueueTransferProtocol)]
public QueueTransferProtocol QueueTransferProtocol
{
get
{
return (this.transport as MsmqTransportBindingElement).QueueTransferProtocol;
}
set
{
(this.transport as MsmqTransportBindingElement).QueueTransferProtocol = value;
}
}
public XmlDictionaryReaderQuotas ReaderQuotas
{
get { return encoding.ReaderQuotas; }
set
{
if (value == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
value.CopyTo(encoding.ReaderQuotas);
}
}
public NetMsmqSecurity Security
{
get { return this.security; }
set { this.security = value; }
}
public EnvelopeVersion EnvelopeVersion
{
get { return EnvelopeVersion.Soap12; }
}
[DefaultValue(TransportDefaults.MaxBufferPoolSize)]
public long MaxBufferPoolSize
{
get { return transport.MaxBufferPoolSize; }
set
{
transport.MaxBufferPoolSize = value;
}
}
internal int MaxPoolSize
{
get
{
return (transport as MsmqTransportBindingElement).MaxPoolSize;
}
set
{
(transport as MsmqTransportBindingElement).MaxPoolSize = value;
}
}
[DefaultValue(MsmqDefaults.UseActiveDirectory)]
public bool UseActiveDirectory
{
get
{
return (transport as MsmqTransportBindingElement).UseActiveDirectory;
}
set
{
(transport as MsmqTransportBindingElement).UseActiveDirectory = value;
}
}
[EditorBrowsable(EditorBrowsableState.Never)]
public bool ShouldSerializeReaderQuotas()
{
if (this.ReaderQuotas.MaxArrayLength != EncoderDefaults.MaxArrayLength)
{
return true;
}
if (this.ReaderQuotas.MaxBytesPerRead != EncoderDefaults.MaxBytesPerRead)
{
return true;
}
if (this.ReaderQuotas.MaxDepth != EncoderDefaults.MaxDepth)
{
return true;
}
if (this.ReaderQuotas.MaxNameTableCharCount != EncoderDefaults.MaxNameTableCharCount)
{
return true;
}
if (this.ReaderQuotas.MaxStringContentLength != EncoderDefaults.MaxStringContentLength)
{
return true;
}
return false;
}
[EditorBrowsable(EditorBrowsableState.Never)]
public bool ShouldSerializeSecurity()
{
if (this.security.Mode != NetMsmqSecurity.DefaultMode)
{
return true;
}
if (this.security.Transport.MsmqAuthenticationMode != MsmqDefaults.MsmqAuthenticationMode ||
this.security.Transport.MsmqEncryptionAlgorithm != MsmqDefaults.MsmqEncryptionAlgorithm ||
this.security.Transport.MsmqSecureHashAlgorithm != MsmqDefaults.MsmqSecureHashAlgorithm ||
this.security.Transport.MsmqProtectionLevel != MsmqDefaults.MsmqProtectionLevel)
{
return true;
}
if (this.security.Message.AlgorithmSuite != MsmqDefaults.MessageSecurityAlgorithmSuite ||
this.security.Message.ClientCredentialType != MsmqDefaults.DefaultClientCredentialType)
{
return true;
}
return false;
}
void Initialize()
{
transport = new MsmqTransportBindingElement();
encoding = new BinaryMessageEncodingBindingElement();
}
void InitializeFrom(MsmqTransportBindingElement transport, BinaryMessageEncodingBindingElement encoding)
{
// only set properties that have standard binding manifestations: MaxPoolSize *is not* one of them
this.CustomDeadLetterQueue = transport.CustomDeadLetterQueue;
this.DeadLetterQueue = transport.DeadLetterQueue;
this.Durable = transport.Durable;
this.ExactlyOnce = transport.ExactlyOnce;
this.MaxReceivedMessageSize = transport.MaxReceivedMessageSize;
this.ReceiveRetryCount = transport.ReceiveRetryCount;
this.MaxRetryCycles = transport.MaxRetryCycles;
this.ReceiveErrorHandling = transport.ReceiveErrorHandling;
this.RetryCycleDelay = transport.RetryCycleDelay;
this.TimeToLive = transport.TimeToLive;
this.UseSourceJournal = transport.UseSourceJournal;
this.UseMsmqTracing = transport.UseMsmqTracing;
this.ReceiveContextEnabled = transport.ReceiveContextEnabled;
this.QueueTransferProtocol = transport.QueueTransferProtocol;
this.MaxBufferPoolSize = transport.MaxBufferPoolSize;
this.UseActiveDirectory = transport.UseActiveDirectory;
this.ValidityDuration = transport.ValidityDuration;
this.ReaderQuotas = encoding.ReaderQuotas;
}
// check that properties of the HttpTransportBindingElement and
// MessageEncodingBindingElement not exposed as properties on NetMsmqBinding
// match default values of the binding elements
bool IsBindingElementsMatch(MsmqTransportBindingElement transport, MessageEncodingBindingElement encoding)
{
// we do not have to check the transport match here: they always match
if (!this.GetTransport().IsMatch(transport))
return false;
if (!this.encoding.IsMatch(encoding))
return false;
return true;
}
void ApplyConfiguration(string configurationName)
{
Config.NetMsmqBindingCollectionElement section = Config.NetMsmqBindingCollectionElement.GetBindingCollectionElement();
Config.NetMsmqBindingElement element = section.Bindings[configurationName];
if (element == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(
SR.GetString(SR.ConfigInvalidBindingConfigurationName,
configurationName,
Config.ConfigurationStrings.NetMsmqBindingCollectionElementName)));
}
else
{
element.ApplyConfiguration(this);
}
}
public override BindingElementCollection CreateBindingElements()
{ // return collection of BindingElements
BindingElementCollection bindingElements = new BindingElementCollection();
// order of BindingElements is important
// add security
SecurityBindingElement wsSecurity = CreateMessageSecurity();
if (wsSecurity != null)
{
bindingElements.Add(wsSecurity);
}
// add encoding (text or mtom)
bindingElements.Add(encoding);
// add transport
bindingElements.Add(GetTransport());
return bindingElements.Clone();
}
internal static bool TryCreate(BindingElementCollection elements, out Binding binding)
{
binding = null;
if (elements.Count > 3)
return false;
SecurityBindingElement security = null;
BinaryMessageEncodingBindingElement encoding = null;
MsmqTransportBindingElement transport = null;
foreach (BindingElement element in elements)
{
if (element is SecurityBindingElement)
security = element as SecurityBindingElement;
else if (element is TransportBindingElement)
transport = element as MsmqTransportBindingElement;
else if (element is MessageEncodingBindingElement)
encoding = element as BinaryMessageEncodingBindingElement;
else
return false;
}
UnifiedSecurityMode mode;
if (!IsValidTransport(transport, out mode))
return false;
if (encoding == null)
return false;
NetMsmqSecurity netMsmqSecurity;
if (!TryCreateSecurity(security, mode, out netMsmqSecurity))
return false;
NetMsmqBinding netMsmqBinding = new NetMsmqBinding(netMsmqSecurity);
netMsmqBinding.InitializeFrom(transport, encoding);
if (!netMsmqBinding.IsBindingElementsMatch(transport, encoding))
return false;
binding = netMsmqBinding;
return true;
}
SecurityBindingElement CreateMessageSecurity()
{
if (this.security.Mode == NetMsmqSecurityMode.Message || this.security.Mode == NetMsmqSecurityMode.Both)
{
return this.security.CreateMessageSecurity();
}
else
{
return null;
}
}
static bool TryCreateSecurity(SecurityBindingElement sbe, UnifiedSecurityMode mode, out NetMsmqSecurity security)
{
if (sbe != null)
mode &= UnifiedSecurityMode.Message | UnifiedSecurityMode.Both;
else
mode &= ~(UnifiedSecurityMode.Message | UnifiedSecurityMode.Both);
NetMsmqSecurityMode netMsmqSecurityMode = NetMsmqSecurityModeHelper.ToSecurityMode(mode);
Fx.Assert(NetMsmqSecurityModeHelper.IsDefined(netMsmqSecurityMode), string.Format("Invalid NetMsmqSecurityMode value: {0}.", netMsmqSecurityMode.ToString()));
if (NetMsmqSecurity.TryCreate(sbe, netMsmqSecurityMode, out security))
{
return true;
}
return false;
}
MsmqBindingElementBase GetTransport()
{
this.security.ConfigureTransportSecurity(transport);
return transport;
}
static bool IsValidTransport(MsmqTransportBindingElement msmq, out UnifiedSecurityMode mode)
{
mode = (UnifiedSecurityMode)0;
if (msmq == null)
return false;
return NetMsmqSecurity.IsConfiguredTransportSecurity(msmq, out mode);
}
}
}
|