C# 可以在 Visual Studio 中使用 WSDL 自动生成代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13061172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Possible to autogenerate to code using WSDL in Visual Studio
提问by
Hi I wish to use test the following function: http://msrmaps.com/terraservice2.asmx?op=ConvertLonLatPtToNearestPlace
嗨,我想使用测试以下功能:http: //msrmaps.com/terraservice2.asmx?op=ConvertLonLatPtToNearestPlace
Is there some faster way I can test it out using Visual Studio 2010? I use C# normally. I just wondering is it possible to feed in the wsdl, and let visual studio auto generate some code to call the service? Thanks.
是否有一些更快的方法可以使用 Visual Studio 2010 对其进行测试?我通常使用 C#。我只是想知道是否可以输入 wsdl,并让 Visual Studio 自动生成一些代码来调用服务?谢谢。
And by the way, what does it mean "The test form is only available for requests from the local machine." in the url?
顺便说一句,“测试表单仅适用于来自本地机器的请求”是什么意思。在网址?
采纳答案by pvanhouten
There are a few things that you can do to generate that code. The first and easiest way (in my opinion) is to create a service reference to that URL. Here are some screenshots:
您可以执行一些操作来生成该代码。第一种也是最简单的方法(在我看来)是创建对该 URL 的服务引用。以下是一些截图:
Right click on your project, add a service reference:
右键单击您的项目,添加服务引用:


Put in the URL for the asmx (without the method in the querystring), give the reference a name and click OK:
输入 asmx 的 URL(不包含查询字符串中的方法),为引用命名并单击“确定”:


That will generate the proxy code you need for making the call:
这将生成您进行调用所需的代理代码:


From there, you can just use that proxy code to call the web service:
从那里,您可以使用该代理代码来调用 Web 服务:
TerraService.TerraServiceSoapClient client = new TerraService
.TerraServiceSoapClient("TerraServiceSoap");
string place = client.ConvertLonLatPtToNearestPlace(
new TerraService.LonLatPt { Lat = 47.6532, Lon = -122.135479 });
The second method is to use the command-line WSDL.exetool that comes with visual studio. Launch a visual studio command prompt and type wsdl /?. That will show you the parameters for the application. In my case, I just pulled down a copy of the WSDL from http://msrmaps.com/terraservice2.asmx?wsdl, saved it to my desktop and ran the command:
第二种方法是使用WSDL.exevisual studio自带的命令行工具。启动 Visual Studio 命令提示符并键入wsdl /?. 这将显示应用程序的参数。就我而言,我只是从http://msrmaps.com/terraservice2.asmx?wsdl 中提取了一份 WSDL 副本,将其保存到我的桌面并运行以下命令:
wsdl /o:./terraservice.cs terraservice.wsdl
Generates the proxy class next to my WSDL file.
在我的 WSDL 文件旁边生成代理类。
One last thing... become best friends with soapUI as @Habibillah suggested. It's a fantastic tool for calling web services without having to write any code.
最后一件事……就像@Habibilah 建议的那样,与soapUI 成为最好的朋友。这是一个无需编写任何代码即可调用 Web 服务的绝佳工具。
Hope that helps!
希望有帮助!
回答by Habibillah
Visual studio can generate code for wsdl/webservice referenced by an URL even it outside on your local machine. However, the test form accessed by browser just can be access on local machine (localhost).
Visual Studio 可以为由 URL 引用的 wsdl/webservice 生成代码,即使它在本地机器之外。但是,浏览器访问的测试表单只能在本地机器(localhost)上访问。
But you still can test the webservice over internet by other tool like soapUI. This tool useful for me to test webservice over internet.
但是您仍然可以通过诸如soapUI 之类的其他工具通过Internet 测试Web 服务。这个工具对我通过互联网测试网络服务很有用。

