Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create SOAP/XML client from WSDL in console cron job app C# vs2012

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?

like image 228
MiddleAgedMutantNinjaProgrammer Avatar asked Dec 25 '22 04:12

MiddleAgedMutantNinjaProgrammer


1 Answers

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);
like image 140
Paul-Jan Avatar answered Jan 13 '23 15:01

Paul-Jan