繁体   English   中英

如何使用 Visual Studio Code 引用 WSDL 文件?

[英]How to reference a WSDL file using Visual Studio Code?

重要免责声明。 这个问题与生成 WSDL 的代理无关。 这也不是关于在 VS Code 中创建引用

我正在使用 Visual Studio Code(最新更新,11 月 16 日 v1.8),我需要创建对使用 WSDL 和 XSD 文件描述的外部 Web 服务的调用。 我想使用上述编辑器来做到这一点,最好不必编写所有代理并包围自己。

有可能吗,还是我不走运?

如果在 VS Code 中不可行,那么最简单的替代方法是什么? 我们是在谈论使用 VS15 生成类和调用并复制文件,还是有我不熟悉的巧妙解决方法?

手动创建(从头开始)

如果从头开始构建并且不关心 Visual Studio 是如何做到的,您可以从这里解决方案中的一些基础知识开始,以及在同一页面上接受的解决方案中引用的其他链接。

使用 Visual Studio 使用的相同方法手动创建

作为参考,下面由 Visual Studio 添加引用方法生成的一些文件存储在子文件夹Web References / Example (其中Example是用于访问引用的变量的名称)中,并包含以下内容:

.map 文件

<?xml version="1.0" encoding="utf-8"?>
<DiscoveryClientResultsFile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Results>
    <DiscoveryClientResult referenceType="System.Web.Services.Discovery.ContractReference" url="http://example.com/api/index.php?wsdl" filename="index.wsdl" />
  </Results>
</DiscoveryClientResultsFile>

.wsdl 文件(与上面的 'filename' 参数同名)

该文件是完整的原始 wsdl 源文件(格式良好的 xml)。

参考文件

该文件包含初始化所有方法和属性的代码,是扩展System.Web.Services.Protocols.SoapHttpClientProtocol的基类

分配给该类的属性(对不起,我正在从一个旧的 VB.NET 项目中剥离:如下所示:

<System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "4.6.1586.0"),  _
 System.Diagnostics.DebuggerStepThroughAttribute(),  _
 System.ComponentModel.DesignerCategoryAttribute("code"),  _
 System.Web.Services.WebServiceBindingAttribute(Name:="ExampleAPIBinding", [Namespace]:="urn:ExampleAPI"),  _
 System.Xml.Serialization.SoapIncludeAttribute(GetType(MyCustomType1)),  _
 System.Xml.Serialization.SoapIncludeAttribute(GetType(MyCustomType2)),  _

 Partial Public Class ExampleAPI
    Inherits System.Web.Services.Protocols.SoapHttpClientProtocol

 End Class

.datasource(每种类型 1 个文件)

示例代码

<?xml version="1.0" encoding="utf-8"?>
<!--
    This file is automatically generated by Visual Studio .Net. It is
    used to store generic object data source configuration information.
    Renaming the file extension or editing the content of this file may
    cause the file to be unrecognizable by the program.
-->
<GenericObjectDataSource DisplayName="MyMethodName" Version="1.0" xmlns="urn:schemas-microsoft-com:xml-msdatasource">
   <TypeInfo>ExampleAPI.SOAP.ClientMerchant, Web References.SOAP.Reference.vb.dll, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null</TypeInfo>
</GenericObjectDataSource>

使用 Visual Studio 为你构建,然后在 VSCode 中再次打开

在 Visual Studio 中,您可以执行以下操作(并将结果复制到您的 VSCode 项目中)

第1步

在项目资源管理器中右键单击您的项目,然后选择添加 > 服务引用..

添加 > 服务参考


第2步

在这个界面点击【高级】

添加服务参考


第 3 步

单击此屏幕上的[添加 Web 引用]

服务参考设置


第四步

输入 WSDL 位置的完整 URL,然后按 Enter。

添加网页参考


最后

如果成功(找到格式良好的 WSDL),将启用[添加引用]按钮。 单击它,它将添加对您的项目的引用。

你也可以使用 donet-svcutil

https://docs.microsoft.com/en-us/dotnet/core/additional-tools/dotnet-svcutil-guide?tabs=dotnetsvcutil2x

例子

dotnet-svcutil https://svn.apache.org/repos/asf/airavata/sandbox/xbaya-web/test/Calculator.wsdl

这可能会帮助其他人作为一种直接的方法。 您可以使用 SDK 中包含的 svutil.exe 工具生成必要的代理客户端。 如果您为文件创建一个文件夹,请通过命令行导航到该文件夹​​并运行该工具将生成代理文件以及您在自己的配置中需要的配置元素。

注意:您可以设置许多选项,包括代理输出语言,默认为 c#。

如果您的目标是您尝试使用的 svc WS,例如: http://abc.dfe.com/myWebService.svc ,将显示如下内容:

要测试此服务,您需要创建一个客户端并使用它来调用该服务。 您可以使用命令行中的 svcutil.exe 工具使用以下语法执行此操作:

svcutil.exe http://abc.dfe.com/myWebService.svc?wsdl

这将生成一个配置文件和一个包含客户端类的代码文件。 将这两个文件添加到您的客户端应用程序并使用生成的客户端类来调用服务。

希望这可以帮助有相同查询的人,如果不是 OP。

根据 Julio 的评论,以下是 .NET Core 所需的所有步骤(OSX 说明):

  1. 安装 dotnet-svcutil:

     dotnet tool install --global dotnet-svcutil
  2. 将工具路径添加到您的 .bash_profile:

     nano ~/.bash_profile

    添加这一行:

     export PATH=$PATH:$HOME/.dotnet/tools

    重新加载您的个人资料:

     . ~/.bash_profile
  3. 导航到您的应用程序或库路径并运行该命令。 您需要在您希望服务引用存在的路径上。 例如:

     cd MY-PROJECT-FOLDER/Library dotnet-svcutil PATH-TO-MY-WSDL/my-wsdl.xml
  4. 将创建的文件添加到您的.csproj中,默认情况下,它会被创造性地命名为ServiceReference/Reference.cs 🤓。 该行在您的文件中将如下所示:

     <Content Include="ServiceReference\\Reference.cs" />

如果您按照将dotnet-svcutil与 Visual Studio Code/.Net Core 结合使用的步骤进行操作,您最终可能会在生成的 Reference.cs 文件中出现编译错误,这是由于包是 .Net Framework 的一部分,而不是 Core 的一部分。 幸运的是,微软已经在 NuGet 上提供了它们。 具体来说,我必须添加 System.ServiceModel.Primitives 和 System.ServiceModel.Http。

在项目目录中的命令行中输入以下内容:

dotnet add package System.ServiceModel.Primitives
dotnet add package System.ServiceModel.Http

有关更多信息,请参阅此答案: System.ServiceModel not found in .NET Core project

这篇 Microsoft 文章提供了有关dotnet-svcutil更多信息: https : dotnet-svcutil = dotnet-svcutil

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM