假设我有一个 asmx Web 服务,地址如下:
http://localhost/BudgetWeb/Service.asmx
此 Web 服务具有一个具有以下签名的 Web 方法:
string GetValue(string key)
这个 GetValue 方法返回一个像这样的字符串:
<?xml version=\"1.0\" encoding=\"utf-8\" ?><value>250.00</value>
如果我想这样做怎么办:
XDocument doc = XDocument.Load("http://localhost/BudgetWeb/Service.asmx?op=GetValue&key=key1")
这不起作用,而且我很确定 XDocument.Load 实际上并没有调用服务器上的 Web 方法。我认为它期望 uri 指向它可以加载的文件。要调用 Web 方法,我想我必须有一个 Web 代理类,并且必须使用它来调用 string GetValue(string key)
,然后我可以使用从要传递给 XDocument.Load 方法的 Web 代理类。
我的理解是否正确,或者 XDocument.Load 有没有办法实际调用服务器上的 Web 方法?
Suppose I have an asmx web service at the following address:
http://localhost/BudgetWeb/Service.asmx
This web service has a web method with the following signature:
string GetValue(string key)
This GetValue method returns a string like this:
<?xml version=\"1.0\" encoding=\"utf-8\" ?><value>250.00</value>
What if I wanted to do this:
XDocument doc = XDocument.Load("http://localhost/BudgetWeb/Service.asmx?op=GetValue&key=key1")
This doesn't work, and I'm pretty sure that XDocument.Load doesn't actually invoke a web method on the server. I think it expects the uri to point to a file that it can load. To call a web method, I think I'd have to have a web proxy class and would have to use that to call string GetValue(string key)
, and then I could use that value returned from the web proxy class to pass to the XDocument.Load method.
Is my understanding correct, or is there a way for XDocument.Load to actually invoke a web method on the server?
发布评论
评论(2)
尝试使用这个:
编辑:刚刚发现:你使用的是无效的URI:
我应该
使用这个代码片段:
Try to use this:
EDIT: Just figured out: you're using a invalid URI:
Should be
I'm using this code snippet:
好的,我发现了问题。在 Web 服务的 web.config 中,您必须添加以下内容:
感谢大家的建议,我真的很感激,特别是 Rubens Farias,他的工作示例让我走上了正轨。
Ok, I found the issue. In the web.config for the web service, you have to add this:
Thanks to everyone for their suggestions, I really appreciate it, especially Rubens Farias whose working example put me on the right track.