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 331 332 333 334 335 336 337 338 339 340 341 342
|
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace System.ServiceModel.Channels
{
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Security;
using System.Xml;
public sealed class OneWayBindingElement : BindingElement,
IPolicyExportExtension
{
ChannelPoolSettings channelPoolSettings;
bool packetRoutable;
int maxAcceptedChannels;
public OneWayBindingElement()
{
this.channelPoolSettings = new ChannelPoolSettings();
this.packetRoutable = OneWayDefaults.PacketRoutable;
this.maxAcceptedChannels = OneWayDefaults.MaxAcceptedChannels;
}
OneWayBindingElement(OneWayBindingElement elementToBeCloned)
: base(elementToBeCloned)
{
this.channelPoolSettings = elementToBeCloned.ChannelPoolSettings.Clone();
this.packetRoutable = elementToBeCloned.PacketRoutable;
this.maxAcceptedChannels = elementToBeCloned.maxAcceptedChannels;
}
public ChannelPoolSettings ChannelPoolSettings
{
get
{
return this.channelPoolSettings;
}
set
{
if (value == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
this.channelPoolSettings = value;
}
}
// server
[DefaultValue(OneWayDefaults.MaxAcceptedChannels)]
public int MaxAcceptedChannels
{
get
{
return this.maxAcceptedChannels;
}
set
{
if (value <= 0)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value", value,
SR.GetString(SR.ValueMustBePositive)));
this.maxAcceptedChannels = value;
}
}
[DefaultValue(OneWayDefaults.PacketRoutable)]
public bool PacketRoutable
{
get
{
return this.packetRoutable;
}
set
{
this.packetRoutable = value;
}
}
public override BindingElement Clone()
{
return new OneWayBindingElement(this);
}
public override IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingContext context)
{
if (context == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
}
if (typeof(TChannel) != typeof(IOutputChannel))
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("TChannel",
SR.GetString(SR.ChannelTypeNotSupported, typeof(TChannel)));
}
// Prefer IDuplexChannel
if (context.CanBuildInnerChannelFactory<IDuplexChannel>())
{
return (IChannelFactory<TChannel>)(object)new DuplexOneWayChannelFactory(this, context);
}
// Prefer IDuplexSessionChannel
if (context.CanBuildInnerChannelFactory<IDuplexSessionChannel>())
{
return (IChannelFactory<TChannel>)(object)new DuplexSessionOneWayChannelFactory(this, context);
}
// Followed by IRequestChannel
if (context.CanBuildInnerChannelFactory<IRequestChannel>())
{
return (IChannelFactory<TChannel>)(object)new RequestOneWayChannelFactory(this, context);
}
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
#pragma warning suppress 56506 // context.Binding will never be null.
new InvalidOperationException(SR.GetString(SR.OneWayInternalTypeNotSupported, context.Binding.Name)));
}
public override IChannelListener<TChannel> BuildChannelListener<TChannel>(BindingContext context)
{
if (context == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
}
if (typeof(TChannel) != typeof(IInputChannel))
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("TChannel",
SR.GetString(SR.ChannelTypeNotSupported, typeof(TChannel)));
}
// Prefer IDuplexChannel
if (context.CanBuildInnerChannelListener<IDuplexChannel>())
{
return (IChannelListener<TChannel>)(object)new DuplexOneWayChannelListener(this, context);
}
// Prefer IDuplexSessionChannel
if (context.CanBuildInnerChannelListener<IDuplexSessionChannel>())
{
return (IChannelListener<TChannel>)(object)new DuplexSessionOneWayChannelListener(this, context);
}
// Followed by IRequestChannel
if (context.CanBuildInnerChannelListener<IReplyChannel>())
{
return (IChannelListener<TChannel>)(object)new ReplyOneWayChannelListener(this, context);
}
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
#pragma warning suppress 56506 // context.Binding will never be null.
new InvalidOperationException(SR.GetString(SR.OneWayInternalTypeNotSupported, context.Binding.Name)));
}
public override bool CanBuildChannelFactory<TChannel>(BindingContext context)
{
if (context == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
}
if (typeof(TChannel) != typeof(IOutputChannel))
{
return false;
}
// we can convert IDuplexChannel
if (context.CanBuildInnerChannelFactory<IDuplexChannel>())
{
return true;
}
// we can convert IDuplexSessionChannel
if (context.CanBuildInnerChannelFactory<IDuplexSessionChannel>())
{
return true;
}
// and also IRequestChannel
if (context.CanBuildInnerChannelFactory<IRequestChannel>())
{
return true;
}
return false;
}
public override bool CanBuildChannelListener<TChannel>(BindingContext context)
{
if (context == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
}
if (typeof(TChannel) != typeof(IInputChannel))
{
return false;
}
// we can convert IDuplexChannel
if (context.CanBuildInnerChannelListener<IDuplexChannel>())
{
return true;
}
// we can convert IDuplexSessionChannel
if (context.CanBuildInnerChannelListener<IDuplexSessionChannel>())
{
return true;
}
// and also IRequestChannel
if (context.CanBuildInnerChannelListener<IReplyChannel>())
{
return true;
}
return false;
}
static MessagePartSpecification oneWaySignedMessageParts;
static MessagePartSpecification OneWaySignedMessageParts
{
get
{
if (oneWaySignedMessageParts == null)
{
MessagePartSpecification tempSignedMessageParts = new MessagePartSpecification(
new XmlQualifiedName(DotNetOneWayStrings.HeaderName, DotNetOneWayStrings.Namespace)
);
tempSignedMessageParts.MakeReadOnly();
oneWaySignedMessageParts = tempSignedMessageParts;
}
return oneWaySignedMessageParts;
}
}
public override T GetProperty<T>(BindingContext context)
{
if (context == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
}
// make sure our Datagram header is signed
if (typeof(T) == typeof(ChannelProtectionRequirements))
{
ChannelProtectionRequirements myRequirements = new ChannelProtectionRequirements();
if (PacketRoutable)
{
myRequirements.IncomingSignatureParts.AddParts(OneWaySignedMessageParts);
myRequirements.OutgoingSignatureParts.AddParts(OneWaySignedMessageParts);
}
ChannelProtectionRequirements innerRequirements = context.GetInnerProperty<ChannelProtectionRequirements>();
if (innerRequirements != null)
{
myRequirements.Add(innerRequirements);
}
return (T)(object)myRequirements;
}
else
{
return context.GetInnerProperty<T>();
}
}
internal override bool IsMatch(BindingElement b)
{
if (b == null)
{
return false;
}
OneWayBindingElement oneWayBindingElement = b as OneWayBindingElement;
if (oneWayBindingElement == null)
{
return false;
}
if (!this.channelPoolSettings.IsMatch(oneWayBindingElement.ChannelPoolSettings))
{
return false;
}
if (this.packetRoutable != oneWayBindingElement.PacketRoutable)
{
return false;
}
if (this.maxAcceptedChannels != oneWayBindingElement.MaxAcceptedChannels)
{
return false;
}
return true;
}
void IPolicyExportExtension.ExportPolicy(MetadataExporter exporter, PolicyConversionContext context)
{
if (context == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
}
if (context.BindingElements != null)
{
OneWayBindingElement oneWay = context.BindingElements.Find<OneWayBindingElement>();
if (oneWay != null)
{
// base assertion
XmlDocument doc = new XmlDocument();
XmlElement assertion = doc.CreateElement(OneWayPolicyConstants.Prefix,
OneWayPolicyConstants.OneWay, OneWayPolicyConstants.Namespace);
if (oneWay.PacketRoutable)
{
// add nested packet routable assertion
XmlElement child = doc.CreateElement(OneWayPolicyConstants.Prefix, OneWayPolicyConstants.PacketRoutable, OneWayPolicyConstants.Namespace);
assertion.AppendChild(child);
}
context.GetBindingAssertions().Add(assertion);
}
}
}
[EditorBrowsable(EditorBrowsableState.Never)]
public bool ShouldSerializeChannelPoolSettings()
{
return this.channelPoolSettings.InternalShouldSerialize();
}
}
}
|