简体   繁体   English

从WCF返回多个响应

[英]Return multiple response from WCF

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 我在服务中有一个WCF服务和一个名为GetStudentList()的方法。当它返回单个响应时工作正常。这样的事情

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

But i want to return multiple response ie xml and json both.something like this 但我想返回多个响应,即xml和json都是这样的

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

Is it possible?if yes then how? 有可能吗?如果是,那怎么样?

It is possible in .NET 4.0 but not in the way you specified it. 它可能在.NET 4.0中,但不是以您指定的方式。 .NET 4.0 adds new parameter to WebHttp behavior: .NET 4.0为WebHttp行为添加了新参数:

  <endpointBehaviors>
    <behavior name="WebHttp">
      <webHttp automaticFormatSelectionEnabled="true" />
    </behavior>
  </endpointBehaviors>

When you use automatic format selection the format of the response is based on: 使用自动格式选择时,响应的格式基于:

  • Accept header from the request 从请求中接受标头
  • Content type of the request 请求的内容类型
  • Default format specified on operation 操作时指定的默认格式
  • Default format specified in webHttp behavior webHttp行为中指定的默认格式

So if you call your REST service with JSON request you will get JSON. 因此,如果您使用JSON请求调用REST服务,您将获得JSON。 If you call it with POX request you will get XML. 如果您使用POX请求调用它,您将获得XML。 Full description of automatic format selection is in MSDN . 自动格式选择的完整描述在MSDN中

I do not think it is possible to return the object as both Json and XML using one call. 我不认为可以使用一个调用将对象作为Json和XML返回。 Think of WCF like a normal method call in this regard; 在这方面,可以将WCF视为正常的方法调用; you call one method, you get one serialized return value. 你调用一个方法,你得到一个序列化的返回值。 Once the service has returned one response to the caller, the call is complete. 一旦服务向呼叫者返回了一个响应,呼叫就完成了。

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. 它们都是对象序列化的信息性通用标准,并且使用WCF,如果直接使用序列化响应文本,则只需要两者。 If at all possible, I would refactor the clients to work with the same response type. 如果可能的话,我会重构客户端以使用相同的响应类型。

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. 您必须通过名称(GetStudentListJSON与GetStudentListXML)或通过在不同的服务类中查找方法来分隔它们。

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. 这确实要求您使用在调用的客户端控制的.NET代码。

I don't know of a way where you can get 2 outputs from a service operation. 我不知道如何从服务操作中获得2个输出。 You can always get the XML (serialized DataContract) and then "JSON Serialize" it. 您始终可以获取XML(序列化DataContract),然后“JSON Serialize”。 Here is how you can do it. 这是你如何做到的。

List<Student> studentList = GetStudent();
string jsonString = JsonSerialize(studentList.GetType(), studentList);

And then add this function to a utility class: 然后将此函数添加到实用程序类:

public static string JsonSerialize(Type type, object objectGraph)
        {
            MemoryStream memoryStream = new MemoryStream();

            try
            {
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(type);
                serializer.WriteObject(memoryStream, objectGraph);
                return Encoding.Default.GetString(memoryStream.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (memoryStream != null) memoryStream.Close();
            }
        }

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

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