WCF Rest服务接收对象为null [英] WCF Rest service receiving object as null

查看:76
本文介绍了WCF Rest服务接收对象为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有WCF rest服务,希望将对象作为输入.

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类如下所示.

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

    }

我正在从android应用程序传递json.

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}

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

但是WCF方法将Item设为null不确定原因.

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

任何帮助将不胜感激.

谢谢.

编辑

终于成功了...将 WebMessageBodyStyle.Wrapped 更改为 WebMessageBodyStyle.Bare

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

推荐答案

感谢分享您的解决方案.我需要WebMessageBodyStyle.Bare位,但是我的另一个问题是,按照其他网站上的建议,我的JSON被包装在父节点"中,例如:

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

但是我的JSON始终为null.但是,一旦我删除了JSON的父节点",我的WCF终于可以读取该对象:

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

这篇关于WCF Rest服务接收对象为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆