将多个参数传递到 WCF Web API 服务
我想知道创建 WCF-Web 服务时发生的幕后魔力。
在一个旧项目中,我得到了可以从 JavaScript 调用的方法,如下所示
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
IEnumerable<Result> SearchObjects(string x, int y, double z);
当我从 JavaScript 发送 { "x": "something", "y": 1, "z": 1.5 } 时,这会起作用。
创建该 Web 服务几个月后,我找到了 WCF Web API 并尝试制作类似的东西。
不同之处在于我使用 HttpServiceHostFactory() 在 Global.asax 中创建了路由
现在,当我尝试调用该方法时,我收到如下异常
异常详细信息:System.InvalidOperationException: HttpOperationHandlerFactory 无法确定应与服务操作“Invoke_LoginRequest”的请求消息内容关联的输入参数。如果该操作不需要请求消息中的内容,请对该操作使用 HTTP GET 方法。否则,请确保一个输入参数的 IsContentParameter 属性设置为“True”,或者是可分配给以下其中一项的类型:HttpContent、ObjectContent1、HttpRequestMessage 或 HttpRequestMessage
1。
为了让它工作,我需要声明这样的方法(VB.Net),
Public Function Invoke_LoginRequest(ByVal request As HttpRequestMessage(Of JsonValue)) As HttpResponseMessage(Of String)
但随后我需要手动解析 JsonValue。那么旧版本的实际效果如何呢?有什么办法可以让我恢复这种行为吗?
此致 杰斯珀
I'm wondering about the behind the scenes magic that's happening when you create a WCF-Web service.
In one old project I got methods that I can call from JavaScript that look like this
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
IEnumerable<Result> SearchObjects(string x, int y, double z);
And this works when I send { "x": "something", "y": 1, "z": 1.5 } from JavaScript.
A couple of months after the creation of that webservice, I found the WCF Web API and tried to make something similar.
Difference was that I created the route in my Global.asax with the HttpServiceHostFactory()
Now when I try to call the method, I get an exception like this
Exception Details: System.InvalidOperationException:
The HttpOperationHandlerFactory is unable to determine the input parameter that should be associated with the request message content for service operation 'Invoke_LoginRequest'. If the operation does not expect content in the request message use the HTTP GET method with the operation. Otherwise, ensure that one input parameter either has it's IsContentParameter property set to 'True' or is a type that is assignable to one of the following: HttpContent, ObjectContent1, HttpRequestMessage or HttpRequestMessage
1.
And to get it to work, I need to declare the method like this (VB.Net)
Public Function Invoke_LoginRequest(ByVal request As HttpRequestMessage(Of JsonValue)) As HttpResponseMessage(Of String)
But then I need to parse the JsonValue manually. So how does the old version really work? And is there any way I could get that behaviour back?
Best regards
Jesper
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1) 定义一个包含要接收的数据的类,即
2) 将操作参数定义为
ObjectContent
HTH
佩德罗
1) Define a class containing the data that you want to receive, i.e,
2) Define the operation parameter as an
ObjectContent<Model>
HTH
Pedro