vb.netwcf-endpoint

How to conditionally set ServiceModel.EndpointAddress URI at runtime


I have a class in which the variable endpoint needs to be global (to other subs and functions in the 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

The problem is that ServiceModel.EndpointAddress has no constructor that accepts a parameter of its own type (i.e. "copy constructor").

Nor does it have a default constructor that allows setting the URI later.

What is the proper way of achieving what I want to do in VB.NET?


Solution

  • Just don't create a new one. Use the one you already have:

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

    Alternatively, you could keep the two endpoint addresses as strings rather than as EndpointAddress objects:

    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