简体   繁体   中英

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 !!

在此输入图像描述

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.

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"));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM