Skip to content

Commit cfb3ac4

Browse files
authored
Support ActivitySource in OPC UA client to enable tracing (.NET6) (#2251)
- Add traceable client session with factory to support ActivitySource on .NET 6 - Only traces the public API calls, inner calls are not yet supported. - Uses the name: Opc.Ua.Client-TraceableSession-ActivitySource
1 parent e6f3004 commit cfb3ac4

File tree

11 files changed

+2274
-22
lines changed

11 files changed

+2274
-22
lines changed

Libraries/Opc.Ua.Client/DefaultSessionFactory.cs

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
using System;
3131
using System.Collections.Generic;
32+
using System.Security.Cryptography.X509Certificates;
3233
using System.Threading;
3334
using System.Threading.Tasks;
3435

@@ -39,9 +40,21 @@ namespace Opc.Ua.Client
3940
/// </summary>
4041
public class DefaultSessionFactory : ISessionFactory
4142
{
43+
/// <summary>
44+
/// The default instance of the factory.
45+
/// </summary>
46+
public static readonly DefaultSessionFactory Instance = new DefaultSessionFactory();
47+
48+
/// <summary>
49+
/// Force use of the default instance.
50+
/// </summary>
51+
protected DefaultSessionFactory()
52+
{
53+
}
54+
4255
#region Public Methods
4356
/// <inheritdoc/>
44-
public async Task<ISession> CreateAsync(
57+
public async virtual Task<ISession> CreateAsync(
4558
ApplicationConfiguration configuration,
4659
ConfiguredEndpoint endpoint,
4760
bool updateBeforeConnect,
@@ -55,7 +68,7 @@ public async Task<ISession> CreateAsync(
5568
}
5669

5770
/// <inheritdoc/>
58-
public async Task<ISession> CreateAsync(
71+
public async virtual Task<ISession> CreateAsync(
5972
ApplicationConfiguration configuration,
6073
ConfiguredEndpoint endpoint,
6174
bool updateBeforeConnect,
@@ -71,7 +84,7 @@ public async Task<ISession> CreateAsync(
7184
}
7285

7386
/// <inheritdoc/>
74-
public async Task<ISession> CreateAsync(
87+
public async virtual Task<ISession> CreateAsync(
7588
ApplicationConfiguration configuration,
7689
ITransportWaitingConnection connection,
7790
ConfiguredEndpoint endpoint,
@@ -89,7 +102,7 @@ public async Task<ISession> CreateAsync(
89102
}
90103

91104
/// <inheritdoc/>
92-
public async Task<ISession> CreateAsync(
105+
public async virtual Task<ISession> CreateAsync(
93106
ApplicationConfiguration configuration,
94107
ReverseConnectManager reverseConnectManager,
95108
ConfiguredEndpoint endpoint,
@@ -102,7 +115,6 @@ public async Task<ISession> CreateAsync(
102115
CancellationToken ct = default
103116
)
104117
{
105-
106118
if (reverseConnectManager == null)
107119
{
108120
return await CreateAsync(configuration, endpoint, updateBeforeConnect,
@@ -141,7 +153,25 @@ await endpoint.UpdateFromServerAsync(
141153
}
142154

143155
/// <inheritdoc/>
144-
public Task<ISession> RecreateAsync(ISession sessionTemplate)
156+
public virtual ISession Create(
157+
ApplicationConfiguration configuration,
158+
ITransportChannel channel,
159+
ConfiguredEndpoint endpoint,
160+
X509Certificate2 clientCertificate,
161+
EndpointDescriptionCollection availableEndpoints = null,
162+
StringCollection discoveryProfileUris = null)
163+
{
164+
return Session.Create(configuration, channel, endpoint, clientCertificate, availableEndpoints, discoveryProfileUris);
165+
}
166+
167+
/// <inheritdoc/>
168+
public virtual Task<ITransportChannel> CreateChannelAsync(ApplicationConfiguration configuration, ITransportWaitingConnection connection, ConfiguredEndpoint endpoint, bool updateBeforeConnect, bool checkDomain)
169+
{
170+
return Session.CreateChannelAsync(configuration, connection, endpoint, updateBeforeConnect, checkDomain);
171+
}
172+
173+
/// <inheritdoc/>
174+
public virtual Task<ISession> RecreateAsync(ISession sessionTemplate)
145175
{
146176
if (!(sessionTemplate is Session template))
147177
{
@@ -152,7 +182,7 @@ public Task<ISession> RecreateAsync(ISession sessionTemplate)
152182
}
153183

154184
/// <inheritdoc/>
155-
public Task<ISession> RecreateAsync(ISession sessionTemplate, ITransportWaitingConnection connection)
185+
public virtual Task<ISession> RecreateAsync(ISession sessionTemplate, ITransportWaitingConnection connection)
156186
{
157187
if (!(sessionTemplate is Session template))
158188
{

Libraries/Opc.Ua.Client/ISessionFactory.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* ======================================================================*/
2929

3030
using System.Collections.Generic;
31+
using System.Security.Cryptography.X509Certificates;
3132
using System.Threading;
3233
using System.Threading.Tasks;
3334

@@ -131,6 +132,39 @@ Task<ISession> CreateAsync(
131132
IList<string> preferredLocales,
132133
CancellationToken ct = default);
133134

135+
/// <summary>
136+
/// Creates a secure channel to the specified endpoint.
137+
/// </summary>
138+
/// <param name="configuration">The application configuration.</param>
139+
/// <param name="connection">The client endpoint for the reverse connect.</param>
140+
/// <param name="endpoint">A configured endpoint to connect to.</param>
141+
/// <param name="updateBeforeConnect">Update configuration based on server prior connect.</param>
142+
/// <param name="checkDomain">Check that the certificate specifies a valid domain (computer) name.</param>
143+
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
144+
Task<ITransportChannel> CreateChannelAsync(
145+
ApplicationConfiguration configuration,
146+
ITransportWaitingConnection connection,
147+
ConfiguredEndpoint endpoint,
148+
bool updateBeforeConnect,
149+
bool checkDomain);
150+
151+
/// <summary>
152+
/// Creates a new session with a server using the specified channel by invoking the CreateSession service.
153+
/// </summary>
154+
/// <param name="configuration">The configuration for the client application.</param>
155+
/// <param name="channel">The channel for the server.</param>
156+
/// <param name="endpoint">The endpoint for the server.</param>
157+
/// <param name="clientCertificate">The certificate to use for the client.</param>
158+
/// <param name="availableEndpoints">The list of available endpoints returned by server in GetEndpoints() response.</param>
159+
/// <param name="discoveryProfileUris">The value of profileUris used in GetEndpoints() request.</param>
160+
ISession Create(
161+
ApplicationConfiguration configuration,
162+
ITransportChannel channel,
163+
ConfiguredEndpoint endpoint,
164+
X509Certificate2 clientCertificate,
165+
EndpointDescriptionCollection availableEndpoints = null,
166+
StringCollection discoveryProfileUris = null);
167+
134168
/// <summary>
135169
/// Recreates a session based on a specified template.
136170
/// </summary>

Libraries/Opc.Ua.Client/Session.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ private void Initialize(
251251
/// </summary>
252252
private void Initialize()
253253
{
254-
m_sessionFactory = new DefaultSessionFactory();
254+
m_sessionFactory = DefaultSessionFactory.Instance;
255255
m_sessionTimeout = 0;
256256
m_namespaceUris = new NamespaceTable();
257257
m_serverUris = new StringTable();

0 commit comments

Comments
 (0)