2014-12-05 107 views
0

我有以下服务:WCF REST服务返回空类

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

在我的服务器端创建国家类的新实例,并设置变量只是为了看看他们到达客户端,因为它是应该

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的状态实例。

在类似的帖子peaople忘记添加DataContract和DataMember属性,但我一定要添加这些。解决方案可能是小而笨的,但我看不到我弄乱了什么地方

+0

您是否已经配置重新审视基础上:http://stackoverflow.com/questions/15910199/wcf-rest -service-not-returning-data-in-browser? – 2014-12-05 15:43:54

+2

为什么这是GET POST? – 2014-12-05 15:46:28

回答

0

servicecontract没有RequestFormat。在我的情况下,它需要WebMessageFormat.Json

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

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