开发者

WCF Dynamic Response Format

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 interfac开发者_如何学Ce 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");
                }
        }
    }       


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜