为什么我的WebGet返回一个空类

时间:2015-06-09 12:30:26

标签: c# json xml wcf rest

当我尝试检索一个充满数据的类时,我从未检索到正确的数据。下面课程中的布尔总是返回假。

当我从Chrome的Postman调用相同的GetState函数而不做任何更改时,我会得到我期望的结果

我有一个类似于这样的课程:

        [DataContract]
        public class State
        {
            [DataMember]
            public bool Camera_1_Ok { get; set; }
            [DataMember]
            public bool Camera_2_Ok { get; set; }
            [DataMember]
            public bool Camera_3_Ok { get; set; }
        }

我的ServiceContract界面如下所示:

        [ServiceContract]
        interface IConnectionService
        {
            [OperationContract]
            [WebGet(BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "GetState", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
            State GetState();
        }

GetState实现如下所示:

        [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
        public sealed class WCFServer : IConnectionService
        {
            public State GetState()
            {
                State tempState = new State();
                State.Camera_1_OK = true;
                State.Camera_2_OK = true;
                State.Camera_3_OK = true;

                return tempState;
            }

我确保服务的双方都有相同的"州"类和相同的" IConnectionService"接口

为什么退回到调用机器的类中的布尔值永远不会被设置?当我从Chrome或Postman调用该函数时,为什么我能获得正确的数据?

针对类似问题的Awnsers提到了[DataContract][DataMember]属性,但我已将这些属性添加到我的班级。

PS:当我在服务的两端设置ResponseFormatWebMessageFormat.Json时,我会按预期获得数据。遗憾的是,由于要求Xml格式,这不是解决方案。

1 个答案:

答案 0 :(得分:0)

BodyStyle = WebMessageBodyStyle.Bare

然后从函数返回System.IO.Stream。

之后,尝试在接收器处反序列化您的流。

相关问题