0

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\"}}"

  • Is this result copied from a debug session where you have your cursor over the result? – Paul Karam Feb 07 '19 at 12:53
  • result is copied from Postman or apiary.io – Jiří Zdražil Feb 07 '19 at 13:25
  • If I see result before return, result is fine, exactly like I expect. – Jiří Zdražil Feb 07 '19 at 13:34
  • { "root" : { "question" : "language", "date" : "2019-02-07" } } is not a valid c# string, it is not wrapped by ". So if you want to use string as the return type, it should be "{\"root\":{\"question\":\"language\",\"date\":\"2019-02-07\"}}", I don't understand why you want to remove the double quotes – Ackelry Xu Feb 11 '19 at 07:04
  • Thanks. Truly I don't want string like result. I want JSON. But I don't know how should I do this. Problem is, that I don't have any objekt. I have only unknow XmlDocument. I don't know, how this XML looks like. So I thought, string was my only chance. If I try return object, it doesn't work. – Jiří Zdražil Feb 11 '19 at 11:12

0 Answers0