Ejemplo n.º 1
0
        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;
        }
Ejemplo n.º 2
0
 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;
 }
Ejemplo n.º 3
0
        protected virtual IDispatchMessageFormatter GetReplyDispatchFormatter(OperationDescription operationDescription, ServiceEndpoint endpoint)
        {
            if (operationDescription.Messages.Count < 2)
            {
                return null;
            }
            ValidateBodyParameters(operationDescription, false);
            WebMessageFormat responseFormat = GetResponseFormat(operationDescription);

            //  Determine if we should add a json formatter; If the ResponseFormat is json, we always add the json formatter even if the
            //  operation is XmlSerializerFormat because the formatter constructor throws the exception: "json not valid with XmlSerializerFormat" [Microsoft]
            bool useJson = (responseFormat == WebMessageFormat.Json || SupportsJsonFormat(operationDescription));

            IDispatchMessageFormatter innerFormatter;
            Type type;

            if (TryGetStreamParameterType(operationDescription.Messages[1], operationDescription, false, out type))
            {
                innerFormatter = new ContentTypeSettingDispatchMessageFormatter(defaultStreamContentType, new HttpStreamFormatter(operationDescription));
            }
            else if (IsUntypedMessage(operationDescription.Messages[1]))
            {
                innerFormatter = new MessagePassthroughFormatter();
            }
            else
            {
                Type parameterType;
                WebMessageBodyStyle style = GetBodyStyle(operationDescription);
                Dictionary<WebMessageFormat, IDispatchMessageFormatter> formatters = new Dictionary<WebMessageFormat, IDispatchMessageFormatter>();

                if (UseBareReplyFormatter(style, operationDescription, responseFormat, out parameterType))
                {
                    formatters.Add(WebMessageFormat.Xml, SingleBodyParameterMessageFormatter.CreateDispatchFormatter(operationDescription, parameterType, false, false, this.xmlSerializerManager, null));
                    if (useJson)
                    {
                        formatters.Add(WebMessageFormat.Json, SingleBodyParameterMessageFormatter.CreateDispatchFormatter(operationDescription, parameterType, false, true, this.xmlSerializerManager, this.JavascriptCallbackParameterName));
                    }
                }
                else
                {
                    MessageDescription temp = operationDescription.Messages[0];
                    operationDescription.Messages[0] = MakeDummyMessageDescription(MessageDirection.Input);
                    formatters.Add(WebMessageFormat.Xml, GetDefaultDispatchFormatter(operationDescription, false, !IsBareResponse(style)));
                    if (useJson)
                    {
                        formatters.Add(WebMessageFormat.Json, GetDefaultDispatchFormatter(operationDescription, true, !IsBareResponse(style)));
                    }
                    operationDescription.Messages[0] = temp;
                }
                innerFormatter = new MultiplexingDispatchMessageFormatter(formatters, responseFormat);
            }

            return innerFormatter;
        }