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

Kon*_*ten 7 c# web-services visual-studio visual-studio-code asp.net-core

重要免责声明.这个问题不是关于为WSDL生成代理.它不是在VS Code中创建引用.

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

这可能还是我在这个上运气不好?

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

Sha*_*ane 8

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

  1. 安装 dotnet-svcutil:

    dotnet tool install --global dotnet-svcutil
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将工具路径添加到您的 .bash_profile 中:

    nano ~/.bash_profile
    
    Run Code Online (Sandbox Code Playgroud)

    添加这一行:

    export PATH=$PATH:$HOME/.dotnet/tools
    
    Run Code Online (Sandbox Code Playgroud)

    重新加载您的个人资料:

    . ~/.bash_profile
    
    Run Code Online (Sandbox Code Playgroud)
  3. 导航到您的应用程序或库路径并运行命令。您需要位于您希望服务引用所在的路径中。例如:

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

    <Content Include="ServiceReference\Reference.cs" />
    
    Run Code Online (Sandbox Code Playgroud)


Kra*_*ime 6

手动创作(从头开始)

如果从头开始构建并且不关心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>
Run Code Online (Sandbox Code Playgroud)

.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
Run Code Online (Sandbox Code Playgroud)

.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>
Run Code Online (Sandbox Code Playgroud)

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

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

步骤1

在Project explorer中右键单击您的项目,然后选择Add> Service Reference ..

添加>服务参考


第2步

单击此屏幕上的[高级]

添加服务参考


第3步

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

服务参考设置


第4步

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

添加Web引用


最后

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


小智 6

你也可以使用 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
Run Code Online (Sandbox Code Playgroud)