Skip to content

Commit 6cce6c8

Browse files
committed
Adjust server applications
1 parent 7dfa024 commit 6cce6c8

File tree

3 files changed

+45
-30
lines changed

3 files changed

+45
-30
lines changed

Applications/ConsoleReferenceServer/Program.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@
3232
using System.IO;
3333
using System.Threading;
3434
using System.Threading.Tasks;
35+
using Microsoft.Extensions.DependencyInjection;
3536
using Microsoft.Extensions.Logging;
3637
using Opc.Ua;
38+
using Opc.Ua.Configuration;
39+
using Opc.Ua.Configuration.Extensions.DependencyInjection;
40+
using Opc.Ua.Server.Extensions.DependencyInjection;
3741

3842
namespace Quickstarts.ReferenceServer
3943
{
@@ -81,6 +85,13 @@ public static async Task<int> Main(string[] args)
8185
{ "ctt", "CTT mode, use to preset alarms for CTT testing.", c => cttMode = c != null },
8286
};
8387

88+
IServiceCollection services = new ServiceCollection()
89+
.AddConfigurationServices()
90+
.AddServerServices()
91+
.AddScoped<IReferenceServer, ReferenceServer>();
92+
93+
IServiceProvider serviceProvider = services.BuildServiceProvider();
94+
8495
try
8596
{
8697
// parse command line and set options
@@ -92,7 +103,10 @@ public static async Task<int> Main(string[] args)
92103
}
93104

94105
// create the UA server
95-
var server = new UAServer<ReferenceServer>(output) {
106+
var server = new UAServer<IReferenceServer>(
107+
serviceProvider.GetRequiredService<IApplicationInstance>(),
108+
serviceProvider.GetRequiredService<IReferenceServer>(),
109+
output) {
96110
AutoAccept = autoAccept,
97111
Password = password
98112
};

Applications/ConsoleReferenceServer/UAServer.cs

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
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
@@ -11,7 +11,7 @@
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,
@@ -36,12 +36,29 @@
3636
using Opc.Ua;
3737
using Opc.Ua.Configuration;
3838
using Opc.Ua.Server;
39+
using static System.Net.Mime.MediaTypeNames;
3940

4041
namespace 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

Applications/Quickstarts.Servers/Utils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static class Utils
4747
/// Applies custom settings to quickstart servers for CTT run.
4848
/// </summary>
4949
/// <param name="server"></param>
50-
public static void ApplyCTTMode(TextWriter output, StandardServer server)
50+
public static void ApplyCTTMode(TextWriter output, IStandardServer server)
5151
{
5252
var methodsToCall = new CallMethodRequestCollection();
5353
var index = server.CurrentInstance.NamespaceUris.GetIndex(Alarms.Namespaces.Alarms);

0 commit comments

Comments
 (0)