简体   繁体   English

通过WSDL url使用Web服务

[英]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 !! 我需要使用wsdl url使用web服务,在互联网上搜索后,我使用wsdl.exe命令行生成了一个类库,然后从类生成实例并从类中发送带有对象的参数但是我收到此错误!

在此输入图像描述

Also I generated dll library from wsdl url and used it in a console project but I get the same error. 我也从wsdl url生成了dll library并在控制台项目中使用它,但是我得到了同样的错误。

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. 源Web服务是(Service.svc)并包含许多方法。

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. 如何使用我使用svcutil工具生成的文件(Service.cs,output.config)我需要任何解决方案来访问该服务。

There should be a [service_name]Client class generated by svcutil.exe in [service_name]Service.svc . 应该有[service_name] Service.svc中的 svcutil.exe生成的[service_name]Client类。 Also, in output.config should be configuration for web service. 另外,在output.config应该是Web服务的配置。 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. 您可以将该配置复制到App.config,然后使用带有参数string endPointConfigurationName客户端类的构造函数(也应该生成它)来使用此配置。

EDIT: 编辑:
You have to know the configuration name from your App.config . 您必须知道App.config中的配置名称。 For now, let's assume that it is "ConfigurationName" . 现在,让我们假设它是“ConfigurationName” Then: 然后:

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

use using keyword for automatically Dispose the client object. 使用using关键字自动Dispose client对象。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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