1

How do I create a dynamic response with out using the query string?

I want to dynamically output the response format based on what the user specifics inside of the message body.

For example, if the user inputs "json","xml","soap", it will return the respective format. Thanks, in advance.

public interface IReg
{
    [OperationContract]
    [WebInvoke]
    MemberBasic Login(string uniqueID, string password, string returnFormat);
}

[DataContract(Namespace = "", IsReference=false)]
[Serializable()]
public class MemberBasic
{

    #region Properties
    [DataMember]
    public DateTime LastModified
    {
        get;
        set; 
    }
}
 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public sealed class RWS : IReg
{


    public MemberBasic Login(string uniqueID, string password, string returnFormat)
    {
        MemberBasic result = new MemberBasic();
        setReturnFormat(returnFormat);
        return result;
    }
}
private static void Init(string returnFormat)
    {
        var response = WebOperationContext.Current.OutgoingResponse;
        response.Headers.Add("cache-Control", "no-cache");
        response.Headers.Add("Last-Modified", string.Format("{0:r}", DateTime.Today));

        switch (returnFormat)
        {
            case "xml":
                {
                    WebOperationContext.Current.OutgoingResponse.Format = WebMessageFormat.Xml;
                    WebOperationContext.Current.OutgoingRequest.Headers.Add(System.Net.HttpRequestHeader.ContentType, "application/json");
                } break;
            case "json":
                {
                    WebOperationContext.Current.OutgoingResponse.Format = WebMessageFormat.Json;
                } break;
            default:
                {
                    throw new ArgumentException("Return Format unrecognized; cannot complete request.",
                        "returnFormat");
                }
        }
    }       
James
  • 9,694
  • 5
  • 32
  • 38

2 Answers2

3

The simplest way to do what you are after is to create different endpoints with different bindings. You can have one for POX, SOAP and JSON. They can share contracts and implementations but WCF/configuration is responsible for managing the request/response formats.

It doesn't make a lot of sense to have SOAP specified as the response format since, in WCF it would mean the request would also have to be a SOAP request.

Phil Degenhardt
  • 7,215
  • 3
  • 35
  • 46
  • That's true. I already have away to support multiple request using query string. However, I want see if there is another way of doing it. It can be done with out multiple end points. I want the user to specify the return format. – James Aug 30 '11 at 03:06
  • So do you need to support a scenario, say, where the client sends a request in JSON format but wants a SOAP response? I'm happy to be corrected by smarter brains than mine but I don't believe WCF supports such a scenario. What sort of client are you running that can handle different message formats for request and response? – Phil Degenhardt Aug 30 '11 at 03:45
  • I think its possible its just no one has given it a try. I want the user to control their response format. Why should they be limited? If they are limited that means there is limitation in my design. I'll put one more day of effort into this project. Thanks, for your response. – James Aug 30 '11 at 13:49
  • The limitation is not in your design it is a limitation of the layered architectures used for inter process communication. The SOAP protocol effectively binds the request and response message formats together. Sending a SOAP request and getting a POX response is simply not supported and can not be utilised by any client, other than one you write yourself. The only conceivable way of doing what you're attempting would be to make the operations one-way and then have the server make a callback on a new channel to the client. But this seems a whole lot of work for no real benefit. – Phil Degenhardt Aug 31 '11 at 01:21
2

You cannot have in a same endpoint a SOAP and a JSON (or POX - "plain old XML") response. SOAP is a protocol which dictates how the request and the response need to be formatted - according to the SOAP envelope version, SOAP addressing headers (or absence of them), etc. If an endpoint "talks" SOAP, it cannot talk "non-SOAP".

For changing between JSON and XML (i.e., POX), you can specify as part of the operation the format which you want to use in the return in a single endpoint. The endpoint needs to be SOAP-less (i.e., its binding must have MessageVersion.None, such as the WebHttpBinding), and have a Web behavior applied to it (usually the WebHttpBehavior, or <webHttp/> if defined in config). Such endpoints are often known as WCF WebHttp endpoints or (rather misnamed) REST endpoints.

Your example is one way to do that for Web endpoints, although you're setting the content-type to application/json if you set the response format to XML, which will likely break your clients.

carlosfigueira
  • 85,035
  • 14
  • 131
  • 171
  • Thanks, so there is no no way to let the user define the response format? – James Aug 30 '11 at 04:02
  • 1
    Yes, they can. If between POX and JSON, it can be done via a parameter like you have. But if it's between POX and SOAP, then not via a parameter, but via the endpoint address instead (one endpoint for POX, one for SOAP). – carlosfigueira Aug 30 '11 at 04:38