private async Task <bool> TryConnect() { if (connection != null) { return(true); } if (string.IsNullOrEmpty(config.Address)) { return(false); } try { var getEndpointsRequest = new GetEndpointsRequest { EndpointUrl = config.Address, ProfileUris = new[] { TransportProfileUris.UaTcpTransport } }; GetEndpointsResponse endpoints = await UaTcpDiscoveryService.GetEndpointsAsync(getEndpointsRequest); EndpointDescription[] noSecurityEndpoints = endpoints.Endpoints.Where(e => e.SecurityPolicyUri == SecurityPolicyUris.None).ToArray(); var(endpoint, userIdentity) = FirstEndpointWithLogin(noSecurityEndpoints); if (endpoint == null || userIdentity == null) { throw new Exception("No matching endpoint"); } var channel = new UaTcpSessionChannel( this.appDescription, null, userIdentity, endpoint, loggerFactory); await channel.OpenAsync(); this.connection = channel; PrintLine($"Opened session with endpoint '{channel.RemoteEndpoint.EndpointUrl}'."); PrintLine($"SecurityPolicy: '{channel.RemoteEndpoint.SecurityPolicyUri}'."); PrintLine($"SecurityMode: '{channel.RemoteEndpoint.SecurityMode}'."); PrintLine($"UserIdentityToken: '{channel.UserIdentity}'."); ItemInfo[] nodesNeedingResolve = mapId2Info.Values.Where(n => n.Node == null).ToArray(); if (nodesNeedingResolve.Length > 0) { PrintLine($"Resolving node ids for {nodesNeedingResolve.Length} items..."); TranslateBrowsePathsToNodeIdsRequest req = new TranslateBrowsePathsToNodeIdsRequest() { BrowsePaths = nodesNeedingResolve.Select(n => new BrowsePath() { StartingNode = n.StartingNode, RelativePath = n.RelativePath }).ToArray() }; TranslateBrowsePathsToNodeIdsResponse resp = await connection.TranslateBrowsePathsToNodeIdsAsync(req); if (resp.Results.Length != nodesNeedingResolve.Length) { LogWarn("Mismatch", "TranslateBrowsePathsToNodeIds failed"); } else { for (int i = 0; i < resp.Results.Length; ++i) { BrowsePathResult x = resp.Results[i]; if (StatusCode.IsGood(x.StatusCode) && x.Targets.Length > 0) { NodeId id = x.Targets[0].TargetId.NodeId; nodesNeedingResolve[i].Node = id; PrintLine($"Resolved item '{nodesNeedingResolve[i].Name}' => {id}"); } else { PrintLine($"Could not resolve item '{nodesNeedingResolve[i].Name}'!"); } } } } return(true); } catch (Exception exp) { Exception baseExp = exp.GetBaseException() ?? exp; LogWarn("OpenChannel", "Open channel error: " + baseExp.Message, dataItem: null, details: baseExp.StackTrace); await CloseChannel(); return(false); } }