WCF 消耗多个可能的 REST 响应
我最近开始阅读有关 WCF 中的 WebHttpBinding 用法以及能够使用 REST 服务的内容,但是,我现在对此有点困惑。
我正在编写一项服务,该服务向给定的 api 发出单个请求,但可以返回多个响应之一。
例如,通用响应:
<ActualResponse>
<ResponseItem>
<Name />
<Area />
</ResponseItem>
</ActualResponse>
但是,如果传出请求中的某些内容无效或响应服务本身遇到任何类型的问题,则返回的响应将是:
<ErrorResponse>
<Message />
</ErrorResponse>
Pedram Rezaei 在 使用 REST 服务,这是我的大部分信息都是借用的。 据我所知,只要该对象定义了可序列化的属性,我们就可以创建该对象。 问题是,创建哪个类没有任何条件(ErrorResponse/ActualResponse)。
我不确定是否应该查看某种 TryParse 功能,如果无法发生反序列化或者是否有更优雅的方法,该功能会发送初始请求并捕获错误。
我对 WCF 世界相当陌生,所以我可能完全忽略了一些东西!
I've recently started reading about the WebHttpBinding usage in WCF and being able to consume REST services, however, I've been stumped on this one for a bit now.
I'm writing a service that makes a single request to a given api, however, can return one of many responses.
For example, the generic response:
<ActualResponse>
<ResponseItem>
<Name />
<Area />
</ResponseItem>
</ActualResponse>
However, if something were invalid in the outgoing request or the responding service itself was experiencing any sort of issue a returning response would be:
<ErrorResponse>
<Message />
</ErrorResponse>
Pedram Rezaei had a great post on consuming REST services, which is where I borrow most of my information from. From what I can tell, we can create an object as long as the object has serializable attributes defined. The issue being, there's no condition to which class to be created(ErrorResponse/ActualResponse).
I'm unsure if I should be looking at some sort of TryParse functionality that sends the initial request and catches the error if no deserialization can occur or if there is a more elegant approach.
I'm fairly new to the WCF world, so the possibility exists I may be entirely overlooking something!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你可以借鉴 SOAP 的一些做法,它有这样的层次结构:
我不是建议你使用 SOAP,而是建议你学习 SOAP 采用的设计。 SOAP 的作用是将成功的(或者用您的话来说是“实际的”)响应嵌入到正文中,或返回soap:体内故障。
SOAP 中的成功可能看起来像这样:
而错误可能看起来像这样:
在您的情况下,您可能会遇到这样的情况:
或
XML 序列化在这方面确实很擅长。 。 。
但听起来你无法控制信封。 事实上,您可以获得多种不同的响应。 为了解决这个问题,您可以将收到的实际响应包装在一个设计的 XML 信封中,然后反序列化该结果。
如果您收到... ,将其包装在可反序列化的信封中以获得类似... 的内容,然后进行反序列化。
I think you can borrow some practice from SOAP, which has a hierarchy like so:
I'm not suggesting that you use SOAP, I'm suggesting that you learn from the design employed by SOAP. What SOAP does is embed the successful (or in your words "actual") response within the Body, or return a soap:Fault within the Body.
a success in SOAP might look like this:
while a fault might look like this:
In your case, you might have this:
or
And XML Serialization is really good at that. . .
But it sounds like you have no control over the envelope. The fact is that you can get multiple different responses. To handle this, you could wrap the actual response received in a contrived XML envelope, and deserialize the result of that.
If you get <ActualResponse>...</ActualResponse> , wrap it in a deserializable envelope to get something like <ServiceResponse><ActualResponse>...</ActualResponse></ServiceResponse>, then deserialize.