WCF 动态响应格式 [英] WCF Dynamic Response Format

查看:50
本文介绍了WCF 动态响应格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不使用查询字符串的情况下创建动态响应?

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.

例如,如果用户输入json"、xml"、soap",则会返回各自的格式.提前致谢.

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");
                }
        }
    }       

推荐答案

最简单的方法是创建具有不同绑定的不同端点.您可以拥有一个用于 POX、SOAP 和 JSON.他们可以共享契约和实现,但 WCF/configuration 负责管理请求/响应格式.

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.

将 SOAP 指定为响应格式没有多大意义,因为在 WCF 中这意味着请求也必须是 SOAP 请求.

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.

这篇关于WCF 动态响应格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆