Skip to content

Commit c326401

Browse files
committed
Implement dependency injection for application instance, server and node managers
1 parent bba6d72 commit c326401

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2756
-697
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* ========================================================================
2+
* Copyright (c) 2005-2020 The OPC Foundation, Inc. All rights reserved.
3+
*
4+
* OPC Foundation MIT License 1.00
5+
*
6+
* Permission is hereby granted, free of charge, to any person
7+
* obtaining a copy of this software and associated documentation
8+
* files (the "Software"), to deal in the Software without
9+
* restriction, including without limitation the rights to use,
10+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the
12+
* Software is furnished to do so, subject to the following
13+
* conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be
16+
* included in all copies or substantial portions of the Software.
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24+
* OTHER DEALINGS IN THE SOFTWARE.
25+
*
26+
* The complete license agreement can be found here:
27+
* http://opcfoundation.org/License/MIT/1.00/
28+
* ======================================================================*/
29+
using System;
30+
using Opc.Ua;
31+
using Opc.Ua.Server;
32+
33+
namespace Quickstarts.ReferenceServer
34+
{
35+
public interface IReferenceServer : IReverseConnectServer
36+
{
37+
ITokenValidator TokenValidator { get; set; }
38+
39+
/// <summary>
40+
/// Creates an instance of the service host.
41+
/// </summary>
42+
ServiceHost CreateServiceHost(ServerBase server, params Uri[] addresses);
43+
}
44+
}

Diff for: Applications/Quickstarts.Servers/ReferenceServer/ReferenceServer.cs

+31-13
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,
@@ -32,7 +32,9 @@
3232
using System.Linq;
3333
using System.Security.Cryptography.X509Certificates;
3434
using Opc.Ua;
35+
using Opc.Ua.Configuration;
3536
using Opc.Ua.Server;
37+
using Opc.Ua.Server.NodeManager;
3638

3739
namespace Quickstarts.ReferenceServer
3840
{
@@ -43,12 +45,23 @@ namespace Quickstarts.ReferenceServer
4345
/// Each server instance must have one instance of a StandardServer object which is
4446
/// responsible for reading the configuration file, creating the endpoints and dispatching
4547
/// incoming requests to the appropriate handler.
46-
///
48+
///
4749
/// This sub-class specifies non-configurable metadata such as Product Name and initializes
4850
/// the EmptyNodeManager which provides access to the data exposed by the Server.
4951
/// </remarks>
50-
public partial class ReferenceServer : ReverseConnectServer
52+
public partial class ReferenceServer : ReverseConnectServer, IReferenceServer
5153
{
54+
public ReferenceServer(
55+
IApplicationInstance applicationInstance,
56+
IServerInternal serverInternal,
57+
IMainNodeManagerFactory mainNodeManagerFactory)
58+
: base(applicationInstance, serverInternal, mainNodeManagerFactory)
59+
{
60+
m_applicationInstance = applicationInstance;
61+
m_serverInternal = serverInternal;
62+
m_mainNodeManagerFactory = mainNodeManagerFactory;
63+
}
64+
5265
#region Properties
5366
public ITokenValidator TokenValidator { get; set; }
5467

@@ -63,7 +76,9 @@ public partial class ReferenceServer : ReverseConnectServer
6376
/// always creates a CoreNodeManager which handles the built-in nodes defined by the specification.
6477
/// Any additional NodeManagers are expected to handle application specific nodes.
6578
/// </remarks>
66-
protected override MasterNodeManager CreateMasterNodeManager(IServerInternal server, ApplicationConfiguration configuration)
79+
protected override IMasterNodeManager CreateMasterNodeManager(
80+
IServerInternal server,
81+
ApplicationConfiguration configuration)
6782
{
6883
Utils.LogInfo(Utils.TraceMasks.StartStop, "Creating the Reference Server Node Manager.");
6984

@@ -78,7 +93,7 @@ protected override MasterNodeManager CreateMasterNodeManager(IServerInternal ser
7893
}
7994

8095
// create master node manager.
81-
return new MasterNodeManager(server, configuration, null, nodeManagers.ToArray());
96+
return m_mainNodeManagerFactory.CreateMasterNodeManager(null, nodeManagers.ToArray());
8297
}
8398

8499
/// <summary>
@@ -127,7 +142,7 @@ protected override ResourceManager CreateResourceManager(IServerInternal server,
127142
/// Initializes the server before it starts up.
128143
/// </summary>
129144
/// <remarks>
130-
/// This method is called before any startup processing occurs. The sub-class may update the
145+
/// This method is called before any startup processing occurs. The sub-class may update the
131146
/// configuration object or do any other application specific startup tasks.
132147
/// </remarks>
133148
protected override void OnServerStarting(ApplicationConfiguration configuration)
@@ -153,11 +168,10 @@ protected override void OnServerStarted(IServerInternal server)
153168

154169
try
155170
{
156-
lock (ServerInternal.Status.Lock)
157-
{
158-
// allow a faster sampling interval for CurrentTime node.
159-
ServerInternal.Status.Variable.CurrentTime.MinimumSamplingInterval = 250;
160-
}
171+
ServerInternal.UpdateServerStatus(
172+
(serverStatus) => {
173+
serverStatus.Variable.CurrentTime.MinimumSamplingInterval = 250;
174+
});
161175
}
162176
catch
163177
{ }
@@ -407,7 +421,7 @@ private IUserIdentity VerifyIssuedToken(IssuedIdentityToken issuedToken)
407421
info = new TranslationInfo("IssuedTokenInvalid", "en-US", "token is an invalid issued token.");
408422
result = StatusCodes.BadIdentityTokenInvalid;
409423
}
410-
else // Rejected
424+
else // Rejected
411425
{
412426
// construct translation object with default text.
413427
info = new TranslationInfo("IssuedTokenRejected", "en-US", "token is rejected.");
@@ -425,6 +439,10 @@ private IUserIdentity VerifyIssuedToken(IssuedIdentityToken issuedToken)
425439

426440
#region Private Fields
427441
private ICertificateValidator m_userCertificateValidator;
442+
private readonly IMainNodeManagerFactory m_mainNodeManagerFactory;
443+
private readonly IServerInternal m_serverInternal;
444+
private readonly IApplicationInstance m_applicationInstance;
445+
428446
#endregion
429447
}
430448
}

Diff for: Libraries/Opc.Ua.Configuration/ApplicationInstance.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ namespace Opc.Ua.Configuration
4444
/// <summary>
4545
/// A class that install, configures and runs a UA application.
4646
/// </summary>
47-
public class ApplicationInstance
47+
public class ApplicationInstance : IApplicationInstance
4848
{
4949
#region Ctors
5050
/// <summary>
@@ -111,7 +111,7 @@ public Type ConfigurationType
111111
/// Gets the server.
112112
/// </summary>
113113
/// <value>The server.</value>
114-
public ServerBase Server => m_server;
114+
public IServerBase Server => m_server;
115115

116116
/// <summary>
117117
/// Gets the application configuration used when the Start() method was called.
@@ -164,7 +164,7 @@ public bool ProcessCommandLine()
164164
/// Starts the UA server as a Windows Service.
165165
/// </summary>
166166
/// <param name="server">The server.</param>
167-
public void StartAsService(ServerBase server)
167+
public void StartAsService(IServerBase server)
168168
{
169169
throw new NotImplementedException(".NetStandard Opc.Ua libraries do not support to start as a windows service");
170170
}
@@ -173,7 +173,7 @@ public void StartAsService(ServerBase server)
173173
/// Starts the UA server.
174174
/// </summary>
175175
/// <param name="server">The server.</param>
176-
public async Task Start(ServerBase server)
176+
public async Task Start(IServerBase server)
177177
{
178178
m_server = server;
179179

@@ -1144,7 +1144,7 @@ private static async Task<bool> ApproveMessageAsync(string message, bool silent)
11441144
private ApplicationType m_applicationType;
11451145
private string m_configSectionName;
11461146
private Type m_configurationType;
1147-
private ServerBase m_server;
1147+
private IServerBase m_server;
11481148
private ApplicationConfiguration m_applicationConfiguration;
11491149
#endregion
11501150
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* ========================================================================
2+
* Copyright (c) 2005-2020 The OPC Foundation, Inc. All rights reserved.
3+
*
4+
* OPC Foundation MIT License 1.00
5+
*
6+
* Permission is hereby granted, free of charge, to any person
7+
* obtaining a copy of this software and associated documentation
8+
* files (the "Software"), to deal in the Software without
9+
* restriction, including without limitation the rights to use,
10+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the
12+
* Software is furnished to do so, subject to the following
13+
* conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be
16+
* included in all copies or substantial portions of the Software.
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24+
* OTHER DEALINGS IN THE SOFTWARE.
25+
*
26+
* The complete license agreement can be found here:
27+
* http://opcfoundation.org/License/MIT/1.00/
28+
* ======================================================================*/
29+
30+
using Microsoft.Extensions.DependencyInjection;
31+
32+
namespace Opc.Ua.Configuration.Extensions.DependencyInjection
33+
{
34+
/// <summary>
35+
/// Class that provides service collection extensions to setup dependency injection
36+
/// for server objects.
37+
/// </summary>
38+
public static class ServiceCollectionExtensions
39+
{
40+
/// <summary>
41+
/// Add all the necessary server services to dependency injection.
42+
/// </summary>
43+
/// <param name="services">The service collection of the application.</param>
44+
/// <returns>The updated service collection.</returns>
45+
public static IServiceCollection AddConfigurationServices(this IServiceCollection services)
46+
{
47+
return services.AddScoped<IApplicationInstance, ApplicationInstance>();
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)