将多个参数传递到 WCF Web API 服务

发布于 2024-12-12 07:01:16 字数 1101 浏览 0 评论 0原文

我想知道创建 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 或 HttpRequestMessage1。

为了让它工作,我需要声明这样的方法(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 HttpRequestMessage1.

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 技术交流群。

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

发布评论

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

评论(1

尘曦 2024-12-19 07:01:16

1) 定义一个包含要接收的数据的类,即

public class Model
{
    public string x { get; set; }
    public int y { get; set; }
    public double z { get; set; }
}

2) 将操作参数定义为 ObjectContent

public HttpResponseMessage Post(ObjectContent<Model> c){
    Model m = c.ReadAs();
    ...
}

HTH
佩德罗

1) Define a class containing the data that you want to receive, i.e,

public class Model
{
    public string x { get; set; }
    public int y { get; set; }
    public double z { get; set; }
}

2) Define the operation parameter as an ObjectContent<Model>

public HttpResponseMessage Post(ObjectContent<Model> c){
    Model m = c.ReadAs();
    ...
}

HTH
Pedro

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