如何从WCF返回Json序列化数据?

时间:2013-07-15 16:06:46

标签: c# json wcf serialization

我已经环顾四周,看到可以从WCF Web服务返回序列化为Json的对象。有谁知道我怎么能这样做?

由于

2 个答案:

答案 0 :(得分:1)

您必须像这样添加属性

    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json)]
    ObjectName YourMethodName();

答案 1 :(得分:0)

是的,您可以将 automaticFormatSelectionEnabled 设置为web.config中的webHttpEndpoint的true standardEndpoint,如

<webHttpEndpoint>
  <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
</webHttpEndpoint>

您需要为您的客户端添加json响应的http标头

using (HttpClient client = new HttpClient("endpoint"))
{
     HttpRequestMessage request = new HttpRequestMessage("GET", "SomeMethod");                
     request.Headers.Accept.AddString("application/json");
    ...
}
相关问题