WCF BodyStyle WrappedRequest不适用于传入的JSON参数吗? [英] WCF BodyStyle WrappedRequest doesn't work for incoming JSON param?

查看:480
本文介绍了WCF BodyStyle WrappedRequest不适用于传入的JSON参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力获取RESTful WCF服务,以接受JSON作为参数并返回一些JSON.

I've been working on getting a RESTful WCF service to both accept a JSON as a parameter and return some JSON.

这是我的服务

    [OperationContract]
    [WebInvoke(
        Method="POST",
        BodyStyle = WebMessageBodyStyle.WrappedRequest,
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,
        UriTemplate = "Authenticate")]
    public AuthResponse Authenticate(AuthRequest data)
    {
        AuthResponse res = new AuthResponse();
        if (data != null)
        {
            Debug.WriteLine(data.TokenId);
            res.TokenId = new Guid(data.TokenId);
        }
        return res;
    }

当我通过{AuthRequest:{TokenId ="some guid"}}时,以上内容会将 data 设置为null.

The above will set data to be null when I pass { AuthRequest: { TokenId = "some guid"} }.

如果我将方法的BodyStyle设置为Bare,则正确设置了 data ,但是我必须从JSON中删除{AuthRequest}(我并不是很想这样做).有什么方法可以使WrappedRequests与{AuthRequest:{TokenId ="some guid"}}一起使用作为JSON?

If I set the BodyStyle of the method to be Bare then data is set correctly but I must remove { AuthRequest } from the JSON (which I don't really want to do). Is there any way to get WrappedRequests to work with { AuthRequest: { TokenId = "some guid"} as the JSON?

谢谢.

推荐答案

包装程序的名称不是参数 type ,而是参数 name .如果以{"data":{"TokenId":"some guid"}}的形式发送,则应该可以.

The name of the wrapper is not the parameter type, but the parameter name. If you send it as {"data":{"TokenId":"some guid"}} it should work.

或者,如果您想使用参数名称以外的其他名称,则可以使用[MessageParameter]属性:

Or if you want to use some name other than the parameter name, you can use the [MessageParameter] attribute:

[OperationContract]
[WebInvoke(
    Method="POST",
    BodyStyle = WebMessageBodyStyle.WrappedRequest,
    RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json,
    UriTemplate = "Authenticate")]
public AuthResponse Authenticate([MessageParameter(Name = "AuthRequest")] AuthRequest data)

这篇关于WCF BodyStyle WrappedRequest不适用于传入的JSON参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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