简体   繁体   中英

How to reference a WSDL file using Visual Studio Code?

Important disclaimer. This question isn't about generating a proxy to WSDL . It's not about creating a reference in VS Code , neither.

I'm using Visual Studio Code (latest update, v1.8 November 16) and I need to create a call to an external web service described using a WSDL and XSD file. I want to do that using the aforementioned editor and preferably not have to compose all the proxies and enveloping myself.

Is it possible or am I out of luck on this one?

What would be the simplest alternative if it's not doable in VS Code? Are we talking about generating the classes and calls using VS15 and copying over the files or is there a neat workaround I'm not familiar with?

Manual Creation (from scratch)

If building from scratch and don't care about how Visual Studio does it, you can start with some basics from this solution here , as well as the other links referenced in the accepted solution on the same page.

Manual Creation using the same method Visual Studio uses

For reference, some of the files generated by the Visual Studio add reference method below, are stored within a subfolder Web References / Example (where Example is the name of the variable used to access the reference) and contains the following :

.map file

<?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 file (same name as the 'filename' parameter from above)

This file is the complete raw wsdl source file (well formatted xml).

reference file

This file contains code to initialize all the methods and properties and is the base class which extends System.Web.Services.Protocols.SoapHttpClientProtocol

The properties assigned to the class (sorry I am stripping from an old VB.NET project: look like the following :

<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 file for each type)

Example code

<?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>

Use Visual Studio to build it for you, then open up again in VSCode

Inside Visual Studio, you can do the following (and copy the results out to your VSCode project)

Step 1

Right-click your project in Project explorer, and select Add > Service Reference..

添加 > 服务参考


Step 2

Click [Advanced] on this screen

添加服务参考


Step 3

Click [Add Web Reference] on this screen

服务参考设置


Step 4

Enter your full URL to the WSDL location and press Enter.

添加网页参考


Finally

If successful (well formatted WSDL is found), the [Add Reference] button will be enabled. Click that, and it will add the reference to your project.

You can also use donet-svcutil

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

Example

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

This may help others as a direct approach. You can get the necessary proxy client generated using the svutil.exe tool included in SDKs. If you create a folder for the files, navigate to that folder via command line and running the tool will generate the proxy file plus the config elements you'll need on your own config.

Note: you can set many options including proxy output language, defaults to c#.

If you are targeting a svc WS you're trying to use, eg: http://abc.dfe.com/myWebService.svc , would show something like:

To test this service, you will need to create a client and use it to call the service. You can do this using the svcutil.exe tool from the command line with the following syntax:

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

This will generate a configuration file and a code file that contains the client class. Add the two files to your client application and use the generated client class to call the Service.

Hope this helps someone with the same query, if not the OP.

Following on Julio's comment, here are all the steps needed with .NET Core (instructions for OSX):

  1. Install dotnet-svcutil:

     dotnet tool install --global dotnet-svcutil
  2. Add the tools path to your .bash_profile:

     nano ~/.bash_profile

    add this line:

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

    reload your profile:

     . ~/.bash_profile
  3. Navigate to your app or library path and run the command. You need to be in the path where you want your service reference to live. For example:

     cd MY-PROJECT-FOLDER/Library dotnet-svcutil PATH-TO-MY-WSDL/my-wsdl.xml
  4. Add the created file to your .csproj which by default will be creatively 🤓named ServiceReference/Reference.cs . The line will look like this in your file:

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

If you follow the steps for using dotnet-svcutil with Visual Studio Code/.Net Core, you may end up with compilation errors on the generated Reference.cs file due to packages that are part of .Net Framework, but not Core. Luckily Microsoft has made them available on NuGet. Specifically, I had to add System.ServiceModel.Primitives and System.ServiceModel.Http.

Enter the following at a command line in the directory of your project:

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

See this answer for more information: System.ServiceModel not found in .NET Core project

And this Microsoft article provides more information on dotnet-svcutil in general: https://docs.microsoft.com/en-us/dotnet/core/additional-tools/dotnet-svcutil-guide?tabs=dotnetsvcutil2x

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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