| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Collections.ObjectModel; |
| | | 7 | | using System.ComponentModel; |
| | | 8 | | using System.IO; |
| | | 9 | | using System.Linq; |
| | | 10 | | using System.Reflection; |
| | | 11 | | using CoreWCF.Channels; |
| | | 12 | | using CoreWCF.Collections.Generic; |
| | | 13 | | using CoreWCF.Dispatcher; |
| | | 14 | | using CoreWCF.Web; |
| | | 15 | | |
| | | 16 | | namespace CoreWCF.Description |
| | | 17 | | { |
| | | 18 | | public class WebHttpBehavior : IEndpointBehavior |
| | | 19 | | { |
| | | 20 | | internal const string GET = "GET"; |
| | | 21 | | internal const string POST = "POST"; |
| | | 22 | | internal const string WildcardAction = "*"; |
| | | 23 | | internal const string WildcardMethod = "*"; |
| | 1 | 24 | | internal static readonly string s_defaultStreamContentType = "application/octet-stream"; |
| | 1 | 25 | | internal static readonly string s_defaultCallbackParameterName = "callback"; |
| | | 26 | | private WebMessageBodyStyle _defaultBodyStyle; |
| | | 27 | | private WebMessageFormat _defaultOutgoingReplyFormat; |
| | | 28 | | private WebMessageFormat _defaultOutgoingRequestFormat; |
| | | 29 | | private string _contractNamespace; |
| | | 30 | | private readonly UnwrappedTypesXmlSerializerManager _xmlSerializerManager; |
| | | 31 | | |
| | 37 | 32 | | public WebHttpBehavior(IServiceProvider serviceProvider) |
| | | 33 | | { |
| | 37 | 34 | | _defaultOutgoingRequestFormat = WebMessageFormat.Xml; |
| | 37 | 35 | | _defaultOutgoingReplyFormat = WebMessageFormat.Xml; |
| | 37 | 36 | | _defaultBodyStyle = WebMessageBodyStyle.Bare; |
| | 37 | 37 | | _xmlSerializerManager = new UnwrappedTypesXmlSerializerManager(); |
| | | 38 | | |
| | 37 | 39 | | ServiceProvider = serviceProvider; |
| | 37 | 40 | | } |
| | | 41 | | |
| | 0 | 42 | | internal IServiceProvider ServiceProvider { get; } |
| | | 43 | | |
| | | 44 | | internal delegate void Effect(); |
| | | 45 | | |
| | | 46 | | public virtual WebMessageBodyStyle DefaultBodyStyle |
| | | 47 | | { |
| | 649 | 48 | | get { return _defaultBodyStyle; } |
| | | 49 | | set |
| | | 50 | | { |
| | 0 | 51 | | if (!WebMessageBodyStyleHelper.IsDefined(value)) |
| | | 52 | | { |
| | 0 | 53 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 54 | | } |
| | | 55 | | |
| | 0 | 56 | | _defaultBodyStyle = value; |
| | 0 | 57 | | } |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | public virtual WebMessageFormat DefaultOutgoingRequestFormat |
| | | 61 | | { |
| | | 62 | | get |
| | | 63 | | { |
| | 0 | 64 | | return _defaultOutgoingRequestFormat; |
| | | 65 | | } |
| | | 66 | | set |
| | | 67 | | { |
| | 0 | 68 | | if (!WebMessageFormatHelper.IsDefined(value)) |
| | | 69 | | { |
| | 0 | 70 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 71 | | } |
| | | 72 | | |
| | 0 | 73 | | _defaultOutgoingRequestFormat = value; |
| | 0 | 74 | | } |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | public virtual WebMessageFormat DefaultOutgoingResponseFormat |
| | | 78 | | { |
| | | 79 | | get |
| | | 80 | | { |
| | 55 | 81 | | return _defaultOutgoingReplyFormat; |
| | | 82 | | } |
| | | 83 | | set |
| | | 84 | | { |
| | 0 | 85 | | if (!WebMessageFormatHelper.IsDefined(value)) |
| | | 86 | | { |
| | 0 | 87 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 88 | | } |
| | | 89 | | |
| | 0 | 90 | | _defaultOutgoingReplyFormat = value; |
| | 0 | 91 | | } |
| | | 92 | | } |
| | | 93 | | |
| | 148 | 94 | | public virtual bool HelpEnabled { get; set; } |
| | | 95 | | |
| | 37 | 96 | | public virtual bool AutomaticFormatSelectionEnabled { get; set; } |
| | | 97 | | |
| | 37 | 98 | | public virtual bool FaultExceptionEnabled { get; set; } |
| | | 99 | | |
| | 41 | 100 | | internal Uri HelpUri { get; set; } |
| | | 101 | | |
| | | 102 | | public virtual void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) |
| | | 103 | | { |
| | | 104 | | // do nothing |
| | 37 | 105 | | } |
| | | 106 | | |
| | | 107 | | public virtual void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) |
| | | 108 | | { |
| | 0 | 109 | | if (endpoint == null) |
| | | 110 | | { |
| | 0 | 111 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(endpoint)); |
| | | 112 | | } |
| | | 113 | | |
| | 0 | 114 | | if (clientRuntime == null) |
| | | 115 | | { |
| | 0 | 116 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(clientRuntime)); |
| | | 117 | | } |
| | | 118 | | |
| | 0 | 119 | | WebMessageEncodingBindingElement webEncodingBindingElement = endpoint.Binding.CreateBindingElements().Find<W |
| | 0 | 120 | | if (webEncodingBindingElement != null && webEncodingBindingElement.CrossDomainScriptAccessEnabled) |
| | | 121 | | { |
| | 0 | 122 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.CrossDomainJavasc |
| | | 123 | | } |
| | | 124 | | |
| | 0 | 125 | | _contractNamespace = endpoint.Contract.Namespace; |
| | 0 | 126 | | } |
| | | 127 | | |
| | | 128 | | public virtual void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) |
| | | 129 | | { |
| | 37 | 130 | | if (endpoint == null) |
| | | 131 | | { |
| | 0 | 132 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(endpoint)); |
| | | 133 | | } |
| | | 134 | | |
| | 37 | 135 | | if (endpointDispatcher == null) |
| | | 136 | | { |
| | 0 | 137 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(endpointDispatcher)); |
| | | 138 | | } |
| | | 139 | | |
| | 37 | 140 | | WebMessageEncodingBindingElement webEncodingBindingElement = endpoint.Binding.CreateBindingElements().Find<W |
| | 37 | 141 | | if (webEncodingBindingElement != null && webEncodingBindingElement.CrossDomainScriptAccessEnabled) |
| | | 142 | | { |
| | 0 | 143 | | ISecurityCapabilities securityCapabilities = endpoint.Binding.GetProperty<ISecurityCapabilities>(new Bin |
| | 0 | 144 | | if (securityCapabilities.SupportsClientAuthentication) |
| | | 145 | | { |
| | 0 | 146 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.CrossDomainJa |
| | | 147 | | } |
| | | 148 | | } |
| | | 149 | | |
| | 37 | 150 | | WebHttpServiceModelCompat.ServiceModelAttributeFixup(endpoint); |
| | | 151 | | |
| | 37 | 152 | | if (HelpEnabled) |
| | | 153 | | { |
| | 0 | 154 | | HelpUri = new UriTemplate(HelpPage.OperationListHelpPageUriTemplate).BindByPosition(endpoint.ListenUri); |
| | | 155 | | } |
| | | 156 | | |
| | 37 | 157 | | _contractNamespace = endpoint.Contract.Namespace; |
| | | 158 | | |
| | | 159 | | // endpoint filter |
| | 37 | 160 | | endpointDispatcher.AddressFilter = new PrefixEndpointAddressMessageFilter(endpoint.Address); |
| | 37 | 161 | | endpointDispatcher.ContractFilter = new MatchAllMessageFilter(); |
| | | 162 | | // operation selector |
| | 37 | 163 | | endpointDispatcher.DispatchRuntime.OperationSelector = GetOperationSelector(endpoint); |
| | | 164 | | // unhandled operation |
| | 37 | 165 | | string actionStarOperationName = null; |
| | 400 | 166 | | foreach (OperationDescription od in endpoint.Contract.Operations) |
| | | 167 | | { |
| | 163 | 168 | | if (od.Messages[0].Direction == MessageDirection.Input |
| | 163 | 169 | | && od.Messages[0].Action == WildcardAction) |
| | | 170 | | { |
| | 0 | 171 | | actionStarOperationName = od.Name; |
| | 0 | 172 | | break; |
| | | 173 | | } |
| | | 174 | | } |
| | | 175 | | |
| | 37 | 176 | | if (actionStarOperationName != null) |
| | | 177 | | { |
| | | 178 | | // WCF v1 installs any Action="*" op into UnhandledDispatchOperation, but WebHttpBehavior |
| | | 179 | | // doesn't want this, so we 'move' that operation back into normal set of operations |
| | 0 | 180 | | endpointDispatcher.DispatchRuntime.Operations.Add( |
| | 0 | 181 | | endpointDispatcher.DispatchRuntime.UnhandledDispatchOperation); |
| | | 182 | | } |
| | | 183 | | |
| | 37 | 184 | | FormatSelectingMessageInspector formatSelectingMessageInspector = null; |
| | | 185 | | string jsonContentType; |
| | | 186 | | string xmlContentType; |
| | | 187 | | |
| | 37 | 188 | | if (webEncodingBindingElement != null) |
| | | 189 | | { |
| | 37 | 190 | | XmlFormatMapping xmlFormatMapping = new XmlFormatMapping(webEncodingBindingElement.WriteEncoding, webEnc |
| | 37 | 191 | | JsonFormatMapping jsonFormatMapping = new JsonFormatMapping(webEncodingBindingElement.WriteEncoding, web |
| | | 192 | | |
| | 37 | 193 | | xmlContentType = xmlFormatMapping.DefaultContentType.ToString(); |
| | 37 | 194 | | jsonContentType = jsonFormatMapping.DefaultContentType.ToString(); |
| | | 195 | | |
| | 37 | 196 | | if (AutomaticFormatSelectionEnabled) |
| | | 197 | | { |
| | 0 | 198 | | formatSelectingMessageInspector = new FormatSelectingMessageInspector(this, new List<MultiplexingFor |
| | 0 | 199 | | endpointDispatcher.DispatchRuntime.MessageInspectors.Add(formatSelectingMessageInspector); |
| | | 200 | | } |
| | | 201 | | } |
| | | 202 | | else |
| | | 203 | | { |
| | 0 | 204 | | xmlContentType = ContentTypeHelpers.GetContentType(XmlFormatMapping.s_defaultMediaType, TextEncoderDefau |
| | 0 | 205 | | jsonContentType = JsonMessageEncoderFactory.GetContentType(null); |
| | | 206 | | } |
| | | 207 | | |
| | | 208 | | // always install UnhandledDispatchOperation (WebHttpDispatchOperationSelector may choose not to use it) |
| | 37 | 209 | | endpointDispatcher.DispatchRuntime.UnhandledDispatchOperation = new DispatchOperation(endpointDispatcher.Dis |
| | 37 | 210 | | endpointDispatcher.DispatchRuntime.UnhandledDispatchOperation.DeserializeRequest = false; |
| | 37 | 211 | | endpointDispatcher.DispatchRuntime.UnhandledDispatchOperation.SerializeReply = false; |
| | 37 | 212 | | endpointDispatcher.DispatchRuntime.UnhandledDispatchOperation.Invoker = new HttpUnhandledOperationInvoker { |
| | | 213 | | |
| | | 214 | | // install formatters and parameter inspectors |
| | 400 | 215 | | foreach (OperationDescription od in endpoint.Contract.Operations) |
| | | 216 | | { |
| | 163 | 217 | | DispatchOperation dop = null; |
| | 163 | 218 | | if (endpointDispatcher.DispatchRuntime.Operations.Contains(od.Name)) |
| | | 219 | | { |
| | 163 | 220 | | dop = endpointDispatcher.DispatchRuntime.Operations[od.Name]; |
| | | 221 | | } |
| | 0 | 222 | | else if (endpointDispatcher.DispatchRuntime.UnhandledDispatchOperation.Name == od.Name) |
| | | 223 | | { |
| | 0 | 224 | | dop = endpointDispatcher.DispatchRuntime.UnhandledDispatchOperation; |
| | | 225 | | } |
| | | 226 | | |
| | 163 | 227 | | if (dop != null) |
| | | 228 | | { |
| | 163 | 229 | | IDispatchMessageFormatter requestDispatch = GetRequestDispatchFormatter(od, endpoint); |
| | 163 | 230 | | IDispatchMessageFormatter replyDispatch = GetReplyDispatchFormatter(od, endpoint); |
| | | 231 | | |
| | | 232 | | |
| | 163 | 233 | | if (replyDispatch is MultiplexingDispatchMessageFormatter replyDispatchAsMultiplexing) |
| | | 234 | | { |
| | | 235 | | // here we are adding all default content types, despite the fact that |
| | | 236 | | // some of the formatters in MultiplexingDispatchMessageFormatter might not be present |
| | | 237 | | // i.e. the JSON formatter |
| | | 238 | | |
| | 160 | 239 | | replyDispatchAsMultiplexing.DefaultContentTypes.Add(WebMessageFormat.Xml, xmlContentType); |
| | 160 | 240 | | replyDispatchAsMultiplexing.DefaultContentTypes.Add(WebMessageFormat.Json, jsonContentType); |
| | | 241 | | |
| | 160 | 242 | | if (formatSelectingMessageInspector != null) |
| | | 243 | | { |
| | 0 | 244 | | formatSelectingMessageInspector.RegisterOperation(od.Name, replyDispatchAsMultiplexing); |
| | | 245 | | } |
| | | 246 | | } |
| | | 247 | | |
| | 163 | 248 | | dop.Formatter = new CompositeDispatchFormatter(requestDispatch, replyDispatch); |
| | 163 | 249 | | dop.FaultFormatter = new WebFaultFormatter(dop.FaultFormatter); |
| | 163 | 250 | | dop.DeserializeRequest = (requestDispatch != null); |
| | 163 | 251 | | dop.SerializeReply = od.Messages.Count > 1 && (replyDispatch != null); |
| | | 252 | | } |
| | | 253 | | } |
| | | 254 | | |
| | 37 | 255 | | if (HelpEnabled) |
| | | 256 | | { |
| | 0 | 257 | | HelpPage helpPage = new HelpPage(); |
| | 0 | 258 | | DispatchOperation dispatchOperation = new DispatchOperation(endpointDispatcher.DispatchRuntime, HelpOper |
| | 0 | 259 | | { |
| | 0 | 260 | | DeserializeRequest = false, |
| | 0 | 261 | | SerializeReply = false, |
| | 0 | 262 | | Invoker = new HelpOperationInvoker(helpPage), |
| | 0 | 263 | | }; |
| | 0 | 264 | | endpointDispatcher.DispatchRuntime.Operations.Add(dispatchOperation); |
| | | 265 | | } |
| | | 266 | | |
| | 37 | 267 | | AddServerErrorHandlers(endpoint, endpointDispatcher); |
| | 37 | 268 | | } |
| | | 269 | | |
| | | 270 | | public virtual void Validate(ServiceEndpoint endpoint) |
| | | 271 | | { |
| | 37 | 272 | | if (endpoint == null) |
| | | 273 | | { |
| | 0 | 274 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(endpoint)); |
| | | 275 | | } |
| | | 276 | | |
| | 37 | 277 | | ValidateNoMessageHeadersPresent(endpoint); |
| | 37 | 278 | | ValidateBinding(endpoint); |
| | 37 | 279 | | ValidateContract(endpoint); |
| | 37 | 280 | | } |
| | | 281 | | |
| | | 282 | | private void ValidateNoMessageHeadersPresent(ServiceEndpoint endpoint) |
| | | 283 | | { |
| | 37 | 284 | | if (endpoint == null || endpoint.Address == null) |
| | | 285 | | { |
| | 0 | 286 | | return; |
| | | 287 | | } |
| | | 288 | | |
| | 37 | 289 | | EndpointAddress address = endpoint.Address; |
| | 37 | 290 | | if (address.Headers.Count > 0) |
| | | 291 | | { |
| | 0 | 292 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Web |
| | | 293 | | } |
| | 37 | 294 | | } |
| | | 295 | | |
| | | 296 | | protected virtual void ValidateBinding(ServiceEndpoint endpoint) |
| | | 297 | | { |
| | 37 | 298 | | ValidateIsWebHttpBinding(endpoint, GetType().ToString()); |
| | 37 | 299 | | } |
| | | 300 | | |
| | | 301 | | internal static string GetWebMethod(OperationDescription od) |
| | | 302 | | { |
| | 529 | 303 | | WebGetAttribute wga = ((KeyedByTypeCollection<IOperationBehavior>)od.OperationBehaviors).Find<WebGetAttribut |
| | 529 | 304 | | WebInvokeAttribute wia = ((KeyedByTypeCollection<IOperationBehavior>)od.OperationBehaviors).Find<WebInvokeAt |
| | 529 | 305 | | EnsureOk(wga, wia, od); |
| | 529 | 306 | | if (wga != null) |
| | | 307 | | { |
| | 454 | 308 | | return GET; |
| | | 309 | | } |
| | 75 | 310 | | else if (wia != null) |
| | | 311 | | { |
| | 75 | 312 | | return wia.Method ?? POST; |
| | | 313 | | } |
| | | 314 | | else |
| | | 315 | | { |
| | 0 | 316 | | return POST; |
| | | 317 | | } |
| | | 318 | | } |
| | | 319 | | |
| | | 320 | | internal static string GetWebUriTemplate(OperationDescription od) |
| | | 321 | | { |
| | | 322 | | // return exactly what is on the attribute |
| | 529 | 323 | | WebGetAttribute wga = ((KeyedByTypeCollection<IOperationBehavior>)od.OperationBehaviors).Find<WebGetAttribut |
| | 529 | 324 | | WebInvokeAttribute wia = ((KeyedByTypeCollection<IOperationBehavior>)od.OperationBehaviors).Find<WebInvokeAt |
| | 529 | 325 | | EnsureOk(wga, wia, od); |
| | 529 | 326 | | if (wga != null) |
| | | 327 | | { |
| | 454 | 328 | | return wga.UriTemplate; |
| | | 329 | | } |
| | 75 | 330 | | else if (wia != null) |
| | | 331 | | { |
| | 75 | 332 | | return wia.UriTemplate; |
| | | 333 | | } |
| | | 334 | | else |
| | | 335 | | { |
| | 0 | 336 | | return null; |
| | | 337 | | } |
| | | 338 | | } |
| | | 339 | | |
| | | 340 | | internal static string GetDescription(OperationDescription od) |
| | | 341 | | { |
| | 0 | 342 | | object[] attributes = null; |
| | 0 | 343 | | if (od.SyncMethod != null) |
| | | 344 | | { |
| | 0 | 345 | | attributes = od.SyncMethod.GetCustomAttributes(typeof(DescriptionAttribute), true); |
| | | 346 | | } |
| | 0 | 347 | | else if (od.BeginMethod != null) |
| | | 348 | | { |
| | 0 | 349 | | attributes = od.BeginMethod.GetCustomAttributes(typeof(DescriptionAttribute), true); |
| | | 350 | | } |
| | 0 | 351 | | else if (od.TaskMethod != null) |
| | | 352 | | { |
| | 0 | 353 | | attributes = od.TaskMethod.GetCustomAttributes(typeof(DescriptionAttribute), true); |
| | | 354 | | } |
| | | 355 | | |
| | 0 | 356 | | if (attributes != null && attributes.Length > 0) |
| | | 357 | | { |
| | 0 | 358 | | return ((DescriptionAttribute)attributes[0]).Description; |
| | | 359 | | } |
| | | 360 | | else |
| | | 361 | | { |
| | 0 | 362 | | return string.Empty; |
| | | 363 | | } |
| | | 364 | | } |
| | | 365 | | |
| | 2113 | 366 | | internal static bool IsTypedMessage(MessageDescription message) => message != null && message.MessageType != nul |
| | | 367 | | |
| | | 368 | | internal static bool IsUntypedMessage(MessageDescription message) |
| | | 369 | | { |
| | 1759 | 370 | | if (message == null) |
| | | 371 | | { |
| | 0 | 372 | | return false; |
| | | 373 | | } |
| | | 374 | | |
| | 1759 | 375 | | return (message.Body.ReturnValue != null && message.Body.Parts.Count == 0 && message.Body.ReturnValue.Type = |
| | 1759 | 376 | | (message.Body.ReturnValue == null && message.Body.Parts.Count == 1 && message.Body.Parts[0].Type == type |
| | | 377 | | } |
| | | 378 | | |
| | | 379 | | internal static MessageDescription MakeDummyMessageDescription(MessageDirection direction) |
| | | 380 | | { |
| | 163 | 381 | | MessageDescription messageDescription = new MessageDescription("urn:dummyAction", direction); |
| | 163 | 382 | | return messageDescription; |
| | | 383 | | } |
| | | 384 | | |
| | | 385 | | internal static bool SupportsJsonFormat(OperationDescription od) |
| | | 386 | | { |
| | | 387 | | // if the type is XmlSerializable, then we cannot create a json serializer for it |
| | 218 | 388 | | DataContractSerializerOperationBehavior dcsob = ((KeyedByTypeCollection<IOperationBehavior>)od.OperationBeha |
| | 218 | 389 | | return (dcsob != null); |
| | | 390 | | } |
| | | 391 | | |
| | | 392 | | internal static void ValidateIsWebHttpBinding(ServiceEndpoint serviceEndpoint, string behaviorName) |
| | | 393 | | { |
| | 37 | 394 | | Binding binding = serviceEndpoint.Binding; |
| | 37 | 395 | | if (binding.Scheme != "http" && binding.Scheme != "https") |
| | | 396 | | { |
| | 0 | 397 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 398 | | SR.Format(SR.WCFBindingCannotBeUsedWithUriOperationSelectorBehaviorBadScheme, |
| | 0 | 399 | | serviceEndpoint.Contract.Name, behaviorName))); |
| | | 400 | | } |
| | | 401 | | |
| | 37 | 402 | | if (binding.MessageVersion != MessageVersion.None) |
| | | 403 | | { |
| | 0 | 404 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 405 | | SR.Format(SR.WCFBindingCannotBeUsedWithUriOperationSelectorBehaviorBadMessageVersion, |
| | 0 | 406 | | serviceEndpoint.Address.Uri.AbsoluteUri, behaviorName))); |
| | | 407 | | } |
| | | 408 | | |
| | 37 | 409 | | TransportBindingElement transportBindingElement = binding.CreateBindingElements().Find<TransportBindingEleme |
| | 37 | 410 | | if (transportBindingElement != null && !transportBindingElement.ManualAddressing) |
| | | 411 | | { |
| | 0 | 412 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 413 | | SR.Format(SR.ManualAddressingCannotBeFalseWithTransportBindingElement, |
| | 0 | 414 | | serviceEndpoint.Address.Uri.AbsoluteUri, behaviorName, transportBindingElement.GetType().Name))); |
| | | 415 | | } |
| | 37 | 416 | | } |
| | | 417 | | |
| | | 418 | | internal WebMessageBodyStyle GetBodyStyle(OperationDescription od) |
| | | 419 | | { |
| | 649 | 420 | | WebGetAttribute wga = ((KeyedByTypeCollection<IOperationBehavior>)od.OperationBehaviors).Find<WebGetAttribut |
| | 649 | 421 | | WebInvokeAttribute wia = ((KeyedByTypeCollection<IOperationBehavior>)od.OperationBehaviors).Find<WebInvokeAt |
| | 649 | 422 | | EnsureOk(wga, wia, od); |
| | 649 | 423 | | if (wga != null) |
| | | 424 | | { |
| | 552 | 425 | | return wga.GetBodyStyleOrDefault(DefaultBodyStyle); |
| | | 426 | | } |
| | 97 | 427 | | else if (wia != null) |
| | | 428 | | { |
| | 97 | 429 | | return wia.GetBodyStyleOrDefault(DefaultBodyStyle); |
| | | 430 | | } |
| | | 431 | | else |
| | | 432 | | { |
| | 0 | 433 | | return DefaultBodyStyle; |
| | | 434 | | } |
| | | 435 | | } |
| | | 436 | | |
| | | 437 | | protected virtual void AddServerErrorHandlers(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) |
| | | 438 | | { |
| | 37 | 439 | | if (!FaultExceptionEnabled) |
| | | 440 | | { |
| | 37 | 441 | | WebErrorHandler errorHandler = new WebErrorHandler(this, endpoint.Contract, endpointDispatcher.DispatchR |
| | 37 | 442 | | endpointDispatcher.DispatchRuntime.ChannelDispatcher.ErrorHandlers.Add(errorHandler); |
| | | 443 | | } |
| | 37 | 444 | | } |
| | | 445 | | |
| | 37 | 446 | | protected virtual WebHttpDispatchOperationSelector GetOperationSelector(ServiceEndpoint endpoint) => new WebHttp |
| | | 447 | | |
| | 326 | 448 | | protected virtual QueryStringConverter GetQueryStringConverter(OperationDescription operationDescription) => new |
| | | 449 | | |
| | | 450 | | internal virtual bool UseBareReplyFormatter(WebMessageBodyStyle style, OperationDescription operationDescription |
| | | 451 | | { |
| | 160 | 452 | | parameterType = null; |
| | | 453 | | |
| | 160 | 454 | | return IsBareResponse(style) && TryGetNonMessageParameterType(operationDescription.Messages[1], operationDes |
| | | 455 | | } |
| | | 456 | | |
| | | 457 | | protected virtual IDispatchMessageFormatter GetReplyDispatchFormatter(OperationDescription operationDescription, |
| | | 458 | | { |
| | 163 | 459 | | if (operationDescription.Messages.Count < 2) |
| | | 460 | | { |
| | 0 | 461 | | return null; |
| | | 462 | | } |
| | | 463 | | |
| | 163 | 464 | | ValidateBodyParameters(operationDescription, false); |
| | 163 | 465 | | WebMessageFormat responseFormat = GetResponseFormat(operationDescription); |
| | | 466 | | |
| | | 467 | | // Determine if we should add a json formatter; If the ResponseFormat is json, we always add the json forma |
| | | 468 | | // operation is XmlSerializerFormat because the formatter constructor throws the exception: "json not valid |
| | 163 | 469 | | bool useJson = (responseFormat == WebMessageFormat.Json || SupportsJsonFormat(operationDescription)); |
| | | 470 | | |
| | | 471 | | IDispatchMessageFormatter innerFormatter; |
| | | 472 | | |
| | 163 | 473 | | if (TryGetStreamParameterType(operationDescription.Messages[1], operationDescription, false, out Type type)) |
| | | 474 | | { |
| | 3 | 475 | | innerFormatter = new ContentTypeSettingDispatchMessageFormatter(s_defaultStreamContentType, new HttpStre |
| | | 476 | | } |
| | 160 | 477 | | else if (IsUntypedMessage(operationDescription.Messages[1])) |
| | | 478 | | { |
| | 0 | 479 | | innerFormatter = new MessagePassthroughFormatter(); |
| | | 480 | | } |
| | | 481 | | else |
| | | 482 | | { |
| | 160 | 483 | | WebMessageBodyStyle style = GetBodyStyle(operationDescription); |
| | 160 | 484 | | Dictionary<WebMessageFormat, IDispatchMessageFormatter> formatters = new Dictionary<WebMessageFormat, ID |
| | | 485 | | |
| | 160 | 486 | | if (UseBareReplyFormatter(style, operationDescription, responseFormat, out Type parameterType)) |
| | | 487 | | { |
| | 160 | 488 | | formatters.Add(WebMessageFormat.Xml, SingleBodyParameterMessageFormatter.CreateDispatchFormatter(ope |
| | 160 | 489 | | if (useJson) |
| | | 490 | | { |
| | | 491 | | //formatters.Add(WebMessageFormat.Json, SingleBodyParameterMessageFormatter.CreateDispatchFormat |
| | 160 | 492 | | formatters.Add(WebMessageFormat.Json, SingleBodyParameterMessageFormatter.CreateDispatchFormatte |
| | | 493 | | } |
| | | 494 | | } |
| | | 495 | | else |
| | | 496 | | { |
| | 0 | 497 | | MessageDescription temp = operationDescription.Messages[0]; |
| | 0 | 498 | | operationDescription.Messages[0] = MakeDummyMessageDescription(MessageDirection.Input); |
| | 0 | 499 | | formatters.Add(WebMessageFormat.Xml, GetDefaultDispatchFormatter(operationDescription, false, !IsBar |
| | 0 | 500 | | if (useJson) |
| | | 501 | | { |
| | 0 | 502 | | formatters.Add(WebMessageFormat.Json, GetDefaultDispatchFormatter(operationDescription, true, !I |
| | | 503 | | } |
| | 0 | 504 | | operationDescription.Messages[0] = temp; |
| | | 505 | | } |
| | 160 | 506 | | innerFormatter = new MultiplexingDispatchMessageFormatter(formatters, responseFormat); |
| | | 507 | | } |
| | | 508 | | |
| | 163 | 509 | | return innerFormatter; |
| | | 510 | | } |
| | | 511 | | |
| | | 512 | | protected virtual IDispatchMessageFormatter GetRequestDispatchFormatter(OperationDescription operationDescriptio |
| | | 513 | | { |
| | 163 | 514 | | IDispatchMessageFormatter result = null; |
| | | 515 | | // get some validation errors by creating "throwAway" formatter |
| | 163 | 516 | | UriTemplateDispatchFormatter throwAway = new UriTemplateDispatchFormatter(operationDescription, null, GetQue |
| | 163 | 517 | | int numUriVariables = throwAway._pathMapping.Count + throwAway._queryMapping.Count; |
| | 163 | 518 | | HideReplyMessage(operationDescription, delegate () |
| | 163 | 519 | | { |
| | 163 | 520 | | WebMessageBodyStyle style = GetBodyStyle(operationDescription); |
| | 163 | 521 | | Effect doBodyFormatter = delegate () |
| | 163 | 522 | | { |
| | 163 | 523 | | if (numUriVariables != 0) |
| | 163 | 524 | | { |
| | 40 | 525 | | EnsureNotUntypedMessageNorMessageContract(operationDescription); |
| | 163 | 526 | | } |
| | 163 | 527 | | |
| | 163 | 528 | | // get body formatter |
| | 163 | 529 | | ValidateBodyParameters(operationDescription, true); |
| | 163 | 530 | | if (TryGetStreamParameterType(operationDescription.Messages[0], operationDescription, true, out Type |
| | 163 | 531 | | { |
| | 3 | 532 | | result = new HttpStreamFormatter(operationDescription); |
| | 163 | 533 | | } |
| | 163 | 534 | | else |
| | 163 | 535 | | { |
| | 160 | 536 | | if (UseBareRequestFormatter(style, operationDescription, out Type parameterType)) |
| | 163 | 537 | | { |
| | 163 | 538 | | // TODO: Can I remove this? |
| | 163 | 539 | | //result = SingleBodyParameterMessageFormatter.CreateXmlAndJsonDispatchFormatter(operationDe |
| | 160 | 540 | | result = SingleBodyParameterMessageFormatter.CreateXmlAndJsonDispatchFormatter(operationDesc |
| | 163 | 541 | | } |
| | 163 | 542 | | else |
| | 163 | 543 | | { |
| | 0 | 544 | | result = GetDefaultXmlAndJsonDispatchFormatter(operationDescription, !IsBareRequest(style)); |
| | 163 | 545 | | } |
| | 163 | 546 | | } |
| | 163 | 547 | | }; |
| | 163 | 548 | | |
| | 163 | 549 | | if (numUriVariables == 0) |
| | 163 | 550 | | { |
| | 123 | 551 | | if (IsUntypedMessage(operationDescription.Messages[0])) |
| | 163 | 552 | | { |
| | 0 | 553 | | ValidateBodyParameters(operationDescription, true); |
| | 0 | 554 | | result = new MessagePassthroughFormatter(); |
| | 163 | 555 | | } |
| | 123 | 556 | | else if (IsTypedMessage(operationDescription.Messages[0])) |
| | 163 | 557 | | { |
| | 0 | 558 | | ValidateBodyParameters(operationDescription, true); |
| | 0 | 559 | | result = GetDefaultXmlAndJsonDispatchFormatter(operationDescription, !IsBareRequest(style)); |
| | 163 | 560 | | } |
| | 163 | 561 | | else |
| | 163 | 562 | | { |
| | 123 | 563 | | doBodyFormatter(); |
| | 163 | 564 | | } |
| | 163 | 565 | | } |
| | 163 | 566 | | else |
| | 163 | 567 | | { |
| | 40 | 568 | | HideRequestUriTemplateParameters(operationDescription, throwAway, delegate () |
| | 40 | 569 | | { |
| | 40 | 570 | | CloneMessageDescriptionsBeforeActing(operationDescription, delegate () |
| | 40 | 571 | | { |
| | 40 | 572 | | doBodyFormatter(); |
| | 80 | 573 | | }); |
| | 80 | 574 | | }); |
| | 163 | 575 | | } |
| | 163 | 576 | | |
| | 163 | 577 | | result = new UriTemplateDispatchFormatter(operationDescription, result, GetQueryStringConverter(operatio |
| | 326 | 578 | | }); |
| | | 579 | | |
| | 163 | 580 | | return result; |
| | | 581 | | } |
| | | 582 | | |
| | | 583 | | private static void CloneMessageDescriptionsBeforeActing(OperationDescription operationDescription, Effect effec |
| | | 584 | | { |
| | 40 | 585 | | MessageDescription originalRequest = operationDescription.Messages[0]; |
| | 40 | 586 | | bool thereIsAReply = operationDescription.Messages.Count > 1; |
| | 40 | 587 | | MessageDescription originalReply = thereIsAReply ? operationDescription.Messages[1] : null; |
| | 40 | 588 | | operationDescription.Messages[0] = originalRequest.Clone(); |
| | 40 | 589 | | if (thereIsAReply) |
| | | 590 | | { |
| | 40 | 591 | | operationDescription.Messages[1] = originalReply.Clone(); |
| | | 592 | | } |
| | | 593 | | |
| | 40 | 594 | | effect(); |
| | 40 | 595 | | operationDescription.Messages[0] = originalRequest; |
| | 40 | 596 | | if (thereIsAReply) |
| | | 597 | | { |
| | 40 | 598 | | operationDescription.Messages[1] = originalReply; |
| | | 599 | | } |
| | 40 | 600 | | } |
| | | 601 | | |
| | | 602 | | internal virtual bool UseBareRequestFormatter(WebMessageBodyStyle style, OperationDescription operationDescripti |
| | | 603 | | { |
| | 160 | 604 | | parameterType = null; |
| | | 605 | | |
| | 160 | 606 | | return IsBareRequest(style) && TryGetNonMessageParameterType(operationDescription.Messages[0], operationDesc |
| | | 607 | | } |
| | | 608 | | |
| | | 609 | | private static Collection<MessagePartDescription> CloneParts(MessageDescription md) |
| | | 610 | | { |
| | 80 | 611 | | MessagePartDescriptionCollection bodyParameters = md.Body.Parts; |
| | 80 | 612 | | Collection<MessagePartDescription> bodyParametersClone = new Collection<MessagePartDescription>(); |
| | 352 | 613 | | for (int i = 0; i < bodyParameters.Count; ++i) |
| | | 614 | | { |
| | 96 | 615 | | MessagePartDescription copy = bodyParameters[i].Clone(); |
| | 96 | 616 | | bodyParametersClone.Add(copy); |
| | | 617 | | } |
| | | 618 | | |
| | 80 | 619 | | return bodyParametersClone; |
| | | 620 | | } |
| | | 621 | | |
| | | 622 | | private static void EnsureNotUntypedMessageNorMessageContract(OperationDescription operationDescription) |
| | | 623 | | { |
| | | 624 | | // Called when there are UriTemplate parameters. UT does not compose with Message |
| | | 625 | | // or MessageContract because the SOAP and REST programming models must be uniform here. |
| | 40 | 626 | | bool isUnadornedWebGet = false; |
| | 40 | 627 | | if (GetWebMethod(operationDescription) == GET && GetWebUriTemplate(operationDescription) == null) |
| | | 628 | | { |
| | 0 | 629 | | isUnadornedWebGet = true; |
| | | 630 | | } |
| | | 631 | | |
| | 40 | 632 | | if (IsTypedMessage(operationDescription.Messages[0])) |
| | | 633 | | { |
| | 0 | 634 | | if (isUnadornedWebGet) |
| | | 635 | | { |
| | | 636 | | // WebGet will give you UriTemplate parameters by default. |
| | | 637 | | // We need a special error message for this case to prevent confusion. |
| | 0 | 638 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 639 | | SR.Format(SR.GETCannotHaveMCParameter, operationDescription.Name, operationDescription.Declaring |
| | | 640 | | } |
| | | 641 | | else |
| | | 642 | | { |
| | 0 | 643 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format( |
| | 0 | 644 | | SR.UTParamsDoNotComposeWithMessageContract, operationDescription.Name, operationDescription.Decl |
| | | 645 | | } |
| | | 646 | | } |
| | | 647 | | |
| | 40 | 648 | | if (IsUntypedMessage(operationDescription.Messages[0])) |
| | | 649 | | { |
| | 0 | 650 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format( |
| | 0 | 651 | | SR.UTParamsDoNotComposeWithMessage, operationDescription.Name, operationDescription.DeclaringContrac |
| | | 652 | | } |
| | 40 | 653 | | } |
| | | 654 | | |
| | | 655 | | private static void EnsureOk(WebGetAttribute wga, WebInvokeAttribute wia, OperationDescription od) |
| | | 656 | | { |
| | 1870 | 657 | | if (wga != null && wia != null) |
| | | 658 | | { |
| | 0 | 659 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 660 | | SR.Format(SR.MultipleWebAttributes, od.Name, od.DeclaringContract.Name))); |
| | | 661 | | } |
| | 1870 | 662 | | } |
| | | 663 | | |
| | | 664 | | private static void HideReplyMessage(OperationDescription operationDescription, Effect effect) |
| | | 665 | | { |
| | 163 | 666 | | MessageDescription temp = null; |
| | 163 | 667 | | if (operationDescription.Messages.Count > 1) |
| | | 668 | | { |
| | 163 | 669 | | temp = operationDescription.Messages[1]; |
| | 163 | 670 | | operationDescription.Messages[1] = MakeDummyMessageDescription(MessageDirection.Output); |
| | | 671 | | } |
| | | 672 | | |
| | 163 | 673 | | effect(); |
| | 163 | 674 | | if (operationDescription.Messages.Count > 1) |
| | | 675 | | { |
| | 163 | 676 | | operationDescription.Messages[1] = temp; |
| | | 677 | | } |
| | 163 | 678 | | } |
| | | 679 | | |
| | | 680 | | internal static void HideRequestUriTemplateParameters(OperationDescription operationDescription, UriTemplateDisp |
| | | 681 | | { |
| | 40 | 682 | | HideRequestUriTemplateParameters(operationDescription, throwAway._pathMapping, throwAway._queryMapping, effe |
| | 40 | 683 | | } |
| | | 684 | | |
| | | 685 | | private static void HideRequestUriTemplateParameters(OperationDescription operationDescription, Dictionary<int, |
| | | 686 | | { |
| | | 687 | | // mutate description to hide UriTemplate parameters |
| | 40 | 688 | | Collection<MessagePartDescription> originalParts = CloneParts(operationDescription.Messages[0]); |
| | 40 | 689 | | Collection<MessagePartDescription> parts = CloneParts(operationDescription.Messages[0]); |
| | 40 | 690 | | operationDescription.Messages[0].Body.Parts.Clear(); |
| | 40 | 691 | | int newIndex = 0; |
| | 176 | 692 | | for (int i = 0; i < parts.Count; ++i) |
| | | 693 | | { |
| | 48 | 694 | | if (!pathMapping.ContainsKey(i) && !queryMapping.ContainsKey(i)) |
| | | 695 | | { |
| | 0 | 696 | | operationDescription.Messages[0].Body.Parts.Add(parts[i]); |
| | 0 | 697 | | parts[i].Index = newIndex++; |
| | | 698 | | } |
| | | 699 | | } |
| | | 700 | | |
| | 40 | 701 | | effect(); |
| | | 702 | | // unmutate description |
| | 40 | 703 | | operationDescription.Messages[0].Body.Parts.Clear(); |
| | 176 | 704 | | for (int i = 0; i < originalParts.Count; ++i) |
| | | 705 | | { |
| | 48 | 706 | | operationDescription.Messages[0].Body.Parts.Add(originalParts[i]); |
| | | 707 | | } |
| | 40 | 708 | | } |
| | | 709 | | |
| | 323 | 710 | | private static bool IsBareRequest(WebMessageBodyStyle style) => style == WebMessageBodyStyle.Bare || style == We |
| | | 711 | | |
| | 323 | 712 | | private static bool IsBareResponse(WebMessageBodyStyle style) => style == WebMessageBodyStyle.Bare || style == W |
| | | 713 | | |
| | | 714 | | internal static bool TryGetNonMessageParameterType(MessageDescription message, OperationDescription declaringOpe |
| | | 715 | | { |
| | 646 | 716 | | type = null; |
| | 646 | 717 | | if (message == null) |
| | | 718 | | { |
| | 0 | 719 | | return true; |
| | | 720 | | } |
| | | 721 | | |
| | 646 | 722 | | if (IsTypedMessage(message) || IsUntypedMessage(message)) |
| | | 723 | | { |
| | 0 | 724 | | return false; |
| | | 725 | | } |
| | | 726 | | |
| | 646 | 727 | | if (isRequest) |
| | | 728 | | { |
| | 323 | 729 | | if (message.Body.Parts.Count > 1) |
| | | 730 | | { |
| | 0 | 731 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR |
| | | 732 | | } |
| | | 733 | | |
| | 323 | 734 | | if (message.Body.Parts.Count == 1 && message.Body.Parts[0].Type != typeof(void)) |
| | | 735 | | { |
| | 31 | 736 | | type = message.Body.Parts[0].Type; |
| | | 737 | | } |
| | | 738 | | |
| | 323 | 739 | | return true; |
| | | 740 | | } |
| | | 741 | | else |
| | | 742 | | { |
| | 323 | 743 | | if (message.Body.Parts.Count > 0) |
| | | 744 | | { |
| | 0 | 745 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR |
| | | 746 | | } |
| | | 747 | | |
| | 323 | 748 | | if (message.Body.ReturnValue != null && message.Body.ReturnValue.Type != typeof(void)) |
| | | 749 | | { |
| | 247 | 750 | | type = message.Body.ReturnValue.Type; |
| | | 751 | | } |
| | | 752 | | |
| | 323 | 753 | | return true; |
| | | 754 | | } |
| | | 755 | | } |
| | | 756 | | |
| | | 757 | | private static bool TryGetStreamParameterType(MessageDescription message, OperationDescription declaringOperatio |
| | | 758 | | { |
| | 652 | 759 | | type = null; |
| | 652 | 760 | | if (message == null || IsTypedMessage(message) || IsUntypedMessage(message)) |
| | | 761 | | { |
| | 0 | 762 | | return false; |
| | | 763 | | } |
| | | 764 | | |
| | 652 | 765 | | if (isRequest) |
| | | 766 | | { |
| | 326 | 767 | | bool hasStream = false; |
| | 708 | 768 | | for (int i = 0; i < message.Body.Parts.Count; ++i) |
| | | 769 | | { |
| | 34 | 770 | | if (typeof(Stream) == message.Body.Parts[i].Type) |
| | | 771 | | { |
| | 6 | 772 | | type = message.Body.Parts[i].Type; |
| | 6 | 773 | | hasStream = true; |
| | 6 | 774 | | break; |
| | | 775 | | } |
| | | 776 | | |
| | | 777 | | } |
| | | 778 | | |
| | 326 | 779 | | if (hasStream && message.Body.Parts.Count > 1) |
| | | 780 | | { |
| | 0 | 781 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.Format(SR.AtMostO |
| | | 782 | | } |
| | | 783 | | |
| | 326 | 784 | | return hasStream; |
| | | 785 | | } |
| | | 786 | | else |
| | | 787 | | { |
| | | 788 | | // validate that the stream is not an out or ref param |
| | 652 | 789 | | for (int i = 0; i < message.Body.Parts.Count; ++i) |
| | | 790 | | { |
| | 0 | 791 | | if (typeof(Stream) == message.Body.Parts[i].Type) |
| | | 792 | | { |
| | 0 | 793 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.Format(SR.NoO |
| | | 794 | | } |
| | | 795 | | } |
| | | 796 | | |
| | 326 | 797 | | if (message.Body.ReturnValue != null && typeof(Stream) == message.Body.ReturnValue.Type) |
| | | 798 | | { |
| | | 799 | | // validate that there are no out or ref params |
| | 6 | 800 | | if (message.Body.Parts.Count > 0) |
| | | 801 | | { |
| | 0 | 802 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.Format(SR.NoO |
| | | 803 | | } |
| | 6 | 804 | | type = message.Body.ReturnValue.Type; |
| | 6 | 805 | | return true; |
| | | 806 | | } |
| | | 807 | | |
| | | 808 | | else |
| | | 809 | | { |
| | 320 | 810 | | return false; |
| | | 811 | | } |
| | | 812 | | } |
| | | 813 | | } |
| | | 814 | | |
| | | 815 | | private static void ValidateAtMostOneStreamParameter(OperationDescription operation, bool request) |
| | | 816 | | { |
| | | 817 | | Type dummy; |
| | 326 | 818 | | if (request) |
| | | 819 | | { |
| | 163 | 820 | | TryGetStreamParameterType(operation.Messages[0], operation, true, out dummy); |
| | | 821 | | } |
| | | 822 | | else |
| | | 823 | | { |
| | 163 | 824 | | if (operation.Messages.Count > 1) |
| | | 825 | | { |
| | 163 | 826 | | TryGetStreamParameterType(operation.Messages[1], operation, false, out dummy); |
| | | 827 | | } |
| | | 828 | | } |
| | 163 | 829 | | } |
| | | 830 | | |
| | | 831 | | private IDispatchMessageFormatter GetDefaultDispatchFormatter(OperationDescription od, bool useJson, bool isWrap |
| | | 832 | | { |
| | 0 | 833 | | DataContractSerializerOperationBehavior dcsob = ((KeyedByTypeCollection<IOperationBehavior>)od.OperationBeha |
| | 0 | 834 | | if (useJson) |
| | | 835 | | { |
| | 0 | 836 | | if (dcsob == null) |
| | | 837 | | { |
| | 0 | 838 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR |
| | | 839 | | } |
| | 0 | 840 | | return CreateDataContractJsonSerializerOperationFormatter(od, dcsob, isWrapped); |
| | | 841 | | } |
| | | 842 | | else |
| | | 843 | | { |
| | 0 | 844 | | EndpointDispatcher dummyED = new EndpointDispatcher(new EndpointAddress("http://localhost/"), "name", "" |
| | 0 | 845 | | DispatchRuntime dispatchRuntime = dummyED.DispatchRuntime; |
| | 0 | 846 | | DispatchOperation dop = new DispatchOperation(dispatchRuntime, "dummyDispatch", "urn:dummy"); |
| | 0 | 847 | | dop.Formatter = null; |
| | | 848 | | |
| | 0 | 849 | | if (dcsob != null) |
| | | 850 | | { |
| | 0 | 851 | | (dcsob as IOperationBehavior).ApplyDispatchBehavior(od, dop); |
| | 0 | 852 | | return dop.Formatter; |
| | | 853 | | } |
| | | 854 | | |
| | 0 | 855 | | XmlSerializerOperationBehavior xsob = ((KeyedByTypeCollection<IOperationBehavior>)od.OperationBehaviors) |
| | 0 | 856 | | if (xsob != null) |
| | | 857 | | { |
| | 0 | 858 | | xsob = new XmlSerializerOperationBehavior(od, xsob.XmlSerializerFormatAttribute, _contractNamespace) |
| | 0 | 859 | | (xsob as IOperationBehavior).ApplyDispatchBehavior(od, dop); |
| | 0 | 860 | | return dop.Formatter; |
| | | 861 | | } |
| | | 862 | | } |
| | | 863 | | |
| | 0 | 864 | | return null; |
| | | 865 | | } |
| | | 866 | | |
| | | 867 | | internal virtual DataContractJsonSerializerOperationFormatter CreateDataContractJsonSerializerOperationFormatter |
| | | 868 | | { |
| | | 869 | | //return new DataContractJsonSerializerOperationFormatter(od, dcsob.MaxItemsInObjectGraph, dcsob.IgnoreExten |
| | | 870 | | |
| | 0 | 871 | | OperationDescription clonedOperation = new OperationDescription(od.Name, od.DeclaringContract); |
| | 0 | 872 | | foreach (MessageDescription message in od.Messages) |
| | | 873 | | { |
| | 0 | 874 | | MessageDescription clonedMessage = message.Clone(); |
| | 0 | 875 | | if (IsValidReturnValue(clonedMessage.Body.ReturnValue)) |
| | | 876 | | { |
| | 0 | 877 | | if (clonedMessage.Body.Parts.Count == 0) |
| | | 878 | | { |
| | 0 | 879 | | if (clonedMessage.Body.ReturnValue.Type == typeof(Stream)) |
| | | 880 | | { |
| | 0 | 881 | | clonedMessage.Body.WrapperName = JsonGlobals.RootString; |
| | 0 | 882 | | clonedMessage.Body.WrapperNamespace = string.Empty; |
| | | 883 | | } |
| | | 884 | | } |
| | | 885 | | } |
| | | 886 | | else |
| | | 887 | | { |
| | 0 | 888 | | if (clonedMessage.Body.Parts.Count == 1) |
| | | 889 | | { |
| | 0 | 890 | | if (clonedMessage.Body.Parts[0].Type == typeof(Stream)) |
| | | 891 | | { |
| | 0 | 892 | | clonedMessage.Body.WrapperName = JsonGlobals.RootString; |
| | 0 | 893 | | clonedMessage.Body.WrapperNamespace = string.Empty; |
| | | 894 | | } |
| | | 895 | | } |
| | | 896 | | } |
| | | 897 | | |
| | 0 | 898 | | clonedOperation.Messages.Add(clonedMessage); |
| | | 899 | | } |
| | | 900 | | |
| | 0 | 901 | | return new DataContractJsonSerializerOperationFormatter(clonedOperation, dcsob.MaxItemsInObjectGraph, dcsob. |
| | | 902 | | } |
| | | 903 | | |
| | | 904 | | private static MessagePartDescription GetStreamPart(MessageDescription messageDescription) |
| | | 905 | | { |
| | 0 | 906 | | if (IsValidReturnValue(messageDescription.Body.ReturnValue)) |
| | | 907 | | { |
| | 0 | 908 | | if (messageDescription.Body.Parts.Count == 0) |
| | | 909 | | { |
| | 0 | 910 | | if (messageDescription.Body.ReturnValue.Type == typeof(Stream)) |
| | | 911 | | { |
| | 0 | 912 | | return messageDescription.Body.ReturnValue; |
| | | 913 | | } |
| | | 914 | | } |
| | | 915 | | } |
| | | 916 | | else |
| | | 917 | | { |
| | 0 | 918 | | if (messageDescription.Body.Parts.Count == 1) |
| | | 919 | | { |
| | 0 | 920 | | if (messageDescription.Body.Parts[0].Type == typeof(Stream)) |
| | | 921 | | { |
| | 0 | 922 | | return messageDescription.Body.Parts[0]; |
| | | 923 | | } |
| | | 924 | | } |
| | | 925 | | } |
| | | 926 | | |
| | 0 | 927 | | return null; |
| | | 928 | | } |
| | | 929 | | |
| | 0 | 930 | | private static bool IsValidReturnValue(MessagePartDescription returnValue) => (returnValue != null) && (returnVa |
| | | 931 | | |
| | | 932 | | // TODO: Can I remove this? |
| | | 933 | | //IClientMessageFormatter GetDefaultXmlAndJsonClientFormatter(OperationDescription od, bool isWrapped) |
| | | 934 | | //{ |
| | | 935 | | // IClientMessageFormatter xmlFormatter = GetDefaultClientFormatter(od, false, isWrapped); |
| | | 936 | | // if (!SupportsJsonFormat(od)) |
| | | 937 | | // { |
| | | 938 | | // return xmlFormatter; |
| | | 939 | | // } |
| | | 940 | | // IClientMessageFormatter jsonFormatter = GetDefaultClientFormatter(od, true, isWrapped); |
| | | 941 | | // Dictionary<WebContentFormat, IClientMessageFormatter> map = new Dictionary<WebContentFormat, IClientMessag |
| | | 942 | | // map.Add(WebContentFormat.Xml, xmlFormatter); |
| | | 943 | | // map.Add(WebContentFormat.Json, jsonFormatter); |
| | | 944 | | // // In case there is no format property, the default formatter to use is XML |
| | | 945 | | // return new DemultiplexingClientMessageFormatter(map, xmlFormatter); |
| | | 946 | | //} |
| | | 947 | | |
| | | 948 | | private IDispatchMessageFormatter GetDefaultXmlAndJsonDispatchFormatter(OperationDescription od, bool isWrapped) |
| | | 949 | | { |
| | 0 | 950 | | IDispatchMessageFormatter xmlFormatter = GetDefaultDispatchFormatter(od, false, isWrapped); |
| | 0 | 951 | | if (!SupportsJsonFormat(od)) |
| | | 952 | | { |
| | 0 | 953 | | return xmlFormatter; |
| | | 954 | | } |
| | | 955 | | |
| | 0 | 956 | | IDispatchMessageFormatter jsonFormatter = GetDefaultDispatchFormatter(od, true, isWrapped); |
| | 0 | 957 | | Dictionary<WebContentFormat, IDispatchMessageFormatter> map = new Dictionary<WebContentFormat, IDispatchMess |
| | 0 | 958 | | map.Add(WebContentFormat.Xml, xmlFormatter); |
| | 0 | 959 | | map.Add(WebContentFormat.Json, jsonFormatter); |
| | 0 | 960 | | return new DemultiplexingDispatchMessageFormatter(map, xmlFormatter); |
| | | 961 | | } |
| | | 962 | | |
| | | 963 | | internal WebMessageFormat GetRequestFormat(OperationDescription od) |
| | | 964 | | { |
| | 0 | 965 | | WebGetAttribute wga = ((KeyedByTypeCollection<IOperationBehavior>)od.OperationBehaviors).Find<WebGetAttribut |
| | 0 | 966 | | WebInvokeAttribute wia = ((KeyedByTypeCollection<IOperationBehavior>)od.OperationBehaviors).Find<WebInvokeAt |
| | 0 | 967 | | EnsureOk(wga, wia, od); |
| | 0 | 968 | | if (wga != null) |
| | | 969 | | { |
| | 0 | 970 | | return wga.IsRequestFormatSetExplicitly ? wga.RequestFormat : this.DefaultOutgoingRequestFormat; |
| | | 971 | | } |
| | 0 | 972 | | else if (wia != null) |
| | | 973 | | { |
| | 0 | 974 | | return wia.IsRequestFormatSetExplicitly ? wia.RequestFormat : this.DefaultOutgoingRequestFormat; |
| | | 975 | | } |
| | | 976 | | else |
| | | 977 | | { |
| | 0 | 978 | | return DefaultOutgoingRequestFormat; |
| | | 979 | | } |
| | | 980 | | } |
| | | 981 | | |
| | | 982 | | internal WebMessageFormat GetResponseFormat(OperationDescription od) |
| | | 983 | | { |
| | 163 | 984 | | WebGetAttribute wga = ((KeyedByTypeCollection<IOperationBehavior>)od.OperationBehaviors).Find<WebGetAttribut |
| | 163 | 985 | | WebInvokeAttribute wia = ((KeyedByTypeCollection<IOperationBehavior>)od.OperationBehaviors).Find<WebInvokeAt |
| | 163 | 986 | | EnsureOk(wga, wia, od); |
| | 163 | 987 | | if (wga != null) |
| | | 988 | | { |
| | 138 | 989 | | return wga.IsResponseFormatSetExplicitly ? wga.ResponseFormat : this.DefaultOutgoingResponseFormat; |
| | | 990 | | } |
| | 25 | 991 | | else if (wia != null) |
| | | 992 | | { |
| | 25 | 993 | | return wia.IsResponseFormatSetExplicitly ? wia.ResponseFormat : this.DefaultOutgoingResponseFormat; |
| | | 994 | | } |
| | | 995 | | else |
| | | 996 | | { |
| | 0 | 997 | | return DefaultOutgoingResponseFormat; |
| | | 998 | | } |
| | | 999 | | } |
| | | 1000 | | |
| | | 1001 | | private void ValidateBodyParameters(OperationDescription operation, bool request) |
| | | 1002 | | { |
| | 326 | 1003 | | string method = GetWebMethod(operation); |
| | 326 | 1004 | | if (request) |
| | | 1005 | | { |
| | 163 | 1006 | | ValidateGETHasNoBody(operation, method); |
| | | 1007 | | } |
| | | 1008 | | |
| | | 1009 | | // validate that if bare is chosen for request/response, then at most 1 parameter is possible |
| | 326 | 1010 | | ValidateBodyStyle(operation, request); |
| | | 1011 | | // validate if the request or response body is a stream, no other body parameters |
| | | 1012 | | // can be specified |
| | 326 | 1013 | | ValidateAtMostOneStreamParameter(operation, request); |
| | 326 | 1014 | | } |
| | | 1015 | | |
| | | 1016 | | private void ValidateBodyStyle(OperationDescription operation, bool request) |
| | | 1017 | | { |
| | 326 | 1018 | | WebMessageBodyStyle style = GetBodyStyle(operation); |
| | | 1019 | | Type dummy; |
| | 326 | 1020 | | if (request && IsBareRequest(style)) |
| | | 1021 | | { |
| | 163 | 1022 | | TryGetNonMessageParameterType(operation.Messages[0], operation, true, out dummy); |
| | | 1023 | | } |
| | | 1024 | | |
| | 326 | 1025 | | if (!request && operation.Messages.Count > 1 && IsBareResponse(style)) |
| | | 1026 | | { |
| | 163 | 1027 | | TryGetNonMessageParameterType(operation.Messages[1], operation, false, out dummy); |
| | | 1028 | | } |
| | 326 | 1029 | | } |
| | | 1030 | | |
| | | 1031 | | private void ValidateGETHasNoBody(OperationDescription operation, string method) |
| | | 1032 | | { |
| | 163 | 1033 | | if (method == GET) |
| | | 1034 | | { |
| | 138 | 1035 | | if (!IsUntypedMessage(operation.Messages[0]) && operation.Messages[0].Body.Parts.Count != 0) |
| | | 1036 | | { |
| | 0 | 1037 | | if (!IsTypedMessage(operation.Messages[0])) |
| | | 1038 | | { |
| | 0 | 1039 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 1040 | | SR.Format(SR.GETCannotHaveBody, operation.Name, operation.DeclaringContract.Name, operation. |
| | | 1041 | | } |
| | | 1042 | | else |
| | | 1043 | | { |
| | 0 | 1044 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 1045 | | SR.Format(SR.GETCannotHaveMCParameter, operation.Name, operation.DeclaringContract.Name, ope |
| | | 1046 | | } |
| | | 1047 | | } |
| | | 1048 | | } |
| | 163 | 1049 | | } |
| | | 1050 | | |
| | | 1051 | | private void ValidateContract(ServiceEndpoint endpoint) |
| | | 1052 | | { |
| | 400 | 1053 | | foreach (OperationDescription od in endpoint.Contract.Operations) |
| | | 1054 | | { |
| | 163 | 1055 | | ValidateNoOperationHasEncodedXmlSerializer(od); |
| | 163 | 1056 | | ValidateNoMessageContractHeaders(od.Messages[0], od.Name, endpoint.Contract.Name); |
| | 163 | 1057 | | ValidateNoBareMessageContractWithMultipleParts(od.Messages[0], od.Name, endpoint.Contract.Name); |
| | 163 | 1058 | | ValidateNoMessageContractWithStream(od.Messages[0], od.Name, endpoint.Contract.Name); |
| | 163 | 1059 | | if (od.Messages.Count > 1) |
| | | 1060 | | { |
| | 163 | 1061 | | ValidateNoMessageContractHeaders(od.Messages[1], od.Name, endpoint.Contract.Name); |
| | 163 | 1062 | | ValidateNoBareMessageContractWithMultipleParts(od.Messages[1], od.Name, endpoint.Contract.Name); |
| | 163 | 1063 | | ValidateNoMessageContractWithStream(od.Messages[1], od.Name, endpoint.Contract.Name); |
| | | 1064 | | } |
| | | 1065 | | } |
| | 37 | 1066 | | } |
| | | 1067 | | |
| | | 1068 | | internal static bool IsXmlSerializerFaultFormat(OperationDescription operationDescription) |
| | | 1069 | | { |
| | 0 | 1070 | | XmlSerializerOperationBehavior xsob = ((KeyedByTypeCollection<IOperationBehavior>)operationDescription.Opera |
| | | 1071 | | |
| | 0 | 1072 | | return (xsob != null && xsob.XmlSerializerFormatAttribute.SupportFaults); |
| | | 1073 | | } |
| | | 1074 | | |
| | | 1075 | | private void ValidateNoMessageContractWithStream(MessageDescription md, string opName, string contractName) |
| | | 1076 | | { |
| | 326 | 1077 | | if (IsTypedMessage(md)) |
| | | 1078 | | { |
| | 0 | 1079 | | foreach (MessagePartDescription description in md.Body.Parts) |
| | | 1080 | | { |
| | 0 | 1081 | | if (description.Type == typeof(Stream)) |
| | | 1082 | | { |
| | 0 | 1083 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( |
| | 0 | 1084 | | new InvalidOperationException(SR.Format(SR.StreamBodyMemberNotSupported, GetType().ToString( |
| | | 1085 | | } |
| | | 1086 | | } |
| | | 1087 | | } |
| | 326 | 1088 | | } |
| | | 1089 | | |
| | | 1090 | | private void ValidateNoOperationHasEncodedXmlSerializer(OperationDescription od) |
| | | 1091 | | { |
| | 163 | 1092 | | XmlSerializerOperationBehavior xsob = ((KeyedByTypeCollection<IOperationBehavior>)od.OperationBehaviors).Fin |
| | 163 | 1093 | | if (xsob != null && (xsob.XmlSerializerFormatAttribute.Style == OperationFormatStyle.Rpc || xsob.XmlSerializ |
| | | 1094 | | { |
| | 0 | 1095 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Rpc |
| | | 1096 | | } |
| | 163 | 1097 | | } |
| | | 1098 | | |
| | | 1099 | | private void ValidateNoBareMessageContractWithMultipleParts(MessageDescription md, string opName, string contrac |
| | | 1100 | | { |
| | 326 | 1101 | | if (IsTypedMessage(md) && md.Body.WrapperName == null) |
| | | 1102 | | { |
| | 0 | 1103 | | if (md.Body.Parts.Count > 1) |
| | | 1104 | | { |
| | 0 | 1105 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 1106 | | SR.Format(SR.InvalidMessageContractWithoutWrapperName, opName, contractName, md.MessageType))); |
| | | 1107 | | } |
| | | 1108 | | |
| | 0 | 1109 | | if (md.Body.Parts.Count == 1 && md.Body.Parts[0].Multiple) |
| | | 1110 | | { |
| | 0 | 1111 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR |
| | | 1112 | | } |
| | | 1113 | | } |
| | 326 | 1114 | | } |
| | | 1115 | | |
| | | 1116 | | private void ValidateNoMessageContractHeaders(MessageDescription md, string opName, string contractName) |
| | | 1117 | | { |
| | 326 | 1118 | | if (md.Headers.Count != 0) |
| | | 1119 | | { |
| | 0 | 1120 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 1121 | | SR.Format(SR.InvalidMethodWithSOAPHeaders, opName, contractName))); |
| | | 1122 | | } |
| | 326 | 1123 | | } |
| | | 1124 | | |
| | | 1125 | | internal class MessagePassthroughFormatter : IClientMessageFormatter, IDispatchMessageFormatter |
| | | 1126 | | { |
| | 0 | 1127 | | public object DeserializeReply(Message message, object[] parameters) => message; |
| | | 1128 | | |
| | | 1129 | | public void DeserializeRequest(Message message, object[] parameters) |
| | | 1130 | | { |
| | 0 | 1131 | | parameters[0] = message; |
| | 0 | 1132 | | } |
| | | 1133 | | |
| | 0 | 1134 | | public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result) => result a |
| | | 1135 | | |
| | 0 | 1136 | | public Message SerializeRequest(MessageVersion messageVersion, object[] parameters) => parameters[0] as Mess |
| | | 1137 | | } |
| | | 1138 | | } |
| | | 1139 | | } |