Message SerializeReplyCore (MessageVersion messageVersion, object [] parameters, object result)
{
// parameters could be null.
// result could be null. For Raw output, it becomes no output.
CheckMessageVersion (messageVersion);
MessageDescription md = GetMessageDescription (MessageDirection.Output);
// FIXME: use them.
// var dcob = Operation.Behaviors.Find<DataContractSerializerOperationBehavior> ();
// XmlObjectSerializer xos = dcob.CreateSerializer (result.GetType (), md.Body.WrapperName, md.Body.WrapperNamespace, null);
// var xsob = Operation.Behaviors.Find<XmlSerializerOperationBehavior> ();
// XmlSerializer [] serializers = XmlSerializer.FromMappings (xsob.GetXmlMappings ().ToArray ());
WebMessageFormat msgfmt = Info.IsResponseFormatSetExplicitly ? Info.ResponseFormat : Behavior.DefaultOutgoingResponseFormat;
XmlObjectSerializer serializer = null;
// FIXME: serialize ref/out parameters as well.
string name = null, ns = null;
switch (msgfmt) {
case WebMessageFormat.Xml:
serializer = GetSerializer (WebContentFormat.Xml, IsResponseBodyWrapped, md.Body.ReturnValue);
name = IsResponseBodyWrapped ? md.Body.WrapperName : null;
ns = IsResponseBodyWrapped ? md.Body.WrapperNamespace : null;
break;
case WebMessageFormat.Json:
serializer = GetSerializer (WebContentFormat.Json, IsResponseBodyWrapped, md.Body.ReturnValue);
name = IsResponseBodyWrapped ? (BodyName ?? md.Body.ReturnValue.Name) : null;
ns = String.Empty;
break;
}
var contentFormat = ToContentFormat (msgfmt, result);
string mediaType = GetMediaTypeString (contentFormat);
Message ret = contentFormat == WebContentFormat.Raw ? new RawMessage ((Stream) result) : Message.CreateMessage (MessageVersion.None, null, new WrappedBodyWriter (result, serializer, name, ns, contentFormat));
// Message properties
var hp = new HttpResponseMessageProperty ();
// FIXME: get encoding from somewhere
hp.Headers ["Content-Type"] = mediaType + "; charset=utf-8";
// apply user-customized HTTP results via WebOperationContext.
if (WebOperationContext.Current != null) // this formatter must be available outside ServiceHost.
WebOperationContext.Current.OutgoingResponse.Apply (hp);
// FIXME: fill some properties if required.
ret.Properties.Add (HttpResponseMessageProperty.Name, hp);
var wp = new WebBodyFormatMessageProperty (contentFormat);
ret.Properties.Add (WebBodyFormatMessageProperty.Name, wp);
return ret;
}