WCF:生成JSON不起作用

时间:2016-01-10 23:04:13

标签: json wcf devexpress xaf

我在我的应用程序中使用DevExpress,XAF和XPO。我需要从Web服务公开我的数据。 ASP.NET Web API V2与XPO对象不兼容......(如果你发现了......我会接受它!)。

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

  • MyContext继承自XpoContext
  • MyService继承自XpoDataServiceV3(该类具有属性:[JSONPSupportBehavior])

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

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

我在WebGet属性上找到了各种属性:RequestFormat,ResponseFormat,BodyStyle,UrlTemplate。在Format属性上,我可以在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编写的。

我们没关系,我已经使用格式属性配置了我的查询...:

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

1 个答案:

答案 0 :(得分:1)

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

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