I have self-hosted web service. I get request (JSON). I process that and I get result like XmlDocument. Now I need to return response (also JSON). But there is a problem, because result is JSON structure, but in string format.
[ServiceContract(Name = "MyService", Namespace = "http://tempuri.org/")]
public interface ImyAgent
{
[OperationContract]
[WebInvoke(UriTemplate = "json-post", Method = "*", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
string ActionJsonPost(string data);
}
namespace MyService
{
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.Single, IncludeExceptionDetailInFaults = true, AddressFilterMode = AddressFilterMode.Any)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class myAgent : ImyAgent
{
public myAgent() { }
public string ActionJsonPost(string data)
{
XmlDocument xml = new XmlDocument();
xml.LoadXml("<root><question>language</question><date>2019-02-07</date></root>"); //some XML structure, I can't specified, depends on requests
return JsonConvert.SerializeXmlNode(xml);
}
}
}
So, XML result is:
<root><question>language</question><date>2019-02-07</date></root>
JSON result should be:
{ "root" : { "question" : "language", "date" : "2019-02-07" } }
but unfortunately real result is:
"{\"root\":{\"question\":\"language\",\"date\":\"2019-02-07\"}}"