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
This commit is contained in:
Родитель
507e0fe94c
Коммит
9f573bea17
|
@ -23,8 +23,8 @@
|
|||
"Id": "<DeviceID>",
|
||||
"SharedAccessKey": "<SharedAccessKey>",
|
||||
"ServerUrl": "opc.tcp://<hostname>:51210/UA/SampleServer",
|
||||
"MinimumSecurityLevel": 0,
|
||||
"MinimumSecurityMode": "SignAndEncrypt",
|
||||
"MinimumSecurityLevel": 0,
|
||||
"MinimumSecurityMode": "SignAndEncrypt",
|
||||
"PublishingInterval": 400,
|
||||
"MonitoredItems": [
|
||||
{
|
||||
|
|
|
@ -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...
|
||||
}
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче