From 9f573bea172c8a63bd95ddca2644e1c276fe51c0 Mon Sep 17 00:00:00 2001 From: Marc Schier Date: Thu, 23 Feb 2017 08:57:45 +0000 Subject: [PATCH] Fix session.create to catch any exceptions, patch host name, plus better log messages (#17) * Fix session.create to catch any exceptions, plus better log messages * Updates based on code review feedback --- samples/gateway_config.json | 4 +- src/Opc.Ua.Client.Module/Module.cs | 62 ++++++++++++++++++------------ 2 files changed, 40 insertions(+), 26 deletions(-) diff --git a/samples/gateway_config.json b/samples/gateway_config.json index 24b2075..6c5932b 100644 --- a/samples/gateway_config.json +++ b/samples/gateway_config.json @@ -23,8 +23,8 @@ "Id": "", "SharedAccessKey": "", "ServerUrl": "opc.tcp://:51210/UA/SampleServer", - "MinimumSecurityLevel": 0, - "MinimumSecurityMode": "SignAndEncrypt", + "MinimumSecurityLevel": 0, + "MinimumSecurityMode": "SignAndEncrypt", "PublishingInterval": 400, "MonitoredItems": [ { diff --git a/src/Opc.Ua.Client.Module/Module.cs b/src/Opc.Ua.Client.Module/Module.cs index 00480c6..61e35be 100644 --- a/src/Opc.Ua.Client.Module/Module.cs +++ b/src/Opc.Ua.Client.Module/Module.cs @@ -344,6 +344,10 @@ namespace Opc.Ua.Client endpoint.SecurityLevel >= MinimumSecurityLevel && endpoint.SecurityMode >= MinimumSecurityMode) { + // patch endpoint to set the original host name we want to connect to. + var url = new UriBuilder(endpoint.EndpointUrl); + url.Host = ServerUrl.Host; + endpoint.EndpointUrl = url.ToString(); selectedEndpoints.Add(endpoint); } } @@ -361,36 +365,46 @@ namespace Opc.Ua.Client ConfiguredEndpoint configuredEndpoint = new ConfiguredEndpoint( endpoint.Server, EndpointConfiguration.Create(Module.Configuration)); configuredEndpoint.Update(endpoint); - - _session = await Session.Create( - Module.Configuration, - configuredEndpoint, - true, - false, - Module.Configuration.ApplicationName, - 60000, - new UserIdentity(new AnonymousIdentityToken()), - null); - - if (_session != null) + try { - var subscription = new Subscription(_session.DefaultSubscription); - subscription.PublishingInterval = PublishingInterval; + Console.WriteLine($"Opc.Ua.Client.SampleModule: Establishing session with mode: {endpoint.SecurityMode}, level:{endpoint.SecurityLevel} to {configuredEndpoint.EndpointUrl}..."); + _session = await Session.Create( + Module.Configuration, + configuredEndpoint, + true, + false, + Module.Configuration.ApplicationName, + 60000, + // TODO: Make user identity configurable, plus add dedicated security policy + new UserIdentity(new AnonymousIdentityToken()), + null); - // TODO: Make other subscription settings configurable... + if (_session != null) + { + var subscription = new Subscription(_session.DefaultSubscription); + subscription.PublishingInterval = PublishingInterval; - subscription.AddItems(MonitoredItems); - _session.AddSubscription(subscription); - subscription.Create(); + // TODO: Make other subscription settings configurable... + subscription.AddItems(MonitoredItems); + _session.AddSubscription(subscription); + subscription.Create(); - Console.WriteLine($"Opc.Ua.Client.SampleModule: Created session with updated endpoint {configuredEndpoint.EndpointUrl} from server!"); - _session.KeepAlive += new KeepAliveEventHandler(StandardClient_KeepAlive); + Console.WriteLine($"Opc.Ua.Client.SampleModule: Session with server endpoint {configuredEndpoint.EndpointUrl} established!"); + _session.KeepAlive += new KeepAliveEventHandler(StandardClient_KeepAlive); - // Done - return; + // Done + return; + } + else + { + Console.WriteLine($"Opc.Ua.Client.SampleModule: WARNING Could not create session to endpoint {endpoint.ToString()}..."); + } + } + catch (Exception ex) + { + Console.WriteLine($"Opc.Ua.Client.SampleModule: ERROR Exception creating session to endpoint {endpoint.ToString()}..."); + Console.WriteLine($"Opc.Ua.Client.SampleModule: {ex.ToString()}"); } - - Console.WriteLine($"Opc.Ua.Client.SampleModule: WARNING Could not create session to endpoint {endpoint.ToString()}..."); // ... try another endpoint until we do not have any more... }