user7245746
user7245746

Reputation:

Consume a web service via WSDL url

I need to consume a web service using a wsdl url, after searching on internet, I generated a class library using wsdl.exe command-line, then made instance from the class and send parameter with object from the class but I am getting this error !!

enter image description here

Also I generated dll library from wsdl url and used it in a console project but I get the same error.

namespace ConsoleProject
{
    class Program
    {
        static void Main(string[] args)
        {

            Services.Service obj = new Services.Service();
            Console.WriteLine(obj.MethodName("Param1", "Param2"));
            Console.ReadLine();


        }
    }
}

the source web service is (Service.svc) and contains many methods.

What I am missing? And how to use the files I generated using svcutil tool(Service.cs , output.config) I need any solution to access the service.

Upvotes: 3

Views: 706

Answers (1)

Viacheslav
Viacheslav

Reputation: 120

There should be a [service_name]Client class generated by svcutil.exe in [service_name]Service.svc. Also, in output.config should be configuration for web service. You can copy that configuration to your App.config and than use constructor of client class with parameter string endPointConfigurationName (it is also should be generated) to use this configuration.

EDIT:
You have to know the configuration name from your App.config. For now, let's assume that it is "ConfigurationName". Then:

var configurationName = "ConfigurationName";
using (var client = new ServiceClient(configurationName))
{
    client.MethodName("Param1", "Param2");
}

use using keyword for automatically Dispose the client object.

UPDATE:

If you need to print the result of method of added service do:

var configurationName = "ConfigurationName";
using (var client = new ServiceClient(configurationName))
{
    Console.WriteLine(client.MethodName("Param1", "Param2"));
}

Upvotes: 1

Related Questions