如何使用 WSDL [英] How to use a WSDL

查看:40
本文介绍了如何使用 WSDL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 Web 服务.他们向我发送了 WSDL 文件.我应该怎么做才能将它添加到我的网站并开始使用它作为代理.(如果我将它放在虚拟目录中,它可以被发现,但它是否授予我与真实 Web 服务的连接?)

I need to consume a Web Service. They sent me the WSDL file. What should I do to add it to my website and start using it as the proxy. ( If I put it on a Virtual Directory it can be discovered, but does it grant me the connection with the real web service?)

推荐答案

我会启动 Visual Studio,创建一个 Web 项目(或控制台应用程序 - 无关紧要).

I would fire up Visual Studio, create a web project (or console app - doesn't matter).

对于 .Net 标准:

For .Net Standard:

  1. 我会右键单击该项目并从添加上下文菜单中选择添加服务引用".
  2. 我会点击高级,然后点击添加服务引用.
  3. 我会获取 wsdl 的完整文件路径并粘贴到地址栏中.然后发射箭头(去按钮).
  4. 如果在尝试加载文件时出现错误,则必须有一个损坏且未解析的 url 文件需要解析,如下所示:有关如何修复的信息,请参阅此答案:Stackoverflow 回答:无法为 wsdl 文件创建服务引用
  1. I would right-click on the project and pick "Add Service Reference" from the Add context menu.
  2. I would click on Advanced, then click on Add Service Reference.
  3. I would get the complete file path of the wsdl and paste into the address bar. Then fire the Arrow (go button).
  4. If there is an error trying to load the file, then there must be a broken and unresolved url the file needs to resolve as shown below: Refer to this answer for information on how to fix: Stackoverflow answer to: Unable to create service reference for wsdl file

如果没有错误,您只需设置要用于访问服务的 NameSpace,它就会为您生成.

If there is no error, you should simply set the NameSpace you want to use to access the service and it'll be generated for you.

对于 .Net 核心

  1. 我会右键单击该项目,然后从添加"上下文菜单中选择连接的服务".
  2. 我会从列表中选择 Microsoft WCF Web 服务引用提供程序.
  3. 我会直接按浏览并选择 wsdl 文件,设置命名空间,然后我就可以开始了.如果您遇到任何错误,请参阅上面的错误修复网址.

上述任何一种方法都会生成一个简单的、非常基本的 WCF 客户端供您使用.您应该在生成的代码中找到YourservicenameClient"类.

Any of the methods above will generate a simple, very basic WCF client for you to use. You should find a "YourservicenameClient" class in the generated code.

作为参考,生成的 cs 文件可以在你的 Obj/debug(or release)/XsdGeneratedCode 中找到,你仍然可以在 TempPE 文件夹中找到 dll.

For reference purpose, the generated cs file can be found in your Obj/debug(or release)/XsdGeneratedCode and you can still find the dlls in the TempPE folder.

所创建的服务应该具有针对 WSDL 合同中定义的每个方法的方法.

The created Service(s) should have methods for each of the defined methods on the WSDL contract.

实例化客户端并调用您想要调用的方法 - 这就是全部!

Instantiate the client and call the methods you want to call - that's all there is!

YourServiceClient client = new YourServiceClient();
client.SayHello("World!");

如果需要指定远程 URL(不使用默认创建的),可以在代理客户端的构造函数中轻松完成:

If you need to specify the remote URL (not using the one created by default), you can easily do this in the constructor of the proxy client:

YourServiceClient client = new YourServiceClient("configName", "remoteURL");

其中 configName 是要使用的端点的名称(您将使用除 URL 之外的所有设置),remoteURL 是表示要连接的 URL 的字符串到(而不是配置中包含的那个).

where configName is the name of the endpoint to use (you will use all the settings except the URL), and the remoteURL is a string representing the URL to connect to (instead of the one contained in the config).

这篇关于如何使用 WSDL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆