C# 如何使用 WSDL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1302525/
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
How to use a WSDL
提问by jmayor
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?)
我需要使用 Web 服务。他们向我发送了 WSDL 文件。我应该怎么做才能将它添加到我的网站并开始使用它作为代理。(如果我将它放在虚拟目录中,它可以被发现,但它是否授予我与真实 Web 服务的连接?)
采纳答案by marc_s
I would fire up Visual Studio, create a web project (or console app - doesn't matter).
我会启动 Visual Studio,创建一个 Web 项目(或控制台应用程序 - 无所谓)。
For .Net Standard:
对于 .Net 标准:
- I would right-click on the project and pick "Add Service Reference" from the Addcontext menu.
- I would click on Advanced, then click on Add Service Reference.
- I would get the complete file path of the wsdl and paste into the address bar. Then fire the Arrow (go button).
- 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
- 我会右键单击该项目并从“添加”上下文菜单中选择“添加服务引用” 。
- 我会单击高级,然后单击添加服务引用。
- 我会得到 wsdl 的完整文件路径并粘贴到地址栏中。然后发射箭头(去按钮)。
- 如果在尝试加载文件时出现错误,则必须有一个损坏且未解析的 url 文件需要解析,如下所示:
有关如何修复的信息,请参阅此答案: Stackoverflow 回答:无法为其创建服务引用wsdl 文件
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.
如果没有错误,您只需设置要用于访问服务的 NameSpace,它就会为您生成。
For .Net Core
对于 .Net 核心
- I would right click on the project and pick Connected Service from the Add context menu.
- I would select Microsoft WCF Web Service Reference Provider from the list.
- I would press browse and select the wsdl file straight away, Set the namespace and I am good to go. Refer to the error fix url above if you encounter any error.
- 我会右键单击该项目并从“添加”上下文菜单中选择“连接的服务”。
- 我会从列表中选择 Microsoft WCF Web 服务引用提供程序。
- 我会按浏览并立即选择 wsdl 文件,设置命名空间,我很高兴。如果遇到任何错误,请参阅上面的错误修复 URL。
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.
上述任何一种方法都会生成一个简单的、非常基本的 WCF 客户端供您使用。您应该在生成的代码中找到“YourservicenameClient”类。
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.
作为参考,生成的 cs 文件可以在你的 Obj/debug(or release)/XsdGeneratedCode 中找到,你仍然可以在 TempPE 文件夹中找到 dll。
The created Service(s) should have methods for each of the defined methods on the WSDL contract.
所创建的服务应该具有针对 WSDL 合同上每个已定义方法的方法。
Instantiate the client and call the methods you want to call - that's all there is!
实例化客户端并调用您想要调用的方法 - 这就是全部!
YourServiceClient client = new YourServiceClient();
client.SayHello("World!");
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:
如果您需要指定远程 URL(不使用默认创建的 URL),您可以在代理客户端的构造函数中轻松执行此操作:
YourServiceClient client = new YourServiceClient("configName", "remoteURL");
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).
其中configName
是要使用的端点的名称(您将使用除 URL 之外的所有设置),remoteURL
是表示要连接到的 URL 的字符串(而不是包含在配置中的那个)。
回答by vidalsasoon
In visual studio.
在视觉工作室。
- Create or open a project.
- Right-click project from solution explorer.
- Select "Add service refernce"
- Paste the address with WSDL you received.
- Click OK.
- 创建或打开一个项目。
- 在解决方案资源管理器中右键单击项目。
- 选择“添加服务引用”
- 用您收到的 WSDL 粘贴地址。
- 单击确定。
If no errors, you should be able to see the service reference in the object browser and all related methods.
如果没有错误,您应该能够在对象浏览器中看到服务引用和所有相关方法。
回答by azheglov
Use WSDL.EXE utilityto generate a Web Service proxy from WSDL.
使用WSDL.EXE 实用程序从 WSDL 生成 Web 服务代理。
You'll get a long C# source file that contains a class that looks like this:
您将获得一个很长的 C# 源文件,其中包含一个如下所示的类:
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.42")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Web.Services.WebServiceBindingAttribute(Name="MyService", Namespace="http://myservice.com/myservice")]
public partial class MyService : System.Web.Services.Protocols.SoapHttpClientProtocol {
...
}
In your client-side, Web-service-consuming code:
在客户端使用 Web 服务的代码中:
- instantiate MyService.
- set its Url property
- invoke Web methods
- 实例化 MyService。
- 设置它的 Url 属性
- 调用 Web 方法
回答by Nedzad G
If you want to add wsdl reference in .Net Coreproject, there is no "Add web reference" option.
如果要在.Net Core项目中添加 wsdl 引用,则没有“添加 Web 引用”选项。
To add the wsdl reference go to Solution Explorer, right-click on the References project item and then click on the Add Connected Service option.
要添加 wsdl 引用,请转到“解决方案资源管理器”,右键单击“引用”项目项,然后单击“添加连接的服务”选项。
Then click 'Microsoft WCF Web Service Reference':
然后单击“Microsoft WCF Web 服务参考”:
Enter the file path into URI text box and import the WSDL:
在 URI 文本框中输入文件路径并导入 WSDL:
It will generate a simple, very basic WCF client and you to use it something like this:
它将生成一个简单的、非常基本的 WCF 客户端,您可以像这样使用它:
YourServiceClient client = new YourServiceClient();
client.DoSomething();