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

时间:2015-03-26 18:24:01

标签: vb.net wcf-endpoint

我有一个类,其中变量端点需要是全局的(到类中的其他子和函数):

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没有接受其自身类型参数的构造函数(即“复制构造函数”)。

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

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

1 个答案:

答案 0 :(得分:1)

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

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