将 WCF 配置为使用两个端点之一

发布于 2024-08-19 12:45:05 字数 212 浏览 4 评论 0原文

如何将 WCF 配置为仅使用两个可用端点中的一个?

我需要两个 TCP 端口(因此使用 netTcpBinding)。服务主机应首先尝试绑定到第一个端口。如果失败,并且只有失败,它才应该尝试绑定到第二个端口。

编辑

我知道它可以通过编程方式实现,但我打算以声明方式实现它(仅使用.config文件)。

How to configure WCF to use just one of two available endpoints?

I need two TCP ports (and thus using netTcpBinding). The service host should first try to bind to the first port. If it fails, and only if it fails, it should try to bind to the second port.

EDIT

I known it can be achived programatically, but my intention to do it declaratively (using .config files only).

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

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

发布评论

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

评论(2

爱情眠于流年 2024-08-26 12:45:05

在使用代理对象打开连接之前,可以在流程中的任何时刻在代码中设置端点地址(包括端口号)。所以你可以设置地址然后测试连接,如果失败再尝试其他端口。这里有一些代码希望能够说明我的观点。

Dim oProxy as New YourWCFServiceType()

oProxy.Endpoint.Address = New System.ServiceModel.EndpointAddress(New Uri("The address and port number you want to try first"))

Dim FirstBindingSucceeded as Boolean
Try
    oProxy.Open()
    FirstBindingSucceeded = True
Catch
End Try

If FirstBindingSucceeded = False Then
    oProxy.Endpoint.Address = New System.ServiceModel.EndpointAddress(New Uri("The address and port number you want to try second"))
End If

oProxy.Open()

The endpoint address, including the port number, can be set in code at any point in the process before you open a connection using your proxy object. So you can set the address and then test the connection, and if it fails, try the other port. Here's some code that hopefully illustrates my point.

Dim oProxy as New YourWCFServiceType()

oProxy.Endpoint.Address = New System.ServiceModel.EndpointAddress(New Uri("The address and port number you want to try first"))

Dim FirstBindingSucceeded as Boolean
Try
    oProxy.Open()
    FirstBindingSucceeded = True
Catch
End Try

If FirstBindingSucceeded = False Then
    oProxy.Endpoint.Address = New System.ServiceModel.EndpointAddress(New Uri("The address and port number you want to try second"))
End If

oProxy.Open()
指尖上的星空 2024-08-26 12:45:05

在服务器端,公开具有两个绑定的服务是没有问题的。

但在客户端,您将收到重复的契约错误(或类似的文字)。

一种方法是创建两个除了名称之外相同的接口(契约)。

您有一个实现的副本,每个服务都继承自该实现。

然后,您可以在不同的端口上拥有两个具有相同实现/功能的服务。

然后,在客户端上,您需要对其进行编程,使其首先尝试第一个端口,如果失败,则尝试第二个端口。

On the server side there is no problem exposing a service with two bindings.

But on the client side you will get a duplicate contract error (or words to that effect)

One way to do it is to create two interfaces (contracts) that are identical except for the name.

You have a single copy of the implementation, each service inherits from this implementation.

You then have two services on different ports, that have the same implementation / functionality.

On the client you then need to program that it first attempts the first port and then if that fails it attempts the second.

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