How to Use a Wsdl

How to use a WSDL

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:
    Sample Image
    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).

How to consume WSDL web service

I am assuming you are using a c# client to consume a WCF service. You need to add service reference to your client project. This creates necessary classes from the WSDL and helps you to create request to call web service and get response. Take a look at this http://www.c-sharpcorner.com/UploadFile/0c1bb2/consuming-wcf-service-in-console-application/

How to get the wsdl file from a webservice's URL

By postfixing the URL with ?WSDL

If the URL is for example:

http://webservice.example:1234/foo

You use:

http://webservice.example:1234/foo?WSDL

And the wsdl will be delivered.

How to consume a wsdl file that doesn't exist on web in C#

I remember, that I had a problem with authentication of a web service some years ago, but I didn't remember where I found this alternative solution:

  • Create a new console app in visual studio (I am not sure, if deleting the old service reference in the existing project works without manual editing project file etc.)
  • Select add service reference as before, but don't enter the path of a wsdl. Instead press the button "Advanced..." in the dialog and "Add Web reference..." in the next dialog.
  • In the new dialog enter the path of the wsdl file and press "Add reference"
  • Open the file "program.cs" in the project and add this class with namespace between the last "using..." line and "namespace...."

    namespace TestWeb.WebReference 
    {
    partial class rmt_ws : System.Web.Services.Protocols.SoapHttpClientProtocol {
    protected override System.Net.WebRequest GetWebRequest(Uri uri)
    {
    System.Net.HttpWebRequest request;

    request = (System.Net.HttpWebRequest)base.GetWebRequest(uri);

    if (PreAuthenticate) {
    System.Net.NetworkCredential networkCredentials = Credentials.GetCredential(uri, "Basic");
    if (networkCredentials != null) {
    System.Diagnostics.Debug.WriteLine("User: " + networkCredentials.UserName);
    System.Diagnostics.Debug.WriteLine("PW: " + networkCredentials.Password);
    byte[] credentialBuffer = new UTF8Encoding().GetBytes(networkCredentials.UserName + ":" + networkCredentials.Password);
    request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(credentialBuffer);
    }
    else {
    throw new ApplicationException("No network credentials");
    }
    }
    return request;
    }
    }
  • Replace "TestWeb" (first line of inserted code) with the namespace of your project (normally the project name).

  • Replace the existing class "Program" with the following code:

    class Program {
    static void Main(string[] args) {
    // Fill with a valid xml request
    String inputXML = "";
    String answer = "";
    try {
    WebReference.rmt_ws _webService = new WebReference.rmt_ws();
    System.Net.CredentialCache myCredentials = new System.Net.CredentialCache();
    // Set correct user and password
    System.Net.NetworkCredential netCred = new System.Net.NetworkCredential("User", "Password");
    _webService.Credentials = netCred.GetCredential(new Uri(_webService.Url), "Basic");
    _webService.PreAuthenticate = true;
    answer = _webService.remittanceXml(inputXML);
    }
    catch (Exception ex) {
    Console.WriteLine(ex.Message);
    }
    Console.WriteLine(answer);
    Console.ReadLine();
    }
    }
  • Set inputXML with a valid xml for your request

  • Set the correct user and password

If this does not help you should really open a new question to find some one other who could help.

How to Consume a WebService using WSDL files in C#

You already found the solution. Use the "Add Service Reference" dialog and make sure your service is accessible by a URL. To do this either request the URL by the people offering the service or deploy the service in IIS.

Personally I would forget about svcutil.exe. If you have Visual Studio, it is much easier to add and update the service reference using the excellent integration of web services in Visual Studio.

Visual Studio 2017 Community consume WSDL

Right Click your project> select "Add (D)" > click "Service Reference (S)"> Input your WSDL url and click "Move (G)" if you have the URL or "Explore (D)" if you have the WSDL in your solution. It should then pop-up below, and remember to NAME your namingspace(N), they are important. And "OK". It should pop-up in your project.

The above solution is only valid to certain project types. Eg: Xamarin.form doesnt have "Service Reference", hence alternative solution for that.

ELSE enjoy!

As for how to use those newly added WSDL. Simply go to your program and new them like any other classes or call them as you normally would. If it's in another project, then you have to USING XXXX;

C# client how to invoke wsdl file

You don't invoke WSDL file, you add service reference from the file.

To add reference, right click on the project, select Add Service Reference. Paste path to your wsdl file and hit Go.

Sample Image

If you want to use legacy Web Service client, select Add Web Reference and paste path to the wsdl file from there.

I recommend to use WCF (Add Service Reference option).

To use the service reference add code like this:

var serviceClient = new ServiceReferenceName.MyClassClient();
serviceClient.DoSomething();

You also need to update config file with the server URL that you customer should provide you with:

<client>
<endpoint address="http://UrlFromYourCustomerHere"
binding="basicHttpBinding"
bindingConfiguration="xxx"
contract="MyServiceReference.xxx"
name="xxx/>
</client>


Related Topics



Leave a reply



Submit