@@ -110,60 +110,92 @@ public void Run()
110110
111111 private async Task ConsoleClient ( )
112112 {
113- Log ( conn_name + " - " + "Create an Application Configuration." ) ;
113+ Log ( conn_name + " - " + "Create an Application Configuration... " ) ;
114114 exitCode = ExitCode . ErrorCreateApplication ;
115115
116116 ApplicationInstance application = new ApplicationInstance
117117 {
118118 ApplicationName = "JSON-SCADA OPC-UA Client" ,
119119 ApplicationType = ApplicationType . Client ,
120- ConfigSectionName = ""
120+ ConfigSectionName = "" ,
121121 } ;
122122
123- // load the application configuration.
124- ApplicationConfiguration config = await application . LoadApplicationConfiguration ( OPCUA_conn . configFileName , false ) ;
125- config . SecurityConfiguration . AutoAcceptUntrustedCertificates = true ;
123+ bool haveAppCertificate = false ;
124+ ApplicationConfiguration config = null ;
126125
127- // check the application certificate.
128- bool haveAppCertificate = await application . CheckApplicationInstanceCertificate ( false , 0 ) ;
129-
130- if ( ! haveAppCertificate )
126+ try
131127 {
132- throw new Exception ( "Application instance certificate invalid!" ) ;
133- }
128+ // load the application configuration.
129+ Log ( conn_name + " - " + "Load config from " + OPCUA_conn . configFileName ) ;
130+ config = await application . LoadApplicationConfiguration ( OPCUA_conn . configFileName , false ) ;
131+ // config.SecurityConfiguration.AutoAcceptUntrustedCertificates = true;
134132
135- if ( haveAppCertificate )
136- {
137- config . ApplicationUri = X509Utils . GetApplicationUriFromCertificate ( config . SecurityConfiguration . ApplicationCertificate . Certificate ) ;
138- if ( config . SecurityConfiguration . AutoAcceptUntrustedCertificates )
133+ // check the application certificate.
134+ haveAppCertificate = await application . CheckApplicationInstanceCertificate ( false , 0 ) ;
135+
136+ if ( ! haveAppCertificate )
137+ {
138+ Log ( conn_name + " - " + "FATAL: Application instance certificate invalid!" , LogLevelNoLog ) ;
139+ Environment . Exit ( 1 ) ;
140+ }
141+
142+ if ( haveAppCertificate )
139143 {
140- autoAccept = true ;
144+ config . ApplicationUri = X509Utils . GetApplicationUriFromCertificate ( config . SecurityConfiguration . ApplicationCertificate . Certificate ) ;
145+ if ( config . SecurityConfiguration . AutoAcceptUntrustedCertificates )
146+ {
147+ autoAccept = true ;
148+ }
149+ config . CertificateValidator . CertificateValidation += new CertificateValidationEventHandler ( CertificateValidator_CertificateValidation ) ;
150+ }
151+ else
152+ {
153+ Log ( conn_name + " - " + "WARN: missing application certificate, using unsecure connection." ) ;
141154 }
142- config . CertificateValidator . CertificateValidation += new CertificateValidationEventHandler ( CertificateValidator_CertificateValidation ) ;
143155 }
144- else
156+ catch ( Exception e )
145157 {
146- Log ( conn_name + " - " + " WARN: missing application certificate, using unsecure connection." ) ;
158+ Log ( conn_name + " - WARN: " + e . Message ) ;
147159 }
148160
149- Log ( conn_name + " - " + "Discover endpoints of " + OPCUA_conn . endpointURLs [ 0 ] ) ;
150- exitCode = ExitCode . ErrorDiscoverEndpoints ;
151- var selectedEndpoint = CoreClientUtils . SelectEndpoint ( OPCUA_conn . endpointURLs [ 0 ] , haveAppCertificate && OPCUA_conn . useSecurity , 15000 ) ;
152- Log ( conn_name + " - " + "Selected endpoint uses: " +
153- selectedEndpoint . SecurityPolicyUri . Substring ( selectedEndpoint . SecurityPolicyUri . LastIndexOf ( '#' ) + 1 ) ) ;
161+ if ( config == null )
162+ {
163+ Log ( conn_name + " - " + "FATAL: error in XML config file!" , LogLevelNoLog ) ;
164+ Environment . Exit ( 1 ) ;
165+ }
166+
167+ try
168+ {
169+ Log ( conn_name + " - " + "Discover endpoints of " + OPCUA_conn . endpointURLs [ 0 ] ) ;
170+ exitCode = ExitCode . ErrorDiscoverEndpoints ;
171+ var selectedEndpoint = CoreClientUtils . SelectEndpoint ( OPCUA_conn . endpointURLs [ 0 ] , haveAppCertificate && OPCUA_conn . useSecurity , 15000 ) ;
172+ Log ( conn_name + " - " + "Selected endpoint uses: " +
173+ selectedEndpoint . SecurityPolicyUri . Substring ( selectedEndpoint . SecurityPolicyUri . LastIndexOf ( '#' ) + 1 ) ) ;
154174
155- Log ( conn_name + " - " + "Create a session with OPC UA server." ) ;
156- exitCode = ExitCode . ErrorCreateSession ;
157- var endpointConfiguration = EndpointConfiguration . Create ( config ) ;
158- var endpoint = new ConfiguredEndpoint ( null , selectedEndpoint , endpointConfiguration ) ;
175+ Log ( conn_name + " - " + "Create a session with OPC UA server." ) ;
176+ exitCode = ExitCode . ErrorCreateSession ;
177+ var endpointConfiguration = EndpointConfiguration . Create ( config ) ;
178+ var endpoint = new ConfiguredEndpoint ( null , selectedEndpoint , endpointConfiguration ) ;
159179
160- await Task . Delay ( 50 ) ;
161- session = await Session . Create ( config , endpoint , false , "OPC UA Console Client" , 60000 , new UserIdentity ( new AnonymousIdentityToken ( ) ) , null ) ;
162- // Log("" + session.KeepAliveInterval); // default is 5000
163- session . KeepAliveInterval = System . Convert . ToInt32 ( OPCUA_conn . timeoutMs ) ;
180+ await Task . Delay ( 50 ) ;
181+ session = await Session . Create ( config , endpoint , false , "OPC UA Console Client" , 60000 , new UserIdentity ( new AnonymousIdentityToken ( ) ) , null ) ;
164182
165- // register keep alive handler
166- session . KeepAlive += Client_KeepAlive ;
183+ // Log("" + session.KeepAliveInterval); // default is 5000
184+ session . KeepAliveInterval = System . Convert . ToInt32 ( OPCUA_conn . timeoutMs ) ;
185+
186+ // register keep alive handler
187+ session . KeepAlive += Client_KeepAlive ;
188+ }
189+ catch ( Exception e )
190+ {
191+ Log ( conn_name + " - WARN: " + e . Message ) ;
192+ }
193+
194+ if ( session == null )
195+ {
196+ Log ( conn_name + " - " + "FATAL: error creating session!" , LogLevelNoLog ) ;
197+ Environment . Exit ( 1 ) ;
198+ }
167199
168200 Log ( conn_name + " - " + "Browsing the OPC UA server namespace." ) ;
169201 exitCode = ExitCode . ErrorBrowseNamespace ;
@@ -204,6 +236,9 @@ private async Task ConsoleClient()
204236 }
205237 private async Task FindObjects ( Opc . Ua . Client . Session session , NodeId nodeid )
206238 {
239+ if ( session == null )
240+ return ;
241+
207242 try
208243 {
209244 ReferenceDescriptionCollection references ;
0 commit comments