简体   繁体   中英

WCF Rest service receiving object as null

I have WCF rest service which is expecting 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;
        }

Item class looks like below.

 [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'm passing json from android application.

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

but the WCF method is getting Item as null not sure why.

any help would be appreciated.

thanks.

EDIT

finally got it work...changed WebMessageBodyStyle.Wrapped to WebMessageBodyStyle.Bare

Thanks for sharing your solution. I needed the WebMessageBodyStyle.Bare bit but my other issue was that my JSON was wrapped in a parent "node" per suggestions on other sites like this:

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

But my JSON was always null. However once I dropped the "parent node" of my JSON my WCF finally could read the object:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM