WebOperationContext.Current.OutgoingResponse.Header stop working with custom IDispatchMessageFormatter

Viewed 44

I have custom IDispatchMessageFormatter registration

        public class JsonOperationBehavior : IOperationBehavior
        {
            public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
            {

            }
            public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
            {

            }
            public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
            {
                dispatchOperation.Formatter = new JsonFormatter(operationDescription, dispatchOperation.Formatter);
            }
            public void Validate(OperationDescription operationDescription)
            {

            }
        }

        public class JsonFormatter : IDispatchMessageFormatter
        {
            readonly OperationDescription _operationDescription;
            readonly IDispatchMessageFormatter _originalFormatter;

            public JsonFormatter(OperationDescription operationDescription, IDispatchMessageFormatter originalFormatter)
            {
                _operationDescription = operationDescription;
                _originalFormatter = originalFormatter;
            }


            public void DeserializeRequest(Message message, object[] parameters)
            {
                parameters[0] = JsonHelper.DeserializeObject(GetRequestType(), WebServiceHelper.GetRequestRawStringBody(message));
            }

            public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result)
            {
                var json = JsonHelper.SerializeObject(GetResponseType(), result);
                var bytes = Encoding.UTF8.GetBytes(json);
                var replyMessage = Message.CreateMessage(messageVersion, GetResponseAction(), new RawDataWriter(bytes));
                replyMessage.Properties.Add(WebBodyFormatMessageProperty.Name, new WebBodyFormatMessageProperty(WebContentFormat.Raw));

                return replyMessage;
            }


            private Type GetRequestType()
            {
                return _operationDescription.Messages[0].Body.Parts.Last().Type;
            }

            private Type GetResponseType()
            {
                return (_operationDescription.SyncMethod ?? _operationDescription.EndMethod).ReturnType;
            }

            private string GetResponseAction()
            {
                return _operationDescription.Messages[1].Action;
            }
        }

Every thing look like its work correctly but I noted that somewhere else that work before

            WebOperationContext.Current.OutgoingResponse.Headers.Add(name, value);

Stop working and new headers are not show in response.

but then I add code in SerializeReply like:

_ = _originalFormatter.SerializeReply(messageVersion, parameters, result);

Then headers start working again even if I do not use result of this function.

1 Answers
Related