简体   繁体   English

配置WCF以使用两个端点之一

[英]Configuring WCF to use one of two endpoints

How to configure WCF to use just one of two available endpoints? 如何配置WCF只使用两个可用的一个端点?

I need two TCP ports (and thus using netTcpBinding ). 我需要两个TCP端口(并因此使用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). 我知道可以通过编程实现,但是我打算以声明的方式实现(仅使用.config文件)。

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()

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. 然后,在客户端上,您需要进行编程,使其首先尝试第一个端口,然后如果失败,则尝试第二个端口。

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

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