none
Send Untyped Json to a WCF-WebHttp (REST) web service RRS feed

  • Question

  • Hello,

    I want to send an untyped json to a WCF-WebHttp (REST) web service (the definition of the contract is below , the service is a Rest web service, configured with webHttp endpoint) :

        [ServiceContract]
        public interface IServiceJsonContract
        {
            [WebInvoke(UriTemplate = "/MyMethod", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, Method = "POST")]
            Message MyMethod(Message input);
        }


    The problem is the Message paramater is used for untyped soap messages and I don't know what is its equivalent for a Json message .

    If I use instead of Message parameter a typed message (a class defined on the web service - with a [DataContract] attribute ) it works , because the default DataContractJsonSerializer knows how to serialize and deserialize it based on the schema of the defined class. What I want to send is , however , json messages that don't have a schema on the web service side (and to dynamically convert them to xml and send after that a json response to he client) . 

    So , the question is : what parameter should I use so untyped json can be accepted by the contract I have above ? (to dynamically convert afterwards in xml and process it on my web service and send an untyped json back to the client)

    Thanks

    Tuesday, June 20, 2017 9:21 AM

Answers

  • Hi CezarBotezatu,

    I suggest you try below code:

    Message message = Message.CreateMessage(  
                      MessageVersion.None,    // No SOAP message version  
                                 "*",                     // SOAP action, ignored since this is JSON  
                                 "Response string: JSON format specified", // Message body  
                                 new DataContractJsonSerializer(typeof(string))); // Specify DataContractJsonSerializer  
         message.Properties.Add( WebBodyFormatMessageProperty.Name,   
                        new WebBodyFormatMessageProperty(WebContentFormat.Json)); // Use JSON format
         var response = WebOperationContext.Current.OutgoingResponse;
         response.ContentType = "application/json";  
         return message;

    If it did not work for you, I would suggest you share us a simple demo which could reproduce your issue.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by CezarBotezatu Friday, June 23, 2017 8:24 AM
    Friday, June 23, 2017 1:48 AM

All replies

  • I almost done it with Message , the only problem is I'm not receiving data in Json format , but Content-type : application\xml

    Message MyMethod(Message input)
    {
    ...
    Message response = Message.CreateMessage(  
                      MessageVersion.None,    // No SOAP message version  
                                 "*",                     // SOAP action, ignored since this is JSON  
                                 "Response string: JSON format specified", // Message body  
                                 new DataContractJsonSerializer(typeof(string))); // Specify DataContractJsonSerializer  
          response.Properties.Add( WebBodyFormatMessageProperty.Name,   
                        new WebBodyFormatMessageProperty(WebContentFormat.Json)); // Use JSON format  
    return response;
    }

    The raw response for this is :

    HTTP/1.1 200 OK
    Content-Length: 40
    Content-Type: application/xml; charset=utf-8
    Server: Microsoft-IIS/7.5
    X-Powered-By: ASP.NET
    Date: Wed, 21 Jun 2017 16:52:31 GMT
    "Response string: JSON format specified"

    I have no idea how to set the Content-type of the response to application/json ...


    Thursday, June 22, 2017 1:34 PM
  • Hi CezarBotezatu,

    I suggest you try below code:

    Message message = Message.CreateMessage(  
                      MessageVersion.None,    // No SOAP message version  
                                 "*",                     // SOAP action, ignored since this is JSON  
                                 "Response string: JSON format specified", // Message body  
                                 new DataContractJsonSerializer(typeof(string))); // Specify DataContractJsonSerializer  
         message.Properties.Add( WebBodyFormatMessageProperty.Name,   
                        new WebBodyFormatMessageProperty(WebContentFormat.Json)); // Use JSON format
         var response = WebOperationContext.Current.OutgoingResponse;
         response.ContentType = "application/json";  
         return message;

    If it did not work for you, I would suggest you share us a simple demo which could reproduce your issue.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by CezarBotezatu Friday, June 23, 2017 8:24 AM
    Friday, June 23, 2017 1:48 AM
  • Thank you , Edward , it works :-) 
    Friday, June 23, 2017 8:25 AM
  • One question , though :

    In the method CreateMessage , on the message body section , I used a string representation of Json . The only way to encode this as a string is escaping the " character :

                                        

    Message message = Message.CreateMessage(  
                                 MessageVersion.None,    // No SOAP message version  
                                 "*",                     // SOAP action, ignored since this is JSON  
                                  "{\"bla\": 2 }", // Message body  
                                 new DataContractJsonSerializer(typeof(string))); // Specify DataContractJsonSerializer  

                                 message.Properties.Add( WebBodyFormatMessageProperty.Name, new WebBodyFormatMessageProperty(WebContentFormat.Json)); // Use JSON format
                            var response = WebOperationContext.Current.OutgoingResponse;
                          response.ContentType = "application/json";  
                                  return message;

    I want to receive back this Json response:

    {
    "bla" : 2
    }

    Instead , I receive this Json (the \ is not removed) :

    "{\"bla\": 2 }" 

    1) Any ideea how to fix this ? 


    PS. Or , even better  , is there any way to map back and forth from a xml infoset to its xml representation . Xml infoset is the generic xml that Wcf is using to map a Json 

    Ex : from 

    <root type="object"><bla type="number">2</bla></root>

    to 

    <bla>2</bla>

    Thanks 


    Monday, June 26, 2017 8:51 AM
  • Hi CezarBotezatu,

    For this new issue, I would suggest you post a new thread.

    It would be much helpful if you could share us a simple demo which could reproduce your issue.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, June 29, 2017 8:01 AM