Skip to content

Commit c4b74f7

Browse files
committed
Improve DI configuration
1 parent f2e48fa commit c4b74f7

File tree

3 files changed

+59
-14
lines changed

3 files changed

+59
-14
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using Opc.Ua;
4+
using Opc.Ua.Configuration;
5+
using Opc.Ua.Server;
6+
7+
namespace Quickstarts
8+
{
9+
public interface IUAServer<T> where T : IStandardServer
10+
{
11+
IApplicationInstance Application { get; }
12+
13+
ApplicationConfiguration Configuration { get; }
14+
15+
bool AutoAccept { get; set; }
16+
17+
string Password { get; set; }
18+
19+
ExitCode ExitCode { get; }
20+
21+
T Server { get; }
22+
23+
/// <summary>
24+
/// Load the application configuration.
25+
/// </summary>
26+
Task LoadAsync(string applicationName, string configSectionName);
27+
28+
/// <summary>
29+
/// Load the application configuration.
30+
/// </summary>
31+
Task CheckCertificateAsync(bool renewCertificate);
32+
33+
/// <summary>
34+
/// Create server instance and add node managers.
35+
/// </summary>
36+
void Create(IList<INodeManagerFactory> nodeManagerFactories);
37+
38+
/// <summary>
39+
/// Start the server.
40+
/// </summary>
41+
Task StartAsync();
42+
43+
/// <summary>
44+
/// Stops the server.
45+
/// </summary>
46+
Task StopAsync();
47+
}
48+
}

Applications/ConsoleReferenceServer/Program.cs

Lines changed: 9 additions & 11 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,
@@ -87,7 +87,9 @@ public static async Task<int> Main(string[] args)
8787
IServiceCollection services = new ServiceCollection()
8888
.AddConfigurationServices()
8989
.AddServerServices()
90-
.AddScoped<IReferenceServer, ReferenceServer>();
90+
.AddScoped<IReferenceServer, ReferenceServer>()
91+
.AddScoped(sp => new LogWriter())
92+
.AddScoped<IUAServer<IReferenceServer>, UAServer<IReferenceServer>>();
9193

9294
IServiceProvider serviceProvider = services.BuildServiceProvider();
9395

@@ -98,17 +100,13 @@ public static async Task<int> Main(string[] args)
98100

99101
if (logConsole && appLog)
100102
{
101-
output = new LogWriter();
103+
output = serviceProvider.GetRequiredService<LogWriter>();
102104
}
103105

104106
// create the UA server
105-
var server = new UAServer<IReferenceServer>(
106-
serviceProvider.GetRequiredService<IApplicationInstance>(),
107-
serviceProvider.GetRequiredService<IReferenceServer>(),
108-
output) {
109-
AutoAccept = autoAccept,
110-
Password = password
111-
};
107+
IUAServer<IReferenceServer> server = serviceProvider.GetRequiredService<IUAServer<IReferenceServer>>();
108+
server.AutoAccept = autoAccept;
109+
server.Password = password;
112110

113111
// load the server configuration, validate certificates
114112
output.WriteLine("Loading configuration from {0}.", configSectionName);

Applications/ConsoleReferenceServer/UAServer.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,10 @@
3636
using Opc.Ua;
3737
using Opc.Ua.Configuration;
3838
using Opc.Ua.Server;
39-
using static System.Net.Mime.MediaTypeNames;
4039

4140
namespace Quickstarts
4241
{
43-
public class UAServer<T> where T : IStandardServer
42+
public class UAServer<T> : IUAServer<T> where T : IStandardServer
4443
{
4544
/// <summary>
4645
/// Constructor of the server.
@@ -51,7 +50,7 @@ public class UAServer<T> where T : IStandardServer
5150
public UAServer(
5251
IApplicationInstance applicationInstance,
5352
T server,
54-
TextWriter writer)
53+
LogWriter writer)
5554
{
5655
m_application = applicationInstance;
5756
m_server = server;

0 commit comments

Comments
 (0)