如何在运行时有条件地设置 ServiceModel.EndpointAddress URI

How to conditionally set ServiceModel.EndpointAddress URI at runtime

我有一个 class,其中变量端点需要是全局的(对于 class 中的其他子程序和函数):

Public Class MyFirstVbNetClassEver
    Dim testEndpoint As New ServiceModel.EndpointAddress("https://test.my.employer.com/ws/soap?wsdl")
    Dim productionEndpoint As New ServiceModel.EndpointAddress("https://my.employer.com/ws/soap?wsdl")

    Public Sub Run()
        If (PRDOCUTION) Then
           Dim endpoint As New ServiceModel.EndpointAddress(productionEndpoint )
        Else
           Dim endpoint As New ServiceModel.EndpointAddress(testEndpoint )
        End If
    End Sub
End Class

问题是 ServiceModel.EndpointAddress 没有接受其自身类型参数的构造函数(即 "copy constructor")。

它也没有允许稍后设置 URI 的默认构造函数。

在 VB.NET 中实现我想做的事情的正确方法是什么?

只是不要创建一个新的。使用您已有的那个:

Public Sub Run()
    Dim endpoint As ServiceModel.EndpointAddress = Nothing
    If (PRDOCUTION) Then
       endpoint = productionEndpoint
    Else
       endpoint = testEndpoint
    End If
    ' ...
End Sub

或者,您可以将两个端点地址保留为字符串而不是 EndpointAddress 对象:

Public Class MyFirstVbNetClassEver
    Dim testUri As String = "https://test.my.employer.com/ws/soap?wsdl"
    Dim productionUri As String = "https://my.employer.com/ws/soap?wsdl"

    Public Sub Run()
        If (PRDOCUTION) Then
           Dim endpoint As New ServiceModel.EndpointAddress(productionUri)
        Else
           Dim endpoint As New ServiceModel.EndpointAddress(testUri)
        End If
    End Sub
End Class