WCF 动态响应格式

发布于 2024-12-02 03:54:43 字数 1907 浏览 0 评论 0原文

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

我想根据用户在消息正文中指定的内容动态输出响应格式。

例如,如果用户输入“json”、“xml”、“soap”,它将返回各自的格式。提前致谢。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谜兔 2024-12-09 03:54:43

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

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

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.

淑女气质 2024-12-09 03:54:43

您不能在同一个端点中拥有 SOAP 和 JSON(或 POX -“普通旧 XML”)响应。 SOAP 是一种协议,它规定了请求和响应需要如何格式化 - 根据 SOAP 信封版本、SOAP 寻址标头(或缺少它们)等。如果端点“对话”SOAP,则它不能对话“非肥皂”。

要在 JSON 和 XML(即 POX)之间进行更改,您可以在操作过程中指定要在单个端点的返回中使用的格式。端点需要是无 SOAP 的(即,其绑定必须具有 MessageVersion.None,例如 WebHttpBinding),并且应用了 Web 行为(通常是WebHttpBehavior,或 (如果在配置中定义)。此类端点通常称为 WCF WebHttp 端点或(名称错误)REST 端点。

您的示例是针对 Web 端点执行此操作的一种方法,但如果您将响应格式设置为 XML,则将内容类型设置为 application/json,这可能会破坏您的客户端。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文