Loading System.ServiceModel configuration section using ConfigurationManager

前端 未结 5 1592
借酒劲吻你
借酒劲吻你 2020-12-07 18:30

Using C# .NET 3.5 and WCF, I\'m trying to write out some of the WCF configuration in a client application (the name of the server the client is connecting to).

The o

5条回答
  •  伪装坚强ぢ
    2020-12-07 19:11

    Thanks to the other posters this is the function I developed to get the URI of a named endpoint. It also creates a listing of the endpoints in use and which actual config file was being used when debugging:

    Private Function GetEndpointAddress(name As String) As String
        Debug.Print("--- GetEndpointAddress ---")
        Dim address As String = "Unknown"
        Dim appConfig As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
        Debug.Print("app.config: " & appConfig.FilePath)
        Dim serviceModel As ServiceModelSectionGroup = ServiceModelSectionGroup.GetSectionGroup(appConfig)
        Dim bindings As BindingsSection = serviceModel.Bindings
        Dim endpoints As ChannelEndpointElementCollection = serviceModel.Client.Endpoints
        For i As Integer = 0 To endpoints.Count - 1
            Dim endpoint As ChannelEndpointElement = endpoints(i)
            Debug.Print("Endpoint: " & endpoint.Name & " - " & endpoint.Address.ToString)
            If endpoint.Name = name Then
                address = endpoint.Address.ToString
            End If
        Next
        Debug.Print("--- GetEndpointAddress ---")
        Return address
    End Function
    

提交回复
热议问题