public void RegisterOperation(string operationName, MultiplexingDispatchMessageFormatter formatter)
 {
     if (formatter == null)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("formatter");
     }
     Fx.Assert(!this.formatters.ContainsKey(operationName), "An operation should only be registered once.");
     this.formatters.Add(operationName, formatter);
     this.caches.Add(operationName, new NameValueCache<FormatContentTypePair>(maxCachedAcceptHeaders));
 }
Пример #2
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;
        }