tags:

views:

100

answers:

3

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();

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?

+1  A: 

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.

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.

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.

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.

KeithS
A: 

I don't know of a way where you can get 2 outputs from a service operation. You can always get the XML (serialized DataContract) and then "JSON Serialize" it. 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();
            }
        }
Josh
+1  A: 

It is possible in .NET 4.0 but not in the way you specified it. .NET 4.0 adds new parameter to WebHttp behavior:

  <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

So if you call your REST service with JSON request you will get JSON. If you call it with POX request you will get XML. Full description of automatic format selection is in MSDN.

Ladislav Mrnka