How to use a WSDL
Asked Answered
P

4

142

I need to consume a Web Service. They sent me the WSDL file. What should I do to add it to my website and start using it as the proxy. ( If I put it on a Virtual Directory it can be discovered, but does it grant me the connection with the real web service?)

Programmer answered 19/8, 2009 at 20:42 Comment(2)
Well, I upvoted because it's a question that I think other users here would benefit from seeing an answer to. I was searching for this exact issue and I'm happy I found it here.Posting
I understand MS was involved in early development of WSDL so can understand the capabilities in support this in VS and .NET, as is below which is very helpful. However given the question was not really platform specifics, is it possible to enhance this further to provide alternative ways of doing so (i.e. Java, Python, etc.)?Eufemiaeugen
C
159

I would fire up Visual Studio, create a web project (or console app - doesn't matter).

For .Net Standard:

  1. I would right-click on the project and pick "Add Service Reference" from the Add context menu.
  2. I would click on Advanced, then click on Add Service Reference.
  3. I would get the complete file path of the wsdl and paste into the address bar. Then fire the Arrow (go button).
  4. If there is an error trying to load the file, then there must be a broken and unresolved url the file needs to resolve as shown below: enter image description here Refer to this answer for information on how to fix: Stackoverflow answer to: Unable to create service reference for wsdl file

If there is no error, you should simply set the NameSpace you want to use to access the service and it'll be generated for you.

For .Net Core

  1. I would right click on the project and pick Connected Service from the Add context menu.
  2. I would select Microsoft WCF Web Service Reference Provider from the list.
  3. I would press browse and select the wsdl file straight away, Set the namespace and I am good to go. Refer to the error fix url above if you encounter any error.

Any of the methods above will generate a simple, very basic WCF client for you to use. You should find a "YourservicenameClient" class in the generated code.

For reference purpose, the generated cs file can be found in your Obj/debug(or release)/XsdGeneratedCode and you can still find the dlls in the TempPE folder.

The created Service(s) should have methods for each of the defined methods on the WSDL contract.

Instantiate the client and call the methods you want to call - that's all there is!

YourServiceClient client = new YourServiceClient();
client.SayHello("World!");

If you need to specify the remote URL (not using the one created by default), you can easily do this in the constructor of the proxy client:

YourServiceClient client = new YourServiceClient("configName", "remoteURL");

where configName is the name of the endpoint to use (you will use all the settings except the URL), and the remoteURL is a string representing the URL to connect to (instead of the one contained in the config).

Cathepsin answered 19/8, 2009 at 20:48 Comment(6)
This works fine. Only one question, if I have the wsdl file on my machine, and I add the reference to this file. When executing the Url it hits is the one hosting the web service, or do I have to especify the Binding and the EndPoint?Programmer
That's up to you - by default, the code created for the client proxy will have the URL that's contained inside the WSDL - if you need to go to another URL, you need to specify it yourself.Cathepsin
Why is this so easy? Isn't there a catch? :p Thanks!Shulem
As the accepted answer it would be good if you also mentioned how to use a web hosted WSDL from a URL as well.Reilly
I think I am fairly close to getting this to work. I have the Web Service installed using the Add Service Reference tool. I am able to Instantiate the client, but calling the methods is where I fail. The end point is using a Java implementation, and all I can see is the XML. The method I am attempting to call is looking for a clientid and "something else". That something else is a class. Which is puzzling. I can put this in a post below if that would help, but I am baffled.Barris
I do understand this is an old thread, but could anyone advise why I am unable to see any client-generated for me. I am struggling to find a way to code the client.Authentic
T
48

In visual studio.

  • Create or open a project.
  • Right-click project from solution explorer.
  • Select "Add service refernce"
  • Paste the address with WSDL you received.
  • Click OK.

If no errors, you should be able to see the service reference in the object browser and all related methods.

Twannatwattle answered 19/8, 2009 at 20:46 Comment(6)
<3 Visual Studio.. Yes, it's really that simple :)Transect
The OP doesn't have an address but a file.Saundrasaunter
The provider of the Web service may have disabled WSDL (security by obscurity), so there is no URL where WSDL can be accessed and then adding a Web reference in VS won't work. However, the provider does want YOU (I mean OP) to know all you need to know about the service contract, that's why they sent the WSDL file. Now the OP needs to generate the proxy code from it, the question is how.Attribute
By typing in the path to the file. What makes you think that doesn't work?Whitby
Hi @vidalsasoon. Could you explain briefly how do I go about using these classes I see in my class explorer. ?Undergarment
Quick question. What if the address being reference in the WSDL is not accessible to the OP's machine. You can't just add a reference like this.Jackiejackinoffice
A
17

Use WSDL.EXE utility to generate a Web Service proxy from WSDL.

You'll get a long C# source file that contains a class that looks like this:

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.42")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Web.Services.WebServiceBindingAttribute(Name="MyService", Namespace="http://myservice.com/myservice")]
public partial class MyService : System.Web.Services.Protocols.SoapHttpClientProtocol {
    ...
}

In your client-side, Web-service-consuming code:

  1. instantiate MyService.
  2. set its Url property
  3. invoke Web methods
Attribute answered 19/8, 2009 at 21:1 Comment(1)
This creates an "old-style" ASMX Webservice - those are obsolete - one should use WCF instead nowadaysCathepsin
V
11

If you want to add wsdl reference in .Net Core project, there is no "Add web reference" option.

To add the wsdl reference go to Solution Explorer, right-click on the References project item and then click on the Add Connected Service option.

enter image description here

Then click 'Microsoft WCF Web Service Reference':

enter image description here

Enter the file path into URI text box and import the WSDL:

enter image description here

It will generate a simple, very basic WCF client and you to use it something like this:

YourServiceClient client = new YourServiceClient();
client.DoSomething();
Vedi answered 31/5, 2018 at 11:19 Comment(1)
This extension doesn't appear to me.Drizzle

© 2022 - 2024 — McMap. All rights reserved.