107

My application needs to talk to a web service that hasn't got an online WSDL definition. The developers however supplied me with a WSDL file.

With a public WSDL Visual Studio can generate this code for me using the Service Reference wizard. But it doesn't seem to work without a public WSDL.

How do I generate the code for talking to this web service using this WSDL file?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Anne
  • 1,073
  • 2
  • 8
  • 4

7 Answers7

147

Using WSDL.exe didn't work for me (gave me an error about a missing type), but I was able to right-click on my project in VS and select "Add Service Reference." I entered the path to the wsdl file in the Address field and hit "Go." That seemed to be able to find all the proper types and added the classes directly to my project.

Jeff
  • 3,307
  • 2
  • 25
  • 36
  • 3
    Faced same scenario and your suggestion saved me lots of frustration and probably some hair. Well deserved rep has been given to you. (As indirect result, you got +100 on all sites :)) – Shadow The GPT Wizard Mar 13 '13 at 09:44
  • I tried this, the file imports, but I cant see in my code the classes – guiomie Mar 16 '15 at 19:36
  • 1
    I tried this method using MSVC 2015 but it refused to process wsdl file using URL. So I used the method described by Andrew M. – Hermann Dec 26 '15 at 08:23
  • 1
    I tried this from a development pc that doesn't have permission to access web service. Despite the .wsdl file is in local, it tried to connect service when I hit "Go". So I was not able to add reference. In this situation I think the solution of Andrew M. should be preferred. It worked good. – gkonuralp Oct 06 '16 at 08:28
  • @jeffaudio im facing a similar problem I'm able to add service reference for web project but using wsdl.exe im not able to generate proxy file. any suggestion ..??? – prakash r Jun 27 '19 at 08:58
  • This did not work for me unfortunately. I was able to generate files with wsdl.exe mentioned above. – Daniel Krzyczkowski Aug 29 '19 at 11:17
  • The proxy client code weren't generated by using "Add Service Reference" in my case. Have to additionally **uncheck "Reuse types in all referenced assemblies"** within Advanced configuration to get it to work. [Another Post](https://stackoverflow.com/a/28774580/8140473) – Rohim Chou Mar 29 '21 at 08:06
123

Try using WSDL.exe and then including the generated file (.cs) into your project.

Fire up the Visual Studio Command prompt (under visual studio/tools in the start menu) then type

>wsdl.exe [path To Your WSDL File]

That'll spit out a file, which you copy/move and include in your project. That file contains a class which is a proxy to your sevice, Fire up an instance of that class, and it'll have a URL property you can set on the fly, and a bunch of methods that you can call. It'll also generate classes for all/any complex objects passed across the service interface.

Madis Otenurm
  • 61
  • 1
  • 7
Andrew M
  • 9,149
  • 6
  • 44
  • 63
  • 3
    Thanks! And you can use /o parameter for path of exported file. e.g. >wsdl.exe http://www.example.com/service.wsdl /o:C:\Users\X\Desktop – gkonuralp Oct 04 '16 at 13:09
  • 2
    and optionally add a namespace manually, covering all classes, to prevent conflict with similar files generated. – Blue Clouds Mar 29 '18 at 12:08
16

On the side note: if you have all of the files locally (not only wsdl file but also xsd files) you can invoke wsdl.exe in that manner:

wsdl.exe [path to your wsdl file] [paths to xsd files imported by wsdl]

That way wsdl.exe can resolve all dependecies locally and correctly generates proxy class.

Maybe it will save somebody some time - it solves "missing type" error when service is not avaliable online.

Jarek Mazur
  • 2,052
  • 1
  • 25
  • 41
4

There's a Microsoft Doc for creating your WCF proxy from the command line .

You can find your local copy of wsdl.exe in a location similar to this: C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools (Learn more here)

In the end your Command should look similar to this:

"C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\wsdl.exe"
 /language:CS /n:"My.Namespace" https://www.example.com/service/wsdl
Serj Sagan
  • 28,927
  • 17
  • 154
  • 183
3

Try the WSDL To Proxy class tool shipped with the .NET Framework SDK. I've never used it before, but it certainly looks like what you need.

Steve Danner
  • 21,818
  • 7
  • 41
  • 51
1

save the file on your disk and then use the following as URL:

file://your_path/your_file.wsdl
-1

Since the true Binding URL for the web service is located in the file, you could do these simple steps from your local machine:

1) Save the file to your local computer for example:

C:\Documents and Settings\[user]\Desktop\Webservice1.asmx

2) In Visual Studio Right Click on your project > Choose Add Web Reference, A dialog will open.

3) In the URL Box Copy the local file location above C:\Documents and Settings[user]\Desktop\Webservice1.asmx, Click Next

4) Now you will see the functions appear, choose your name for the reference, Click add reference

5) You are done! you can start using it as a namespace in your application don't worry that you used a local file, because anyway the true URL for the service is located in the file at the Binding section

Israel Margulies
  • 8,656
  • 2
  • 30
  • 26