gpt4 book ai didi

c# - 通过 WSDL url 使用 Web 服务

转载 作者:行者123 更新时间:2023-11-30 17:27:45 24 4
gpt4 key购买 nike

我需要使用 wsdl url 使用 Web 服务,在互联网上搜索后,我使用 wsdl.exe 命令行 生成了一个类库,然后从该类创建实例并发送参数来自类(class)的对象,但我收到此错误!

enter image description here

我还从 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();


}
}
}

源 Web 服务是 (Service.svc) 并且包含许多方法。

我错过了什么?以及如何使用我使用svcutil 工具(Service.cs、output.config)生成的文件我需要任何解决方案来访问该服务。

最佳答案

[service_name]Service.svc 中应该有一个由svcutil.exe 生成的[service_name]Client 类。此外,在 output.config 中应该是 web 服务的配置。您可以将该配置复制到您的 App.config,然后使用带有参数 string endPointConfigurationNameclient 类的构造函数(它也应该生成)使用此配置。

编辑:
您必须从 App.config 中知道配置名称。现在,我们假设它是 "ConfigurationName"。然后:

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

使用 using 关键字自动Dispose client 对象。

更新:

如果你需要打印添加服务方法的结果:

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

关于c# - 通过 WSDL url 使用 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54416688/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com