Opc.Ua.ServerTest.ServerTestClient.GetEndpoints C# (CSharp) Method

GetEndpoints() private method

Reads the endpoints for a server.
private GetEndpoints ( ConfiguredEndpoint endpoint ) : ConfiguredEndpointCollection
endpoint ConfiguredEndpoint
return ConfiguredEndpointCollection
        private ConfiguredEndpointCollection GetEndpoints(ConfiguredEndpoint endpoint)
        {
            // some protocols will require a seperate endpoint for the discovery endpoints.
            List<Uri> urlsToTry = new List<Uri>();

            foreach (string discoveryUrl in endpoint.Description.Server.DiscoveryUrls)
            {
                urlsToTry.Add(new Uri(discoveryUrl));
            }

            urlsToTry.Add(endpoint.EndpointUrl);

            // servers that do not support using the same endpoint for discovery and sessions should
            // use the convention where the discovery endpoint is constructed by appending "/discovery";
            if (endpoint.EndpointUrl.Scheme != Utils.UriSchemeOpcTcp)
            {
                urlsToTry.Add(new Uri(endpoint.EndpointUrl.ToString() + "/discovery"));
            }

            for (int ii = 0; ii < urlsToTry.Count; ii++)
            {
                // the discovery client provides a programmer's interace to a discovery server.
                DiscoveryClient client = DiscoveryClient.Create(urlsToTry[ii], m_bindingFactory, null);

                try
                {
                    // fetch the endpoints from the server.
                    EndpointDescriptionCollection endpoints = client.GetEndpoints(null);

                    // add endpoints to cache.
                    ConfiguredEndpointCollection endpointCache = new ConfiguredEndpointCollection();

                    foreach (EndpointDescription description in endpoints)
                    {
                        endpointCache.Add(description, endpoint.Configuration);
                    }

                    return endpointCache;
                }
                catch (Exception)
                {                    
                    Report("No discovery endpoint found at: {0}", urlsToTry[ii]);
                }
                finally
                {
                    // must always close the channel to free resources.
                    client.Close();
                }
            }
             
            // nothing found.
            throw ServiceResultException.Create(
                StatusCodes.BadUnexpectedError, 
                "Could not find discovery endpoint for server at: {0}", 
                endpoint);
        }