13

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?

Community
  • 1
  • 1
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
  • Hopefully my solution works well for you. Unfortunately I haven't found another way to add this reference type. If you wanted to proxy the service, you could create wrappers and copy/paste the code generated by Visual Studio as a standard web service. – Kraang Prime Jan 06 '17 at 00:11
  • Well, if nobody else comes with anything better, I'll accept your answer. Let's hope someone knows a better trick. – Konrad Viltersten Jan 06 '17 at 00:13
  • I hope so as well. Best I can think of is let Visual Studio do the grunt work, then copy/paste the results into the VSCode project. It automatically generates the code for all the methods and types. i am kind of curious about this as well -- would VSCode allow you to right-click update the reference (for changes to the WSDL) I am going to add some more info to the top -- perhaps this might help – Kraang Prime Jan 06 '17 at 00:17
  • *wipes sweat from brow* phew... I have added what is necessary to get started with either manually writing it out using the same method as Visual Studio, as well as if you want to cheat and just let Visual Studio (community) do it for you. - covered all bases I think. [ Manual, Manual using Visual Studio method, Using Visual Studio to built it and copy it out ] -- my preference is the 3rd option as it's the laziest, although option #1 could prove to be cleaner code. – Kraang Prime Jan 06 '17 at 00:45

5 Answers5

10

You can also use donet-svcutil

https://learn.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
Julio Ramos
  • 99
  • 1
  • 4
  • 1
    This will only generate async methods by default. If you also want synchronous methods add the --sync argument. i.e. dotnet-svcutil --sync https://webservice... – Michael Dec 08 '20 at 21:41
7

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

Add > Service Reference


Step 2

Click [Advanced] on this screen

Add Service Reference


Step 3

Click [Add Web Reference] on this screen

Service Reference Settings


Step 4

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

Add Web Reference


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.

Community
  • 1
  • 1
Kraang Prime
  • 9,981
  • 10
  • 58
  • 124
  • Hehe, that's awesome, mate but I suspect that you missed the disclaimer. I don't have Project Explorer because I'm working with Visual Studio Code (i.e. VS **Code** not VS 20xx). It's a new, lightweight IDE from MS. Check [this](https://code.visualstudio.com/) out. – Konrad Viltersten Jan 06 '17 at 00:11
  • @KonradViltersten - I did miss that tag as it's tagged Visual Studio as well. However I am pretty sure you can simply copy the output from that. I don't know if there is a quick way (in either case) to embed a reference as SOAP is quite old (while many still use) I do not believe a natural way was implemented in VSCode. – Kraang Prime Jan 06 '17 at 00:14
  • I fear that you're right. As for the "quite old" part - well, our banking system seems to believe that working with NET 2.0 is okay and NET 3.5 feel too hasty to upgrade to, so I need to work with this dino tech. – Konrad Viltersten Jan 06 '17 at 00:16
  • @KonradViltersten - I added some sample code from an old VB.NET project which hopefully can assist when building it if doing so from scratch (might be able to do much cleaner than Visual Studio's generated version which imho is rather bloated). – Kraang Prime Jan 06 '17 at 00:30
  • FYI I didn't downvote. In fact, I just +1 on you. I despise downvotes. – Konrad Viltersten Jan 06 '17 at 00:32
  • @KonradViltersten no problem. I also don't downvote unless it truly is horrible, even still, i will comment or vote to close. :) Trying to harvest data from this project to help you if you need to build the references from scratch. – Kraang Prime Jan 06 '17 at 00:34
7

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" />
    
Shanerk
  • 5,175
  • 2
  • 40
  • 36
  • 1
    Not everybody's so lucky as we were. We had the option for a one-line solution. *Skip WSDL and use a provider for auto-class generation.* +1 for OSX-based suggestion. – Konrad Viltersten Mar 27 '20 at 13:49
3

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, e.g.: 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.

ips1 38
  • 309
  • 2
  • 5
3

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://learn.microsoft.com/en-us/dotnet/core/additional-tools/dotnet-svcutil-guide?tabs=dotnetsvcutil2x