WCF BodyStyle WrappedRequest 不适用于传入的 JSON 参数?

发布于 2024-12-02 13:16:43 字数 863 浏览 0 评论 0原文

我一直致力于让 RESTful WCF 服务接受 JSON 作为参数并返回一些 JSON。

这是我的服务:

<前><代码> [操作合同] [网络调用( 方法=“POST”, BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate =“身份验证”)] 公共AuthResponse验证(AuthRequest数据) { AuthResponse res = new AuthResponse(); 如果(数据!=空) { 调试.WriteLine(data.TokenId); res.TokenId = new Guid(data.TokenId); } 返回资源; }

当我传递 { AuthRequest: { TokenId = "some guid"} } 时,上面会将 data 设置为 null。

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

谢谢。

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

This is my service:

    [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;
    }

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

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?

Thanks.

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

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

发布评论

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

评论(2

梦魇绽荼蘼 2024-12-09 13:16:43

包装器的名称不是参数type,而是参数name。如果您将其作为 {"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)

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.

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)
冷月断魂刀 2024-12-09 13:16:43

这是一个迟到的回复,但我希望它对某人有所帮助。

我还尝试让 JSON“POST”Web 服务正常工作,但它的参数始终设置为 null。忘记尝试反序列化任何 JSON 数据吧,根本没有什么可做的!

public string CreateNewSurvey(string JSONdata)
{
    if (JSONdata == null)
        return "JSON received: NULL, damn.";
    else
        return "You just sent me: " + JSONdata;
}

我的 GET Web 服务运行得很好,但是这个“POST”服务让我抓狂。

奇怪的是,我的解决方案是将参数类型从 string 更改为 Stream

public string CreateNewSurvey(Stream JSONdataStream)
{
    StreamReader reader = new StreamReader(JSONdataStream);
    string JSONdata = reader.ReadToEnd();

    //  Finally, I can add my JSON deserializer code in here!

    return JSONdata;
}

...以及在我的 web.config 中...

  [OperationContract(Name = "createNewSurvey")]
  [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "createNewSurvey")]
  string CreateNewSurvey(Stream JSONdata);   

完成此操作后,我的 iPad 应用程序最终可以调用我的 WCF 服务。我是一个快乐的人!希望这有帮助。

This is a late reply, but I hope it helps somebody.

I was also trying to get a JSON "POST" web service to work, but it's parameter was always being set as null. Forget about trying to deserialize any JSON data, there was never anything there to work on!

public string CreateNewSurvey(string JSONdata)
{
    if (JSONdata == null)
        return "JSON received: NULL, damn.";
    else
        return "You just sent me: " + JSONdata;
}

My GET web services worked great, but this "POST" one drove me nuts.

My solution, oddly, was to change the parameter type from string to Stream.

public string CreateNewSurvey(Stream JSONdataStream)
{
    StreamReader reader = new StreamReader(JSONdataStream);
    string JSONdata = reader.ReadToEnd();

    //  Finally, I can add my JSON deserializer code in here!

    return JSONdata;
}

...and in my web.config...

  [OperationContract(Name = "createNewSurvey")]
  [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "createNewSurvey")]
  string CreateNewSurvey(Stream JSONdata);   

With this in place, finally my iPad application could call my WCF Service. I was a happy man! Hope this helps.

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