Consuming WCF REST Service with multiple parameters WITHOUT DataContract



I need to call my WCF REST Service with multiple parameters with the POST method, but I can’t create DataContract containing my parameters because I need simple types : my webservice will be consumed by an objective C application.

I found this syntax on the MSDN site :

[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "savejson?id={id}&fichier={fichier}")]
bool SaveJSONData(string id, string fichier);

To explain quickly the context, I have to call this method to save a JSON file with the Id passed on a database.

My fisrt question is : is it really possible to pass several parameters to a POST method as shown before ?

Secondly : how can I do to consume my service (in C# for the moment, just to test it) with several parameters ?

I’ve already tested with DataContract, and I was doing like that :

string url = "http://localhost:62240/iECVService.svc/savejson";
        WebClient webClient = new WebClient();
        webClient.Headers["Content-type"] = "application/json; charset=utf-8";
        RequestData reqData = new RequestData { IdFichier = "15", Fichier = System.IO.File.ReadAllText(@"C:DeviECViECVMvcApplicationContentfichier.json") };
        MemoryStream requestMs = new MemoryStream();
        DataContractJsonSerializer requestSerializer = new DataContractJsonSerializer(typeof(RequestData));
        requestSerializer.WriteObject(requestMs, reqData);
        byte[] responseData = webClient.UploadData(url, "POST", requestMs.ToArray());
        MemoryStream responseMs = new MemoryStream(responseData);
        DataContractJsonSerializer responseSerializer = new DataContractJsonSerializer(typeof(ResponseData));
        ResponseData resData = responseSerializer.ReadObject(responseMs) as ResponseData;

RequestData and ResponseData were declared this way :

[DataContract(Namespace = "")]
public class RequestData
    public string IdFichier { get; set; }

    public string Fichier { get; set; }

public class ResponseData
    public bool Succes { get; set; }

But as I said, I can’t do it like this anymore…

I hope I’m enough clear, if not, don’t hesitate to ask me details !

Thanks a lot for your help.





There are a few things you can do to avoid using data contracts. The simplest of them is to use a JSON DOM library which lets you create (and parse) JSON data as a tree, without having to convert them to existing classes. Two of them are the JSON.NET project (used in the sample code below), or the System.Json library (can be downloaded via NuGet). There are many JSON libraries for non-.NET languages as well.

Another thing you can do to make your life simpler is to change the body style of the operation from Wrapped (which wraps the response) to Wrapped to WrappedRequest. The request needs to be wrapped, since you have two inputs, but the response doesn’t, so you can eliminate one step with that.

public class Post_182e5e41_4625_4190_8a4d_4d4b13d131cb
    public class Service
        [WebInvoke(Method = "POST",
            ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.WrappedRequest,
            UriTemplate = "savejson")]
        public bool SaveJSONData(string id, string fichier)
            return true;

    public static void Test()
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress));
        Console.WriteLine("Host opened");

        JObject json = new JObject();
        json.Add("id", JToken.FromObject("15"));
        json.Add("Fichier", "the file contents"); //System.IO.File.ReadAllText(@"C:DeviECViECVMvcApplicationContentfichier.json"));

        WebClient c = new WebClient();
        c.Headers[HttpRequestHeader.ContentType] = "application/json";
        string result = c.UploadString(baseAddress + "/savejson", json.ToString(Newtonsoft.Json.Formatting.None, null));
        JToken response = JToken.Parse(result);
        bool success = response.ToObject<bool>();

        Console.Write("Press ENTER to close the host");


[], [], [], []

Facebook Comments

Post a comment