WCF BodyStyle WrappedRequest对传入的JSON参数不起作用?

ada*_*iko 10 rest wcf json web-services web

我一直在努力获得RESTful WCF服务,以接受JSON作为参数并返回一些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;
    }
Run Code Online (Sandbox Code Playgroud)

当我通过{AuthRequest:{TokenId ="some guid"}}时,上面将数据设置为null.

如果我将方法的BodyStyle设置为Bare,那么数据设置正确但我必须从JSON中删除{AuthRequest}(我真的不想这样做).有没有办法让WrappedRequests使用{AuthRequest:{TokenId ="some guid"}作为JSON?

谢谢.

car*_*ira 20

包装器的名称不是参数类型,而是参数名称.如果你发送{"data":{"TokenId":"some guid"}}它应该工作.

或者,如果要使用参数名称以外的某个名称,则可以使用以下[MessageParameter]属性:

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