接受 POST 的多种表示
我正在快速了解 WCF Web API。我想通过 POST 方法公开一个可以接受注释的端点。我的问题是,我想支持笔记的多种表示形式。例如,我可能想要使用我们在其他地方使用的自定义 XML 序列化或作为 atom:entry
元素接受注释。我已经有了格式化程序,可以将它们反序列化为 Note
类(我们自己的自定义类)或作为 SynminationItem
。
但问题来了,如何定义方法呢?我目前得到了这个:
[WebInvoke(UriTemplate = GetNotesUriRoot,Method="POST")]
public HttpResponseMessage PostNote(ObjectContent item,HttpRequestMessage request)
启动时失败:
服务操作“PostNote”永远不会接收“ObjectContent”类型的输入参数“item”的值。确保请求 HttpOperationHandler 具有可分配给“ObjectContent”类型的输出参数。
我最初尝试使用两个单独的方法(具有适当类型的参数),但它们不能共享相同的端点名称。当前的工作(使用 ObjectContent
)基于我发现的其他帖子,这些帖子表明它可能是一个参数。 Note
和 SyndicateItem
之间没有通用的基本类型或接口,
我们使用的是 WCF Web API v0.6.0
I'm getting up to speed with the WCF Web API. I want to expose an endpoint that can accept notes, via the POST method. My issue is, I want to support multiple representations for notes. For example, I might want to accept a note using a custom XML serialization that we're using elsewhere, or as an atom:entry
element. I already have formatters that can deserialize these into a Note
class (our own custom class) or as a SyndicationItem
.
The question comes though, how do I define the method? I've currently got this:
[WebInvoke(UriTemplate = GetNotesUriRoot,Method="POST")]
public HttpResponseMessage PostNote(ObjectContent item,HttpRequestMessage request)
Which fails when starting up:
The service operation 'PostNote' will never receive a value for the input parameter 'item' of type 'ObjectContent'. Ensure that a request HttpOperationHandler has an output parameter with a type assignable to 'ObjectContent'.
I initially tried having two separate methods (with appropriately typed parameters), but they can't share the same endpoint name. The current effort (using ObjectContent
) was based on other posts I could find that suggested that it could be a parameter. There is no common base type or interface between Note
and SyndicationItem
We're using v0.6.0 of the WCF Web API
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要有一个 Note 类型的参数/返回类型,并且您的格式化程序会将其序列化(反)序列化为所需的表示形式或从所需的表示形式进行序列化。
You need to have a parameter / return type of type Note and your formatters will (de-)serialize it to / from the required representation.
那么在您的请求中,内容类型标头将确定对象的反序列化方式。您无需担心决定如何反序列化,只要相关的格式化程序存在,就会为您做出决定(我还没有深入研究格式化程序,因为到目前为止 json/xml 对我来说已经足够了)
then in your request the content-type header will determine how the object is deserialised. You don't need to worry about deciding how to deserialise, the decision is made for you, as long as the relevant formatter exists (I've not delved in to formatters yet as json/xml have been enough for me so far)