从 WSDL C# 生成 Web 服务代理
我正在使用 Fedex 网络服务。他们没有提供像 http://hostServer/WebserviceRoot/WebServiceName.asmx 这样的 Web 服务 URL。相反,他们提供了一个 .wsdl
文件。
请指导我从 .wsdl
文件生成 Web 服务代理类并向我的项目添加 Web 服务引用。
我知道从 wsdl
生成代理,
wsdl /l:cs /protocol:SOAP /o:MyProxy.cs mywsdl.wsdl
但我想知道它的作用。它可能会创建代理类,但如何将 Web 服务引用添加到我的项目中?
I am working with Fedex webservice. They did not provide a webservice URL like http://hostServer/WebserviceRoot/WebServiceName.asmx. Rather they provided a .wsdl
file.
Please guide me in generating the web service proxy classes from the .wsdl
file and adding a web service reference to my project.
I know to generate the proxy from wsdl
like
wsdl /l:cs /protocol:SOAP /o:MyProxy.cs mywsdl.wsdl
but I want to know what it does. It may create the proxy class but how can I add the web service reference to my project?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您手动创建代理类,则只需将它们添加到您的项目中。 Visual Studio 中的
添加 Web 引用
机制用于自动执行此过程并添加类型。wsdl
可执行文件将读取您已下载的 WSDL 文件(“mywsdl .wsdl”),并在您的示例中以 C# 语言生成代理代码(在“MyProxy.cs”中)。您可以将“MyProxy.cs”添加到您的项目中,并开始在项目中实例化生成的客户端的实例。If you are creating the proxy classes manually, you need only add them to your project. The
Add Web Reference
mechanism in Visual Studio is used to automate this process and add the types in.The
wsdl
executable will read the WSDL file that you've downloaded ("mywsdl.wsdl"), and generate the proxy code, in your example in the C# language (in "MyProxy.cs"). You can add "MyProxy.cs" to your project and start instantiating instances of the generated client in your project.从 Visual Studio 命令提示符中使用 wsdl.exe 命令。
前任:
use the wsdl.exe command from the visual studio command prompt.
ex:
通常有两种方法来处理 Web 服务。一如您提到的,使用 Web 服务 URL ../service.asmx。这样,您只需在 Visual Studio 项目中使用“添加 Web 引用”,即可始终连接到实时 Web 服务。这要容易得多,缺点是如果网络服务决定更改某些内容,您也需要更改您的引用。重新编译项目并重新部署。
当您保留 wsdl 文件的本地副本时,如果它们在实时 WSDL 中引入强制字段,只要底层代码库仍然可以使用它,您仍然受到保护。如果您自己生成 proxy.cs 类,如接受的答案中所示,它的工作原理就像通过 Visual Studio 添加它一样,只不过您必须手动将该类添加到项目中。
另外,我相信您可以从 WSDL 文件中的
获取整个 Web 服务 url。通常,如果您打算跟上任何更改,那么使用本地 WSDL 文件并不是一个好习惯。我怀疑您会遇到这个问题,因为它是联邦快递网络服务,可能已经经受住了时间的考验。There are usually two ways to deal with web services. One, like you mentioned, use a web service URL ../service.asmx. This way you just need to use "Add web reference" in the visual studio project and you are always connected to the live web service. This is much easier, the downside is if the web service decides to change something, you need to change your reference as well. Recompile the project and redeploy.
When you keep a local copy of the wsdl file, if they introduce a mandatory field in the live WSDL, you are still protected as long as the underlying code base still works with it. If you are generating the proxy.cs class on your own, as in the accepted answer, it just works like adding it through visual studio except that you have to manually add the class to the project.
Also, i believe you can get the whole web service url from
<soap:address location="http://testsite.com/test.asmx" />
in the WSDL file. Usually it is not a good practice to use a local WSDL file if your intention is to keep up with any changes. I doubt you will have that problem since it is a fedex web service, probably has stood through the test of time.