SOAP 连接 WCF

发布于 2024-12-28 05:07:47 字数 381 浏览 1 评论 0原文

我正在连接到第三方 API。他们向我提供了一个 WSDL 文件,我使用 Web 参考添加了该文件。是否可以通过 SOAP 连接并通过同一连接发送多条消息?

我已经从代理类创建了客户端,但似乎没有 Open() 或 Close() 方法。调用方法时客户端是否连接和断开?

SampleService client = new SampleService

client.SampleMethod();

编辑:

我从 WSDL 文件中添加了“服务参考”。客户端是根据 WSDL 文件中的“PortType”构建的。有一个 Close() 或 Abort() 方法。 SampleService.client 上的唯一方法是 SampleMethod()

I am connecting to a 3rd party API. They have provided me with a WSDL file which i have added by using a web reference. Is it possible to connect via SOAP and send multiple messages across the same connection?

I have created the client from the proxy classes but there doesn't appear to be a Open() or a Close() method. Does the client connect and disconnect when a method is called?

SampleService client = new SampleService

client.SampleMethod();

Edit:

I have added a "Service Reference" from the WSDL file. The client is constructed from the "PortType" in the WSDL file. There inst a Close() or a Abort() method. The only method on SampleService.client is SampleMethod()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

指尖上得阳光 2025-01-04 05:07:48

他们向我提供了一个 WSDL 文件,我使用 Web 参考添加了该文件。

在这种情况下,您使用的不是 WCF,而是 ASP.NET WebServices 客户端。在您的情况下,这可能没有太大区别,但 ASP.NET WebServices 主要是为了在将遗留代码移动到新版本的 .NET 框架时向后兼容。今天,您应该像其他建议一样使用“添加服务引用”来使用 WCF。

SOAP 连接 WCF

没有什么比 SOAP 连接更好的了。 SOAP 是通过某种传输协议建立隧道的应用程序协议。在您的情况下,传输协议是 HTTP。

我已经从代理类创建了客户端,但没有
似乎是 Open() 或 Close() 方法。客户端是否连接并且
调用方法时断开连接?

您不需要调用OpenClose。这都是在通信堆栈的低层处理的。默认情况下,所有 HTTP 客户端应用程序都使用称为持久连接的东西。这意味着当您第一次访问某个主机时,您的客户端计算机和托管目标资源的服务器之间会建立 TCP 连接。后续调用将重用同一连接。如果连接在某个预定义的时间内没有使用(客户端和服务器都可以有不同的超时),则连接将关闭,下次客户端想要调用服务器时,它将创建一个新连接(同样,您不需要打扰与此)。

因此,如果没有人存在,在应用程序中调用 SOAP 代理上的方法可以自动打开连接。您不需要显式关闭连接,但应该处置代理以释放其资源。连接本身可以在代理被处置后继续存在,并且可以被另一个代理重用。

They have provided me with a WSDL file which i have added by using a web reference.

In such case you are not using WCF but ASP.NET WebServices client. In your case it is probably not a big difference but ASP.NET WebServices are mostly for backward compatibility when moving legacy code to new version of .NET framework. Today you should rather use Add Service Reference to use WCF as other also recommended.

SOAP Connection WCF

There is nothing like SOAP connection. SOAP is application protocol tunneled through some transport protocol. In your case the transport protocol is HTTP.

I have created the client from the proxy classes but there doesn't
appear to be a Open() or a Close() method. Does the client connect and
disconnect when a method is called?

You don't need to call Open or Close. This is all handled in low level layer of communication stack. By default all HTTP client applications use something called persistent connection. It means that when you access some host for the first time TCP connection is established between your client computer and server hosting target resource. Subsequent calls reuse the same connection. If the connection is not in use for some predefined time (both client and server can have different timeouts) the connection is closed and next time the client wants to call the server it will create a new connection (again you don't need to bother with this).

So calling the method on SOAP proxy in your application can automatically open connection if no one exists. You don't need to explicitly close the connection but you should dispose the proxy to release its resources. Connection itself can live after the proxy was disposed and can be reused by another proxy.

岁月蹉跎了容颜 2025-01-04 05:07:48

您应该添加服务引用 (WCF),而不是使用过时的 Web 引用。

然后按照以下模式操作:

try
{
    SampleServiceClient client = new SampleServiceClient();
    client.SampleMethod();
    //... Make as many calls to as many methods as you like.
    client.SampleMethod();
    client.Close();
}
catch (Exception e)
{
    //client is unusable once an exception occurs.
    ...
    client.Abort();
    throw;
}

You should add a service reference (WCF) instead of using the outdated Web Reference.

Then follow the bellow pattern:

try
{
    SampleServiceClient client = new SampleServiceClient();
    client.SampleMethod();
    //... Make as many calls to as many methods as you like.
    client.SampleMethod();
    client.Close();
}
catch (Exception e)
{
    //client is unusable once an exception occurs.
    ...
    client.Abort();
    throw;
}
几度春秋 2025-01-04 05:07:48

您需要使用“添加服务引用”而不是“添加 Web 引用”。添加后,您可以调用该服务,如下所示:

using(SampleServiceClient client = new SampleServiceClient())
{
    var response = client.SampleMethod();
}

希望有帮助。

You need to use the Add Service Reference rather than Add Web Reference. And once added you can invoke the service as shown:

using(SampleServiceClient client = new SampleServiceClient())
{
    var response = client.SampleMethod();
}

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文