Click here to Skip to main content
15,031,680 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
VB
how to make wcf service method which returns the data in both xml and json,
in short how to enable wcf service method which return dynamic output either xml or json.


i made one method that meet my requirement but i know its not true method to do this.

my code:

INTERFACE :

C#
[OperationContract]
        [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "/ReturnTime?ReturnType={ReturnType}")]
        string ReturnTime(String ReturnType);



METHOD :


C#
public string ReturnTime(String ReturnType)
        {
            if (ReturnType != "" && ReturnType != null)
            {
                if (ReturnType.ToUpper() == "STRING" || ReturnType.ToUpper() == "JSON" || ReturnType.ToUpper() == "XML")
                {
                    if (ReturnType.ToUpper() == "JSON")
                        return "[{\"Current Time is \":" + DateTime.Now.ToString() + "}]";
                    else if (ReturnType.ToUpper() == "XML")
                        return "<CurrentTimeIs>" + DateTime.Now.ToString() + "</CurrentTimeIs>";
                    else if (ReturnType.ToUpper() == "STRING")
                        return "Current time is : " + DateTime.Now.ToString();
                    else
                        return "<CurrentTimeIs>" + DateTime.Now.ToString() + "</CurrentTimeIs>";
                }
                else
                {
                    return "<ErrorMessage><Message>" + ReturnType + " as returntype is not available. Please use JSON,XML OR STRING.</Message></ErrorMessage>";
                }
            }
            else
            {
                return "<ErrorMessage><Message>Please provide Returntype either JSON,XML OR STRING.</Message></ErrorMessage>";
            }
        }


i m doing this. i know wcf has power to do this but how it does i dont' know plz help me to achieve this. thanx in advance.
Posted
Updated 9-Dec-13 22:41pm
v2

you can check
http://blogs.msdn.com/b/dotnetinterop/archive/2008/11/04/rest-in-wcf-varying-response-content-type-based-on-http-request-headers.aspx
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900