OPC Studio User's Guide and Reference
DisposableLockConnection Method
Example 



OpcLabs.EasyOpcUA Assembly > OpcLabs.EasyOpc.UA.Services.Extensions Namespace > IEasyUAClientConnectionControlExtension Class : DisposableLockConnection Method
The client connection control object that will perform the operation.

This is typically obtained by calling System.IServiceProvider.GetService(System.Type) on the OpcLabs.EasyOpc.UA.IEasyUAClient (EasyUAClient object) with the type of OpcLabs.EasyOpc.UA.Services.IEasyUAClientConnectionControl as an argument.

The value of this parameter cannot be null (Nothing in Visual Basic).

The OPC UA endpoint descriptor of the connection ot be locked.

Because the OpcLabs.EasyOpc.UA.UAEndpointDescriptor has implicit conversions from System.String and System.Uri, in languages that support implicit conversion operators (such as C# or VB.NET), you can simply use a string (representing the endpoint URL, or a so-called OPC UA endpoint descriptor string), or a System.Uri object, in place of this parameter, and the corresponding endpoint descriptor will be constructed automatically. When the implicit conversion operators are not supported (such as with Python.NET), you can use the OpcLabs.EasyOpc.UA.UAEndpointDescriptor.FromString or OpcLabs.EasyOpc.UA.UAEndpointDescriptor.FromUri static method instead.

Also, because the OpcLabs.EasyOpc.UA.Discovery.UAApplicationElement and OpcLabs.EasyOpc.UA.Discovery.UADiscoveryElement have an implicit conversion to OpcLabs.EasyOpc.UA.UAEndpointDescriptor, in languages that support implicit conversion operators (such as C# or VB.NET), you can simply use a OpcLabs.EasyOpc.UA.Discovery.UAApplicationElement or OpcLabs.EasyOpc.UA.Discovery.UADiscoveryElement (results from OPC UA discovery) in place of this parameter, and the corresponding endpoint descriptor will be constructed automatically. When the implicit conversion operators are not supported (such as with Python.NET), you can convert a (non-null) OpcLabs.EasyOpc.UA.Discovery.UAApplicationElement or OpcLabs.EasyOpc.UA.Discovery.UADiscoveryElement to OpcLabs.EasyOpc.UA.UAEndpointDescriptor using the OpcLabs.EasyOpc.UA.Discovery.UAApplicationElement.ToUAEndpointDescriptor or OpcLabs.EasyOpc.UA.Discovery.UADiscoveryElement.ToUAEndpointDescriptor method instead.

If you are using OPC Wizard (for server development), an implicit conversion from OpcLabs.EasyOpc.UA.EasyUAServerCore can be used in the same way to simply pass the server object in place of this parameter, which will use its OpcLabs.EasyOpc.UA.EasyUAServerCore.EffectiveServerDescriptor property for the connection.

The value of this parameter cannot be null (Nothing in Visual Basic).

Locks a connection to the specified endpoint descriptor and returns an object that unlocks the connection upon its disposal.
Syntax
'Declaration
 
<ExtensionAttribute()>
<NotNullAttribute()>
Public Shared Function DisposableLockConnection( _
   ByVal clientConnectionControl As IEasyUAClientConnectionControl, _
   ByVal endpointDescriptor As UAEndpointDescriptor _
) As IDisposable
'Usage
 
Dim clientConnectionControl As IEasyUAClientConnectionControl
Dim endpointDescriptor As UAEndpointDescriptor
Dim value As IDisposable
 
value = IEasyUAClientConnectionControlExtension.DisposableLockConnection(clientConnectionControl, endpointDescriptor)

Parameters

clientConnectionControl
The client connection control object that will perform the operation.

This is typically obtained by calling System.IServiceProvider.GetService(System.Type) on the OpcLabs.EasyOpc.UA.IEasyUAClient (EasyUAClient object) with the type of OpcLabs.EasyOpc.UA.Services.IEasyUAClientConnectionControl as an argument.

The value of this parameter cannot be null (Nothing in Visual Basic).

endpointDescriptor
The OPC UA endpoint descriptor of the connection ot be locked.

Because the OpcLabs.EasyOpc.UA.UAEndpointDescriptor has implicit conversions from System.String and System.Uri, in languages that support implicit conversion operators (such as C# or VB.NET), you can simply use a string (representing the endpoint URL, or a so-called OPC UA endpoint descriptor string), or a System.Uri object, in place of this parameter, and the corresponding endpoint descriptor will be constructed automatically. When the implicit conversion operators are not supported (such as with Python.NET), you can use the OpcLabs.EasyOpc.UA.UAEndpointDescriptor.FromString or OpcLabs.EasyOpc.UA.UAEndpointDescriptor.FromUri static method instead.

Also, because the OpcLabs.EasyOpc.UA.Discovery.UAApplicationElement and OpcLabs.EasyOpc.UA.Discovery.UADiscoveryElement have an implicit conversion to OpcLabs.EasyOpc.UA.UAEndpointDescriptor, in languages that support implicit conversion operators (such as C# or VB.NET), you can simply use a OpcLabs.EasyOpc.UA.Discovery.UAApplicationElement or OpcLabs.EasyOpc.UA.Discovery.UADiscoveryElement (results from OPC UA discovery) in place of this parameter, and the corresponding endpoint descriptor will be constructed automatically. When the implicit conversion operators are not supported (such as with Python.NET), you can convert a (non-null) OpcLabs.EasyOpc.UA.Discovery.UAApplicationElement or OpcLabs.EasyOpc.UA.Discovery.UADiscoveryElement to OpcLabs.EasyOpc.UA.UAEndpointDescriptor using the OpcLabs.EasyOpc.UA.Discovery.UAApplicationElement.ToUAEndpointDescriptor or OpcLabs.EasyOpc.UA.Discovery.UADiscoveryElement.ToUAEndpointDescriptor method instead.

If you are using OPC Wizard (for server development), an implicit conversion from OpcLabs.EasyOpc.UA.EasyUAServerCore can be used in the same way to simply pass the server object in place of this parameter, which will use its OpcLabs.EasyOpc.UA.EasyUAServerCore.EffectiveServerDescriptor property for the connection.

The value of this parameter cannot be null (Nothing in Visual Basic).

Return Value

Returns a disposable object which, when disposed, unlocks the connection.

This method never returns null (Nothing in Visual Basic).

Exceptions
ExceptionDescription

A null reference (Nothing in Visual Basic) is passed to a method that does not accept it as a valid argument.

This is a usage error, i.e. it will never occur (the exception will not be thrown) in a correctly written program. Your code should not catch this exception.

Remarks

This is an extension method (info: C#, VB.NET). In languages that have support for extensions methods (such as C# and VB.NET), you can use the extension method as if it were a regular method on the object that is its first parameter. In other languages (such as with Python.NET), you will call the extension as a static method, and pass it the object on which it acts as its first parameter.

Example
// This example shows how to obtain lock the OPC UA connection and obtain a disposable object which unlocks the connection
// when it is disposed.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
// OPC client and subscriber examples in C# on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-CSharp .
// Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
// a commercial license in order to use Online Forums, and we reply to every post.

using System;
using Microsoft.Extensions.DependencyInjection;
using OpcLabs.EasyOpc.UA;
using OpcLabs.EasyOpc.UA.OperationModel;
using OpcLabs.EasyOpc.UA.Services;
using OpcLabs.EasyOpc.UA.Services.Extensions;

namespace UADocExamples._EasyUAClientConnectionControl
{
    class DisposableLockConnection
    {
        public static void Main1()
        {
            UAEndpointDescriptor endpointDescriptor =
                "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer";
            // or "http://opcua.demo-this.com:51211/UA/SampleServer" (currently not supported)
            // or "https://opcua.demo-this.com:51212/UA/SampleServer/"

            // Instantiate the client object and hook events
            using (var client = new EasyUAClient())
            {
                // Obtain the client connection monitoring service.
                IEasyUAClientConnectionMonitoring clientConnectionMonitoring = client.GetService<IEasyUAClientConnectionMonitoring>();
                if (clientConnectionMonitoring is null)
                {
                    Console.WriteLine("The client connection monitoring service is not available.");
                    return;
                }

                // Obtain the client connection control service.
                IEasyUAClientConnectionControl clientConnectionControl = client.GetService<IEasyUAClientConnectionControl>();
                if (clientConnectionControl is null)
                {
                    Console.WriteLine("The client connection control service is not available.");
                    return;
                }

                // Display the server condition changed events.
                clientConnectionMonitoring.ServerConditionChanged += (sender, args) => Console.WriteLine(args);

                try
                {
                    Console.WriteLine("Reading (1)");
                    // The first read will cause a connection to the server.
                    UAAttributeData attributeData1 =
                        client.Read(endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10853");
                    Console.WriteLine(attributeData1);

                    Console.WriteLine("Waiting for 10 seconds...");
                    // Since the connection is now not used for some time, and it is not locked, it will be closed.
                    System.Threading.Thread.Sleep(10 * 1000);

                    Console.WriteLine("Locking");
                    // Locking the connection causes it to open, if possible.
                    using (clientConnectionControl.DisposableLockConnection(endpointDescriptor))
                    {
                        Console.WriteLine("Waiting for 10 seconds...");
                        // The connection is locked, it will not be closed now.
                        System.Threading.Thread.Sleep(10 * 1000);

                        Console.WriteLine("Reading (2)");
                        UAAttributeData attributeData2 =
                            client.Read(endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10853");
                        Console.WriteLine(attributeData2);

                        Console.WriteLine("Waiting for 10 seconds...");
                        // The connection is still locked, it will not be closed now.
                        System.Threading.Thread.Sleep(10 * 1000);

                        // Unlocking will occur now.
                    }
                    Console.WriteLine("Unlocked");
                }
                catch (UAException uaException)
                {
                    Console.WriteLine("*** Failure: {0}", uaException.GetBaseException().Message);
                }

                Console.WriteLine("Waiting for 10 seconds...");
                // After some delay, the connection will be closed, because there are no subscriptions to the server and no
                // connection locks.
                System.Threading.Thread.Sleep(10 * 1000);

                Console.WriteLine("Finished.");
            }
        }


        // Example output:
        //
        //Reading (1)
        //"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" Connecting; Success; Attempt #1
        //"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" Connected; Success
        //-1.034588E+18 {Single} @2021-11-15T15:26:39.169 @@2021-11-15T15:26:39.169; Good
        //Waiting for 10 seconds...
        //"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" Disconnecting; Success
        //"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" Disconnected(RetrialDelay=Infinite); Success
        //Locking
        //"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" Connecting; Success; Attempt #1
        //"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" Connected; Success
        //Waiting for 10 seconds...
        //Reading (2)
        //2.288872E+21 {Single} @2021-11-15T15:26:59.836 @@2021-11-15T15:26:59.836; Good
        //Waiting for 10 seconds...
        //Unlocked
        //Waiting for 10 seconds...
        //"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" Disconnecting; Success
        //"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" Disconnected(RetrialDelay=Infinite); Success
        //Finished.
    }
}
' This example shows how to obtain lock the OPC UA connection and obtain a disposable object which unlocks the connection
' when it is disposed.
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
' OPC client and subscriber examples in VB.NET on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-VBNET .
' Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
' a commercial license in order to use Online Forums, and we reply to every post.

Imports Microsoft.Extensions.DependencyInjection
Imports OpcLabs.EasyOpc.UA
Imports OpcLabs.EasyOpc.UA.OperationModel
Imports OpcLabs.EasyOpc.UA.Services
Imports OpcLabs.EasyOpc.UA.Services.Extensions

Namespace _EasyUAClientConnectionControl
    Partial Friend Class DisposableLockConnection
        Public Shared Sub Main1()
            Dim endpointDescriptor As UAEndpointDescriptor =
                    "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer"
            ' or "http://opcua.demo-this.com:51211/UA/SampleServer" (currently not supported)
            ' or "https://opcua.demo-this.com:51212/UA/SampleServer/"

            ' Instantiate the client object and hook events
            Using client = New EasyUAClient()
                ' Obtain the client connection monitoring service.
                Dim clientConnectionMonitoring As IEasyUAClientConnectionMonitoring = client.GetService(Of IEasyUAClientConnectionMonitoring)
                If clientConnectionMonitoring Is Nothing Then
                    Console.WriteLine("The client connection monitoring service is not available.")
                    Exit Sub
                End If

                ' Obtain the client connection control service.
                Dim clientConnectionControl As IEasyUAClientConnectionControl = client.GetService(Of IEasyUAClientConnectionControl)
                If clientConnectionControl Is Nothing Then
                    Console.WriteLine("The client connection control service is not available.")
                    Exit Sub
                End If

                ' Display the server condition changed events.
                AddHandler clientConnectionMonitoring.ServerConditionChanged, Sub(sender, args)
                                                                                  Console.WriteLine(args)
                                                                              End Sub
                Try
                    Console.WriteLine("Reading (1)")
                    ' The first read will cause a connection to the server.
                    Dim attributeData1 As UAAttributeData =
                        client.Read(endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10853")
                    Console.WriteLine(attributeData1)

                    Console.WriteLine("Waiting for 10 seconds...")
                    ' Since the connection is now not used for some time, and it is not locked, it will be closed.
                    System.Threading.Thread.Sleep(10 * 1000)

                    Console.WriteLine("Locking")
                    ' Locking the connection causes it to open, if possible.

                    Using clientConnectionControl.DisposableLockConnection(endpointDescriptor)
                        Console.WriteLine("Waiting for 10 seconds...")
                        ' The connection is locked, it will Not be closed now.
                        System.Threading.Thread.Sleep(10 * 1000)

                        Console.WriteLine("Reading (2)")
                        Dim attributeData2 As UAAttributeData =
                            client.Read(endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10853")
                        Console.WriteLine(attributeData2)

                        Console.WriteLine("Waiting for 10 seconds...")
                        ' The connection is still locked, it will not be closed now.
                        System.Threading.Thread.Sleep(10 * 1000)

                        ' Unlocking will occur now.
                    End Using
                    Console.WriteLine("Unlocked")
                Catch uaException As UAException
                    Console.WriteLine("*** Failure: {0}", uaException.GetBaseException().Message)
                End Try

                Console.WriteLine("Waiting for 10 seconds...")
                ' After some delay, the connection will be closed, because there are no subscriptions to the server and no
                ' connection locks.
                System.Threading.Thread.Sleep(10 * 1000)

                Console.WriteLine("Finished.")
            End Using
        End Sub

        ' Example output
        '
        'Reading (1)
        '"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" Connecting; Success; Attempt #1
        '"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" Connected; Success
        '-1.034588E+18 {Single} @2021-11-15T15:26:39.169 @@2021-11-15T15:26:39.169; Good
        'Waiting for 10 seconds...
        '"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" Disconnecting; Success
        '"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" Disconnected(RetrialDelay=Infinite); Success
        'Locking
        '"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" Connecting; Success; Attempt #1
        '"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" Connected; Success
        'Waiting for 10 seconds...
        'Reading (2)
        '2.288872E+21 {Single} @2021-11-15T15:26:59.836 @@2021-11-15T15:26:59.836; Good
        'Waiting for 10 seconds...
        'Unlocked
        'Waiting for 10 seconds...
        '"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" Disconnecting; Success
        '"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" Disconnected(RetrialDelay=Infinite); Success
        'Finished.
    End Class
End Namespace
Requirements

Target Platforms: .NET Framework: Windows 10 (selected versions), Windows 11 (selected versions), Windows Server 2016, Windows Server 2022; .NET: Linux, macOS, Microsoft Windows

See Also