2016-01-10 48 views
1

我正在为我的应用程序使用DevExpress,XAF和XPO。我需要从web服务公开我的数据。 ASP.NET Web API V2与XPO对象不兼容...(如果您发现如何...我会接受它!)。WCF:生成JSON不起作用

的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

我找到了!在你的WCF服务全球班上,写下如下代码:

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