WCF BodyStyle WrappedRequest 不适用于传入的 JSON 参数?
我一直致力于让 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
包装器的名称不是参数type,而是参数name。如果您将其作为
{"data":{"TokenId":"some guid"}}
发送,它应该可以工作。或者,如果您想使用参数名称以外的其他名称,可以使用
[MessageParameter]
属性: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:这是一个迟到的回复,但我希望它对某人有所帮助。
我还尝试让 JSON“POST”Web 服务正常工作,但它的参数始终设置为 null。忘记尝试反序列化任何 JSON 数据吧,根本没有什么可做的!
我的 GET Web 服务运行得很好,但是这个“POST”服务让我抓狂。
奇怪的是,我的解决方案是将参数类型从 string 更改为 Stream。
...以及在我的 web.config 中...
完成此操作后,我的 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!
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.
...and in my web.config...
With this in place, finally my iPad application could call my WCF Service. I was a happy man! Hope this helps.