protected virtual object ReadObject(Message message) { if (HttpStreamFormatter.IsEmptyMessage(message)) { return(null); } XmlObjectSerializer[] inputSerializers = GetInputSerializers(); XmlDictionaryReader reader = message.GetReaderAtBodyContents(); if (inputSerializers != null) { for (int i = 0; i < inputSerializers.Length; ++i) { if (inputSerializers[i].IsStartObject(reader)) { return(inputSerializers[i].ReadObject(reader, false)); } } } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SerializationException(SR2.GetString(SR2.CannotDeserializeBody, reader.LocalName, reader.NamespaceURI, operationName, contractName, contractNs, this.serializerType))); }
protected virtual IDispatchMessageFormatter GetRequestDispatchFormatter(OperationDescription operationDescription, ServiceEndpoint endpoint) { IDispatchMessageFormatter result = null; // get some validation errors by creating "throwAway" formatter UriTemplateDispatchFormatter throwAway = new UriTemplateDispatchFormatter(operationDescription, null, GetQueryStringConverter(operationDescription), endpoint.Contract.Name, endpoint.Address.Uri); int numUriVariables = throwAway.pathMapping.Count + throwAway.queryMapping.Count; HideReplyMessage(operationDescription, delegate() { WebMessageBodyStyle style = GetBodyStyle(operationDescription); Effect doBodyFormatter = delegate() { if (numUriVariables != 0) { EnsureNotUntypedMessageNorMessageContract(operationDescription); } // get body formatter ValidateBodyParameters(operationDescription, true); Type type; if (TryGetStreamParameterType(operationDescription.Messages[0], operationDescription, true, out type)) { result = new HttpStreamFormatter(operationDescription); } else { Type parameterType; if (UseBareRequestFormatter(style, operationDescription, out parameterType)) { result = SingleBodyParameterMessageFormatter.CreateXmlAndJsonDispatchFormatter(operationDescription, parameterType, true, this.xmlSerializerManager, this.JavascriptCallbackParameterName); } else { result = GetDefaultXmlAndJsonDispatchFormatter(operationDescription, !IsBareRequest(style)); } } }; if (numUriVariables == 0) { if (IsUntypedMessage(operationDescription.Messages[0])) { ValidateBodyParameters(operationDescription, true); result = new MessagePassthroughFormatter(); } else if (IsTypedMessage(operationDescription.Messages[0])) { ValidateBodyParameters(operationDescription, true); result = GetDefaultXmlAndJsonDispatchFormatter(operationDescription, !IsBareRequest(style)); } else { doBodyFormatter(); } } else { HideRequestUriTemplateParameters(operationDescription, throwAway, delegate() { CloneMessageDescriptionsBeforeActing(operationDescription, delegate() { doBodyFormatter(); }); }); } result = new UriTemplateDispatchFormatter(operationDescription, result, GetQueryStringConverter(operationDescription), endpoint.Contract.Name, endpoint.Address.Uri); }); return result; }
protected virtual IClientMessageFormatter GetRequestClientFormatter(OperationDescription operationDescription, ServiceEndpoint endpoint) { WebMessageFormat requestFormat = GetRequestFormat(operationDescription); bool useJson = (requestFormat == WebMessageFormat.Json); WebMessageEncodingBindingElement webEncoding = (useJson) ? endpoint.Binding.CreateBindingElements().Find<WebMessageEncodingBindingElement>() : null; IClientMessageFormatter innerFormatter = null; // get some validation errors by creating "throwAway" formatter // validate that endpoint.Address is not null before accessing the endpoint.Address.Uri. This is to avoid throwing a NullRefException while constructing a UriTemplateClientFormatter if (endpoint.Address == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( SR2.GetString(SR2.ServiceEndpointMustHaveNonNullAddress, typeof(ServiceEndpoint), typeof(ChannelFactory), typeof(WebHttpEndpoint), AddressPropertyName, typeof(ServiceEndpoint)))); } UriTemplateClientFormatter throwAway = new UriTemplateClientFormatter(operationDescription, null, GetQueryStringConverter(operationDescription), endpoint.Address.Uri, false, endpoint.Contract.Name); int numUriVariables = throwAway.pathMapping.Count + throwAway.queryMapping.Count; bool isStream = false; HideReplyMessage(operationDescription, delegate() { WebMessageBodyStyle style = GetBodyStyle(operationDescription); bool isUntypedWhenUriParamsNotConsidered = false; Effect doBodyFormatter = delegate() { if (numUriVariables != 0) { EnsureNotUntypedMessageNorMessageContract(operationDescription); } // get body formatter ValidateBodyParameters(operationDescription, true); IClientMessageFormatter baseFormatter; Type parameterType; if (TryGetStreamParameterType(operationDescription.Messages[0], operationDescription, true, out parameterType)) { isStream = true; baseFormatter = new HttpStreamFormatter(operationDescription); } else if (UseBareRequestFormatter(style, operationDescription, out parameterType)) { baseFormatter = SingleBodyParameterMessageFormatter.CreateClientFormatter(operationDescription, parameterType, true, useJson, this.xmlSerializerManager); } else { baseFormatter = GetDefaultClientFormatter(operationDescription, useJson, !IsBareRequest(style)); } innerFormatter = baseFormatter; isUntypedWhenUriParamsNotConsidered = IsUntypedMessage(operationDescription.Messages[0]); }; if (numUriVariables == 0) { if (IsUntypedMessage(operationDescription.Messages[0])) { ValidateBodyParameters(operationDescription, true); innerFormatter = new MessagePassthroughFormatter(); isUntypedWhenUriParamsNotConsidered = true; } else if (IsTypedMessage(operationDescription.Messages[0])) { ValidateBodyParameters(operationDescription, true); innerFormatter = GetDefaultClientFormatter(operationDescription, useJson, !IsBareRequest(style)); } else { doBodyFormatter(); } } else { HideRequestUriTemplateParameters(operationDescription, throwAway, delegate() { CloneMessageDescriptionsBeforeActing(operationDescription, delegate() { doBodyFormatter(); }); }); } innerFormatter = new UriTemplateClientFormatter(operationDescription, innerFormatter, GetQueryStringConverter(operationDescription), endpoint.Address.Uri, isUntypedWhenUriParamsNotConsidered, endpoint.Contract.Name); }); string defaultContentType = GetDefaultContentType(isStream, useJson, webEncoding); if (!string.IsNullOrEmpty(defaultContentType)) { innerFormatter = new ContentTypeSettingClientMessageFormatter(defaultContentType, innerFormatter); } return innerFormatter; }