C# WCF REST服务返回空类
我有以下服务:C# WCF REST服务返回空类,c#,web-services,wcf,C#,Web Services,Wcf,我有以下服务: [ServiceContract] interface IConnectionService { [OperationContract] [WebInvoke(Method = "POST", UriTemplate = "GetState", BodyStyle = System.ServiceModel.Web.WebMessageBodyStyle.Wrapped), Description("")]
[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实例
在类似的帖子中,People忘记添加DataContract和DataMember属性,但我确保添加了这些属性。解决方案可能是一些小而愚蠢的东西,但我看不出哪里出了问题servicecontract没有RequestFormat。在我的例子中,它需要WebMessageFormat.Json 我的服务合同现在如下所示:
[ServiceContract]
interface IConnectionService
{
[WebGet(UriTemplate = "GetState", BodyStyle = System.ServiceModel.Web.WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json), Description("")]
State GetState();
}
您的配置是否必须基于以下内容重新访问:?为什么这会成为一篇文章?