4

I have a lot of SOAP/XML and REST/JSON experience in Java and C++, but am pretty much a newbie in .NET. I have to create a SOAP client from a WSDL in C# in VS 2012. The app is not a web-based app, but a console app that will be run as a cron job every 24 hours. It has to query a Web Service for a token, do a client database lookup, and then use the token to update a list of client id's on the Web Service with any new ones - two calls only.

The company has a tester where I can type in either SOAP message (envelope and contents) by hand, click the run button, and a window shows the correct response in its SOAP envelope. My only confusion would seem to be endpoint-related. A WSDL-generated client should take care of everything.

I don't know much about C# (5), the .NET framework (4.5.x), or the newer .NET versions of VS (I've been using Eclipse, IntelliJ IDEA, and even jEdit for the past decade and more).

I've seen a dozen different "solutions" to this problem, ranging from WSDL.EXE and SiteUtil.Exe through adding the WSDL file as a (web?) reference or using one of the NuGet addons. The problem is that every solution I've found appears to asssume the client app is built on one of the web templates. I have to do this as a background .exe in plain C# without any web-based support or interaction in my app.

Any suggestions on the best (hopefully simplest) way to generate client source code?

1 Answers1

11

You are not alone in being confused. You have to realize classic SOAP (asmx/wsdl) is considered an "outdated" technology (by Microsoft), so these days it hidden away in the toolset in favor of newer technologies. The most basic approach is to add it as a Reference from the solution explorer within Visual Studio itself. Here is a step-by-step:

  1. Create a console application
  2. In the solution explorer, right-click the References node and choose Add Service ReferenceAdd Service Reference from within Visual Studio
  3. Ignore most of the dialog that comes up, and go straight for Advanced: enter image description here
  4. From the bottom of the Service Reference Setttings, choose Add Web Reference... Add Web Reference
  5. Now fill in the location of your ASMX, hit the little arrow, wait a bit for the tooling to discover the service, then hit Add Reference Add Web Reference - web service details
  6. This will add a Web Reference to your project that you can then use to access the methods of the webservice.Web reference within project

[Edit] (to answer your question)

If you have a .WSDL file on disk, you simply enter the file location on disk in the URL box of the Add Web Reference dialog:WSDL on Disk

In that case, the generated service has a default namespace of WebReference (unless you change it, of course), and you'll most likely want to explicitly set the URL:

var service = new WebReference.GlobalWeather {Url = "http://www.webservicex.net/globalweather.asmx"};
var weather = service.GetWeather("Amsterdam", "Netherlands");
Console.WriteLine(weather);
Paul-Jan
  • 16,746
  • 1
  • 63
  • 95
  • Looks good with a couple of questions still hanging. First, all I have is a .WSDL file supplied by the Web Service provider. I believ – MiddleAgedMutantNinjaProgrammer Jun 27 '16 at 17:51
  • Looks good, but still a bit confused. First, my understanding of an .asmx file is that it is used to generate a Web Service and a client proxy through ASP.NET. In the process, creates its own internal WSDL file. All I have is a .WSDL file supplied by the Web Service provider that I have 2 use. Does your solution require me to convert the WSDL to an ASMX and then use the ASMX in place of the WSDL file? I need to generate the client proxy directly from the WSDL file, which has all of the necessary binding info. Also, does dragging ASP.NET into a pure console app have any ramifications? – MiddleAgedMutantNinjaProgrammer Jun 27 '16 at 17:58
  • Added the first question (about using a local .WSDL file) to the question. Your understanding about asmx files serving autogenerated WSDL files is correct. However, the ASMX file is not an essential part of using the Web Reference tooling, see the updated answer: the tooling supports both direct reference of a WSDL and getting the WSDL from an ASMX service. It also doesn't require the use of ASP.NET, and works fine in a console application. Hope that helps clear things up a bit. :-) – Paul-Jan Jun 27 '16 at 18:08