.NET Based OPC UA Client/Server SDK  3.1.0.500
Connect – Advanced Connect

Table of Contents

Description

The following dialog shows an advanced implementation of the Connect service. Press the button “Show Code” to display the corresponding code, and the button “Help” to show this documentation page.

Note
This example demonstrates how to implement the Connect service. For this purpose, a new session with the server is opened. The settings performed using this dialog window do not affect the other Getting Started Examples.
clienttutorials_advanced_connect.png

As apposed to the Simple Connect example, where the connection is established automatically, this example requires user interaction.

At “Server URL”, the server to connect to has to be specified. By default, this is the Unified Automation .NET Demo Server running on localhost.

Click on “Get Endpoints” to request a list of available Endpoints from the server. Check the box at “Use Asynchronous Pattern” to call BeginGetEndpoints instead of GetEndpoints.

The server’s response is then displayed as a table at “Get Endpoints Response” (see screenshot below).

clienttutorials_advanced_connect2.png

Double-click on an entry to display more detailed information for a certain Endpoint.

clienttutorials_advanced_connect3.png

Select an Endpoint from the list and click on “Connect” to establish a connection. When choosing a secure endpoint, it is necessary that client and server trust each other’s certificate first (as described in in the simple connect example).

If successful, the displayed connection state will switch to “Connected”. Check the box at “Use Asynchronous Pattern” to call BeginConnect instead of Connect.

clienttutorials_advanced_connect4.png

Sample Code

You can find sample code for calling GetEndpoints here.

The following code shows how to use a returned Endpoint for the synchronous Connect method.

m_session.Connect(SelectedEndpoint, m_session.DefaultRequestSettings);

The following code shows how to use a returned Endpoint for the asynchronous Connect method. You can call all other asynchronous methods provided by the Client SDK similarly.

A callback (OnConnectCompleted) and user data (session) are passed to BeginConnect. The callback casts the user data and calls End Connect.

private void BeginConnect()
{
try
{
m_session.BeginConnect(
SelectedEndpoint,
m_session.DefaultRequestSettings,
OnConnectCompleted,
m_session);
}
catch (Exception e)
{
ExceptionDlg.ShowInnerException(this.Text, e);
}
}
private void OnConnectCompleted(IAsyncResult result)
{
// need to make sure the results are processed on the correct thread.
if (InvokeRequired)
{
BeginInvoke(new AsyncCallback(OnConnectCompleted), result);
return;
}
// get the session used to send the request which was passed as the userData in the Begin call.
Session session = (Session)result.AsyncState;
try
{
session.EndConnect(result);
}
catch (Exception e)
{
ExceptionDlg.ShowInnerException(this.Text, e);
}
}