返回从WCF多响应 [英] Return multiple response from WCF

查看:135
本文介绍了返回从WCF多响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个WCF服务,并命名GetStudentList(单一法)service.It的工作正常时是这样的。

Hi I have one WCF service and one single method named GetStudentList() in the service.It's working fine when it's return single response.Something like this

  [WebGet(ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
List<Student> GetStudentList();



不过,我想回到比如XML和JSON both.something这样

But i want to return multiple response i.e. xml and json both.something like this

  [WebGet(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
[WebGet(ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
List<Student> GetStudentList();



这可能吗?如果是的话怎么样?

Is it possible?if yes then how?

推荐答案

我不认为这是可能返回对象既JSON和使用一个电话XML。这样想在这方面的一个正常的方法调用WCF的;你调用一个方法,你得到一个序列化的返回值。一旦服务已返回一个响应给调用者,调用完成

I do not think it is possible to return the object as both Json and XML using one call. Think of WCF like a normal method call in this regard; you call one method, you get one serialized return value. Once the service has returned one response to the caller, the call is complete.

仔细想想为什么要使用这两种响应类型;他们为对象的序列化知识性,普及标准,并使用WCF,你只需要同时如果你直接使用序列化的响应文本。如果可能的话,我会重构客户提供相同的响应类型的工作。

Think carefully about why you want to use both response types; they are both informative, universal standards for object serialization, and using WCF, you would only need both if you were using the serialized response text directly. If at all possible, I would refactor the clients to work with the same response type.

最简单的解决办法,如果真的需要两种类型,会提供两个这种方法的重载,使每一个客户类型足够聪明,知道哪些调用它需要做出。因为不同的是不是在方法签名,它不是一个真正的过载;你必须给他们的名字(GetStudentListJSON VS GetStudentListXML),或通过查找在不同的服务类中的方法是独立的。

The simplest workaround, if two types really are needed, would be to provide two "overloads" of this method, and make each client type smart enough to know which call it needs to make. Because the difference is not in the method signature, it's not a true overload; you'll have to separate them either by name (GetStudentListJSON vs GetStudentListXML) or by locating the methods in different service classes.

您也可以随时返回一个响应类型,并且在客户端反序列化/ reserializing当你需要在其他格式的序列化的对象转换。这确实需要在使用,你可以控制在电话会议上的客户端.NET代码是

You could also always return one response type, and convert on the client side by deserializing/reserializing when you need the object serialized in the other format. This does require you to be using .NET code that you have control over on the client side of the call.

这篇关于返回从WCF多响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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