WCF:生成 JSON 无效

WCF : Generate JSON not works

我正在为我的应用程序使用 DevExpress、XAF 和 XPO。我需要从网络服务公开我的数据。 ASP.NET Web API V2 与 XPO 对象不兼容...(如果您找到了方法...我会接受它!)。

DevExpress 向导可以帮助我生成一个 WCF Web 服务项目,其中

我会得到我的 XPO 对象列表,为此,我编写了下一个代码

[WebGet]
public IQueryable<MyType> Get()
{
    return new XPQuery<MyType>(new UnitOfWork());
}

我在 WebGet 属性上找到了各种属性:RequestFormat、ResponseFormat、BodyStyle、UrlTemplate。在格式属性上,我可以在 WebMessageFormat.Json 和 WebMessageFormat.Xml 之间进行选择。从逻辑上讲,我输入 WebMessageFormat.Json.

当我继续使用我最喜欢的网络浏览器或提琴手时,我会执行以下请求:

GET http://localhost:51555/MyService.svc/Get HTTP/1.1
User-Agent: Fiddler
Host: localhost:51555
Content-Type: application/json

但这不起作用...响应是:

HTTP/1.1 200 OK
Cache-Control: no-cache
Content-Length: 24250
Content-Type: application/atom+xml;type=feed;charset=utf-8
Server: Microsoft-IIS/10.0
...

内容写在XML。

没问题,我已使用格式属性配置我的查询...:[=​​15=]

[WebGet(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]

我找到了!在您的 WCF 服务全局 class 上,编写了以下代码:

HttpContext.Current.Request.Headers.Add("Accept", "application/json;");