混合XML和JSON REST风格的WCF没有单独的方法 [英] Mixing XML and JSON in RESTful WCF without separate methods

查看:181
本文介绍了混合XML和JSON REST风格的WCF没有单独的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RESTful的WCF服务,可以返回XML,JSON或JSONP,根据不同的参数,例如 /service.svc/stuff?format=xml service.svc /东东格式= JSON和放大器;回调= myCallBack函数 。要做到这一点,我创建了一个自定义行为,MethodEncoder和MethodEncoderFactory其基于全格式参数包裹JSONP回调,并选择作家。在我的编码器的的WriteMessage()的方法,我做这样的事情。

I have a RESTful WCF service that can return XML, JSON, or JSONP, depending on the arguments, e.g. /service.svc/stuff?format=xml or service.svc/stuff?format=json&callback=myCallback. To do this, I've created a custom Behavior, MethodEncoder and MethodEncoderFactory which handle wrapping the JSONP callback and chooses the writer based on the format argument. In my encoder's WriteMessage() method, I do something like

XmlWriter writer = IsXmlRequested() ? XmlDictionaryWriter.CreateTextWriter(stream) :
  JsonReaderWriterFactory.CreateJsonWriter(stream)
message.WriteMessage(writer);



然后,我定义我的服务方式,如果他们只是返回JSON,但用我的自定义绑定元素:

Then, I define my service methods as if they just return JSON but use my custom binding element:

[OperationContract, JSONPBehavior, WebGet(ResponseFormat = WebMessageFormat.Json,
  UriTemplate = "stuff")
public List<Thing> GetStuff(){...}

和几乎工作。当我问XML或JSON,我得到正确的格式的东西,但我预计XML不是序列。这里的XML是什么样子:

And it almost works. When I ask for XML or JSON, I get something in the right format, but the XML isn't serialized as I expect. Here's what the XML looks like:

<root type="array">
 <item type="object">
  <FirstPropertyOnAThing>1</FirstPropertyOnAThing>



而如果我是刚刚设置的WebMessageFormat为XML,我会得到这样的:

Whereas if I were to just set the WebMessageFormat to XML, I would get something like this:

<ArrayOfThings xmlns="...>
 <Thing ...>
  <FirstPropertyOnAThing>1</FirstPropertyOnAThing>

我肯定要后者我想这种情况正在发生,因为。在创建Message对象时,结果是序列化到一个字典;我的自定义编码仅仅是决定如何编写词典响应流因此它的编码正确的,但不完全的格式,它已经被ResponseFormat决定

I definitely want the latter. I guess this is happening because the result is serialized to a dictionary when the Message object is created; my custom encoder is just deciding how to write that dictionary to the response stream. So it gets the encoding right, but not exactly the format, which has already been decided by the ResponseFormat.

首先,是这样吗?如果是这样,我怎么能解决这个问题呢?比如,我可以写我自己的WebMessageFormat?或者我只是让步,写有不同ResponseFormat属性/ JSON / *和/ XML / *

First, is that right? If so, how can I fix this? For example, can I write my own WebMessageFormat? Or do I just have to give in and write separate methods (and URI templates) that have different ResponseFormat properties for /json/* and /xml/*?

更新单独的方法(和URI模板):在。网4,还有就是你可以只设置一个 WebOperationContext.Current.OutgoingResponse.Format 属性。我想我的问题归结为:有没有办法来完成,在.NET 3.5

Update: In .net 4, there's a WebOperationContext.Current.OutgoingResponse.Format property you can just set. I guess my issue boils down to: is there a way to accomplish that in .net 3.5?

推荐答案

是的,有办法吗?要完成你想要什么.NET 3.5,而无需编写单独的方法。

Yes, there's a way to accomplish what you want in .NET 3.5, without writing separate methods.

这个博客帖子与你所描述的情况的交易:不同的内容类型基于传入请求的响应。不过,帖子描述了一个稍微不同的方法来解决。

This blog post deals with the situation you describe: varying the content-type of the response based in the incoming request. But, the post describes a slightly different approach to the solution.

在特定的,请求指定所需的内容类型没有请求的URL,而在接受请求头。

In particular, the requester specifies the desired content type NOT in the request URL, but rather in the Accept header of the request.

该解决方案包括使用一个自定义的 WebHttpBehavior 的检查Accept头并适当格式的响应。一个非常优雅的解决方案,在我看来。没有什么你在你的业务逻辑做的就是自适应格式。只是附加的行为和它的作品。

The solution involves the use of a custom WebHttpBehavior that inspects the Accept header and formats the response appropriately. A very elegant solution, in my opinion. There's nothing you have to do in your business logic to get the adaptive formatting. Just attach the behaviour and it works.

还检查出的 WCF REST的Contrib在CodePlex库

这篇关于混合XML和JSON REST风格的WCF没有单独的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆