Manish
Manish

Reputation: 1326

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

Upvotes: 2

Views: 1806

Answers (1)

Christopher
Christopher

Reputation: 2256

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"}

Upvotes: 1

Related Questions