【问题标题】:WCF REST service returning empty classWCF REST 服务返回空类
【发布时间】:2014-12-08 16:48:03
【问题描述】:

我有以下服务:

    [ServiceContract]
    interface IConnectionService
    {
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "GetState", BodyStyle = System.ServiceModel.Web.WebMessageBodyStyle.Wrapped), Description("")]
        State GetState();
    }

    [DataContract]
    public class State
    {
        [DataMember]
        public bool Client_1_Ok { get; set; }

        [DataMember]
        public bool Client_2_Ok { get; set; }
    }

在我的服务器端,我创建了一个 State 类的新实例并设置变量只是为了查看它们是否按预期到达客户端

    public class Server : IConnectionService
    {
        public State GetState()
        {
            State tempState = new State();

            tempState .Client_1_Ok = true;
            tempState .Client_2_Ok = true;

            return tempState ;
        }

在客户端我打开一个频道并在我的服务器端调用 GetState.. 到目前为止一切顺利

        private IConnectionService ConnectionChannel = null;

        public State GetState()
        {
            try
            {
                ConnectionFactory = new WebChannelFactory<IConnectionService>(new Uri("http://" + this.HostIpAddress + ":" + this.Port.ToString() + "/Tes"));
                this.ConnectionChannel = ConnectionFactory.CreateChannel();

                State returnvalue = this.ConnectionChannel.GetState();

                return state;
            }
            catch (Exception e)
            {
                //Communication error
                return null;
            }
            finally
            {
                try { this.ConnectionChannel.Close(); }
                catch { this.ConnectionChannel.Abort(); }

                //dispose of the connection channel
                this.ConnectionChannel.Dispose();
                this.ConnectionChannel = null;
            }
        }

但来自客户端的 GetState 调用总是返回一个布尔值为 false 的 State 实例。

在类似的帖子中,人们忘记添加 DataContract 和 DataMember 属性,但我确保添加了这些。解决方案可能有点小而愚蠢,但我看不出我在哪里搞砸了

【问题讨论】:

标签: c# web-services wcf


【解决方案1】:

服务合同没有 RequestFormat。就我而言,它需要 WebMessageFormat.Json

我的 ServiceContract 现在看起来像这样:

    [ServiceContract]
    interface IConnectionService
    {
        [WebGet(UriTemplate = "GetState", BodyStyle = System.ServiceModel.Web.WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json), Description("")]
        State GetState();
    }

【讨论】:

    猜你喜欢
    • 2015-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多