22 * Copyright (c) 2005-2020 The OPC Foundation, Inc. All rights reserved.
33 *
44 * OPC Foundation MIT License 1.00
5- *
5+ *
66 * Permission is hereby granted, free of charge, to any person
77 * obtaining a copy of this software and associated documentation
88 * files (the "Software"), to deal in the Software without
1111 * copies of the Software, and to permit persons to whom the
1212 * Software is furnished to do so, subject to the following
1313 * conditions:
14- *
14+ *
1515 * The above copyright notice and this permission notice shall be
1616 * included in all copies or substantial portions of the Software.
1717 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
3636using Opc . Ua ;
3737using Opc . Ua . Configuration ;
3838using Opc . Ua . Server ;
39+ using static System . Net . Mime . MediaTypeNames ;
3940
4041namespace Quickstarts
4142{
42- public class UAServer < T > where T : StandardServer , new ( )
43+ public class UAServer < T > where T : IStandardServer
4344 {
44- public ApplicationInstance Application => m_application ;
45+ /// <summary>
46+ /// Constructor of the server.
47+ /// </summary>
48+ /// <param name="applicationInstance">Application instance.</param>
49+ /// <param name="server">Server of the specified generic type.</param>
50+ /// <param name="writer">The text output.</param>
51+ public UAServer (
52+ IApplicationInstance applicationInstance ,
53+ T server ,
54+ TextWriter writer )
55+ {
56+ m_application = applicationInstance ;
57+ m_server = server ;
58+ m_output = writer ;
59+ }
60+
61+ public IApplicationInstance Application => m_application ;
4562 public ApplicationConfiguration Configuration => m_application . ApplicationConfiguration ;
4663
4764 public bool AutoAccept { get ; set ; }
@@ -50,15 +67,6 @@ namespace Quickstarts
5067 public ExitCode ExitCode { get ; private set ; }
5168 public T Server => m_server ;
5269
53- /// <summary>
54- /// Ctor of the server.
55- /// </summary>
56- /// <param name="writer">The text output.</param>
57- public UAServer ( TextWriter writer )
58- {
59- m_output = writer ;
60- }
61-
6270 /// <summary>
6371 /// Load the application configuration.
6472 /// </summary>
@@ -69,13 +77,12 @@ public async Task LoadAsync(string applicationName, string configSectionName)
6977 ExitCode = ExitCode . ErrorNotStarted ;
7078
7179 ApplicationInstance . MessageDlg = new ApplicationMessageDlg ( m_output ) ;
72- CertificatePasswordProvider PasswordProvider = new CertificatePasswordProvider ( Password ) ;
73- m_application = new ApplicationInstance {
74- ApplicationName = applicationName ,
75- ApplicationType = ApplicationType . Server ,
76- ConfigSectionName = configSectionName ,
77- CertificatePasswordProvider = PasswordProvider
78- } ;
80+ var passwordProvider = new CertificatePasswordProvider ( Password ) ;
81+
82+ m_application . ApplicationName = applicationName ;
83+ m_application . ApplicationType = ApplicationType . Server ;
84+ m_application . ConfigSectionName = configSectionName ;
85+ m_application . CertificatePasswordProvider = passwordProvider ;
7986
8087 // load the application configuration.
8188 await m_application . LoadApplicationConfiguration ( false ) . ConfigureAwait ( false ) ;
@@ -125,8 +132,6 @@ public void Create(IList<INodeManagerFactory> nodeManagerFactories)
125132 {
126133 try
127134 {
128- // create the server.
129- m_server = new T ( ) ;
130135 if ( nodeManagerFactories != null )
131136 {
132137 foreach ( var factory in nodeManagerFactories )
@@ -148,9 +153,6 @@ public async Task StartAsync()
148153 {
149154 try
150155 {
151- // create the server.
152- m_server = m_server ?? new T ( ) ;
153-
154156 // start the server
155157 await m_application . Start ( m_server ) . ConfigureAwait ( false ) ;
156158
@@ -190,7 +192,6 @@ public async Task StopAsync()
190192 using ( T server = m_server )
191193 {
192194 // Stop status thread
193- m_server = null ;
194195 await m_status . ConfigureAwait ( false ) ;
195196
196197 // Stop server and dispose
@@ -281,8 +282,8 @@ private async Task StatusThreadAsync()
281282
282283 #region Private Members
283284 private readonly TextWriter m_output ;
284- private ApplicationInstance m_application ;
285- private T m_server ;
285+ private readonly T m_server ;
286+ private readonly IApplicationInstance m_application ;
286287 private Task m_status ;
287288 private DateTime m_lastEventTime ;
288289 #endregion
0 commit comments