0

I am trying to pass a custom object and 2 string values to a WCF service that has REST and SOAP enabled

below is the service contract

[OperationContract]
     [WebInvoke(UriTemplate = "/AddData/{Name}/{Id}", RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json, Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped )]
           bool AddData(CustomData itm,string Name, string Id);

then I have sample code to try call the service using HTTP Client , the issue I have is that the string values get passed in but the object value is null

Im not sure if I have defined the service wrong or calling the service wrong ?

private async void button1_Click(object sender, EventArgs e)
    {
        try
        {
 var client = new HttpClient();
            var uri = new Uri("http://localhost:52309/Service1.svc/rest/AddData/test/1");

            CustomData data = new  CustomData ();
            data.description = "TEST";
            data.fieldid = "test1";
            data.fieldvalue = "BLA";

            string postBody = JsonSerializer(data);

            HttpContent contentPost = new StringContent(postBody , Encoding.UTF8, "application/json");
            HttpResponseMessage wcfResponse = await client.PostAsync(uri, contentPost).ContinueWith((postTask) => postTask.Result.EnsureSuccessStatusCode());


            client.Dispose();
 string responJsonText = await wcfResponse.Content.ReadAsStringAsync();

 }
        catch (Exception ex)
        {

        }
}

public string JsonSerializer(FormsData  objectToSerialize)
    {
        if (objectToSerialize == null)
        {
            throw new ArgumentException("objectToSerialize must not be null");
        }
        MemoryStream ms = null;

        DataContractJsonSerializer serializer = new DataContractJsonSerializer(objectToSerialize.GetType());
        ms = new MemoryStream();
        serializer.WriteObject(ms, objectToSerialize);
        ms.Seek(0, SeekOrigin.Begin);
        StreamReader sr = new StreamReader(ms);
        return sr.ReadToEnd();
    }
12
  • 1
    Is the message body supposed to contain CustomData formeatted as JSON? If so, the type needs to be Stream then you deserialise it that way. Commented Feb 4, 2015 at 16:33
  • Yes I think im trying to format the object as JSON, would you mind elaborating a bit , where abouts does the type need to be stream ? Commented Feb 4, 2015 at 16:35
  • Have you looked at using PostAsJsonAsync<T>? Commented Feb 4, 2015 at 16:39
  • 1
    Unless of course the CustomData object has some sort of deserialisation step, such as a DataContract. Commented Feb 4, 2015 at 16:55
  • 1
    @RachaelM sorry, I meant to link PostAsync<T> which lets you post the object directly without having to first convert to Json. Commented Feb 4, 2015 at 17:14

1 Answer 1

1

I resolved the issue by adding [DataContract(Namespace = "CustomData")] and [DataMember(Name = "fieldvalue")] to each variable in the class definition and then called it using PostAsJsonAsync and passing the object directly. Thanks @DanielPark and @DavidG for the pointers in the right direction

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.