WCF Rest receiving object as null

I have a WCF service that expects an object as input.

[WebInvoke(Method = "POST", UriTemplate = "SaveItem", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)] [OperationContract] public string SaveItem(Item item) { string retValue; using (var business = new ItemBusiness()) { retValue = business.SaveItem(item).ToString(); } return retValue; } 

The class of the element is as follows.

  [DataContract] public class Item { [DataMember] public string UserId { get; set; } [DataMember] public string Name { get; set; } [DataMember] public string Description { get; set; } [DataMember] public string Cost { get; set; } //[DataMember] //public string AvailableQunatity { get; set; } [DataMember] public string IsNegotiable { get; set; } [DataMember] public string LocationLat { get; set; } [DataMember] public string LocationLong { get; set; } [DataMember] public string Condition { get; set; } [DataMember] public string DeliveryType { get; set; } [DataMember] public string PostalCode { get; set; } [DataMember] public string Category { get; set; } } 

I am passing json from an android app.

{"UserId": "1", "Name": "1", "Description": "1", "Cost": "1", "IsNegotiable": "1", "LocationLat": "1", " LocationLong ":" 1 "," Status ":" 1 "," DeliveryType ":" 1 "," PostalCode ":" 1 "," Category ":" 13 "}

but the WCF method gets the element as null, but not sure why.

Any help would be appreciated.

thanks.

EDIT

finally working ... changed WebMessageBodyStyle.Wrapped to WebMessageBodyStyle.Bare

+1
source share
1 answer

Thank you for sharing your decision. I need the WebMessageBodyStyle.Bare bit, but my other problem was that my JSON was wrapped in the parent "node" on sentences on other sites, for example:

 Not Working --> {"MethodParamName":[{"prop1":"value1","prop2":"value2"}]} 

But my JSON was always null. However, as soon as I reset the β€œparent node” of my JSON, my WCF was finally able to read the object:

 Working --> {"prop1":"value1","prop2":"value2"} 
0
source

Source: https://habr.com/ru/post/1413176/


All Articles