WCF BodyStyle WrappedRequest 不适用于传入的 JSON 参数? [英] WCF BodyStyle WrappedRequest doesn't work for incoming JSON param?
问题描述
我一直致力于让 RESTful WCF 服务既接受 JSON 作为参数又返回一些 JSON.
这是我的服务:
<块引用> [操作契约][网络调用(方法=POST",BodyStyle = WebMessageBodyStyle.WrappedRequest,RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json,UriTemplate = "验证")]公共 AuthResponse Authenticate(AuthRequest 数据){AuthResponse res = new AuthResponse();如果(数据!= null){Debug.WriteLine(data.TokenId);res.TokenId = new Guid(data.TokenId);}返回资源;}
当我通过 { AuthRequest: { TokenId = "some guid"} } 时,上面会设置 data 为空.
如果我将方法的 BodyStyle 设置为 Bare,则 data 设置正确,但我必须从 JSON 中删除 { AuthRequest } (我真的不想这样做).有没有办法让 WrappedRequests 与 { AuthRequest: { TokenId = "some guid"} 作为 JSON 一起使用?
谢谢.
包装器的名字不是参数type,而是参数name.如果您将其作为 {"data":{"TokenId":"some guid"}}
发送,它应该可以工作.
或者如果你想使用参数名称以外的其他名称,你可以使用[MessageParameter]
属性:
[操作契约][网络调用(方法=POST",BodyStyle = WebMessageBodyStyle.WrappedRequest,RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json,UriTemplate = "验证")]public AuthResponse Authenticate([MessageParameter(Name = "AuthRequest")] AuthRequest data)
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.
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)
这篇关于WCF BodyStyle WrappedRequest 不适用于传入的 JSON 参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!