简体   繁体   English

调用 wcf 休息服务并以 xml 或 json 形式返回数据

[英]call a wcf rest service and return data as xml or json

I'm trying to get data from a WCF REST service to come back as either xml or json.我正在尝试从 WCF REST 服务获取数据,以作为 xml 或 json 返回。

I can return data as xml or json when setting the RequestFormat and ResponseFormat using the WebInvoke attribute and setting both to the appropriate format as such:在使用 WebInvoke 属性设置 RequestFormat 和 ResponseFormat 并将两者设置为适当的格式时,我可以将数据作为 xml 或 json 返回:

But I don't want to hardcode the format, so researched it further on the web and I found a couple of things but no matter what I do, it always seems to return my data in xml format.但我不想对格式进行硬编码,所以在网上进一步研究了它,我发现了一些东西,但无论我做什么,它似乎总是以 xml 格式返回我的数据。

Here is what I've done:这是我所做的:

  1. Removed the RequestFormat and ResponseFormat from the WebInvoke attribute.从 WebInvoke 属性中删除了 RequestFormat 和 ResponseFormat。

     [OperationContract] [WebInvoke( Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "Search/{searchName}/{fieldValues}")] Result Search(string searchName, string fieldValues);
  2. I added the following section to my web.config:我在 web.config 中添加了以下部分:

     <system.serviceModel> <standardEndpoints> <webHttpEndpoint> <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/> </webHttpEndpoint> </standardEndpoints> </system.serviceModel>

    I don't know if the above is required since I was able to format in both format without having this in my web.config, but I guess, it does say automaticaFormat我不知道是否需要上述内容,因为我能够在没有在我的 web.config 中使用这两种格式的情况下进行格式化,但我想,它确实说的是 automaticaFormat

  3. When calling WebClient , I set both the Content-Type and Accept headers to application/xml or `application/json.调用WebClient ,我将Content-TypeAccept标头都设置为application/xml或`application/json。

    But even after making these changes and setting my headers to application/json , it still returns the data as XML.但即使在进行这些更改并将我的标头设置为application/json ,它仍然以 XML 形式返回数据。

    I tried it in Fiddler, but same thing.我在 Fiddler 中尝试过,但同样的事情。 Request is received, response is returned but in xml.收到请求,返回响应,但在 xml 中。

  4. I also tried putting the following code into the calling WCF method, but still no good:我也尝试将以下代码放入调用 WCF 方法中,但仍然不行:

     WebOperationContext.Current.OutgoingResponse.ContentType = "application/json";
  5. Finally, I found this article and followed the steps as far as I can tell, but no good once again!最后,我找到了这篇文章并按照我所知道的步骤进行了操作,但再次不行! Just look for "message format selection section".只需查找“消息格式选择部分”。

I'm obviously missing something, but what???我显然错过了一些东西,但是什么??? Has anyone got any ideas on how this can be achieved?有没有人对如何实现这一目标有任何想法?

What you are requesting is something I actually achieved today.你所要求的是我今天实际实现的。 I needed to create a RESTful service providing either JSON or XML in return.我需要创建一个 RESTful 服务来提供 JSON 或 XML 作为回报。 It is a bit domain specific, but if your read between the lines the following blog shows how to achieve this by means of a MemoryStream return type:它是特定于域的,但如果您在两行之间阅读以下博客,则显示了如何通过 MemoryStream 返回类型实现此目的:

http://www.j4jayant.com/articles/fhir/22-fhir-feel-2 http://www.j4jayant.com/articles/fhir/22-fhir-feel-2

In the GetResourceFeed function, you can see how the Accept value in the request is used to switch between JSON and XML (atom).在 GetResourceFeed 函数中,您可以看到如何使用请求中的 Accept 值在 JSON 和 XML(原子)之间切换。 Please feel free to try it out.请随意尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM