Disconnecting client failure

Questions regarding the use of the .NET SDK 2.0 for Server or Client development or integration into customer products ...

Moderator: uasdknet

Post Reply
marsu
Jr. Member
Jr. Member
Posts: 1
Joined: 08 May 2020, 06:54

Disconnecting client failure

Post by marsu »

I have a C# program that communicates with KepServer.
It's working fine but the disconnect feature does not want to work.

I want the code to work so that when KepServer Runtime is booted, OPC client disconnects and tries to reconnect.
Currently code is made so that when it recognizes connection status change to Disconnected, ServerShutdownInProgress, ServerShutdown or ConnectionErrorClientReconnect it tries to disconnect like this: this.session.Disconnect(SubscriptionCleanupPolicy.Delete);
But nothing happens and after a while I get a timeout from this method. I also tried just Disconnect(); and I tried to fiddle with ReconnectDelay but the results are the same. In some rare debugging cases I somehow managed to reconnect the client, but not consistently and I cannot reproduce it anymore.

What am I doing wrong and why the Disconnect is not working? What would be the correct way to work in this kind of situation?

junhoyoun
Full Member
Full Member
Posts: 6
Joined: 21 Jul 2022, 01:58

Re: Disconnecting client failure

Post by junhoyoun »

Hi,

This might be too old or too late for the reply, however, I leave my workaround for developers seeking any thread.
I had the same problem as described by "marsu", and was suspicious about being related with the ui-thread of winform.
The reason why I thought it might be related with ui-thread was I found Session.Disconnect call had no problem in ConsoleClient sample code.

So, I tested to call Session.Disconnect in program.cs main thread rather than winform codes.
It was enough for me because my application only needs to close session when the client program exits.

I changed the 'Run' method of sample code - which is called in static void Main().

===> changed codes go!

[STAThread]
static void Run(object userState)
{
var form = new Form1();
System.Windows.Forms.Application.Run(form);

// Session property in Form1 class is declared as "public"
if(form.Session != null)
{
if(form.Session.ConnectionStatus != UnifiedAutomation.UaClient.ServerConnectionStatus.Disconnected)
{
form.Session.Disconnect();
}
}
}

junhoyoun
Full Member
Full Member
Posts: 6
Joined: 21 Jul 2022, 01:58

Re: Disconnecting client failure

Post by junhoyoun »

It looks more confident that being related with ui-thread.
I've written code like below and has no issues.

private async void btnDisconnect_Click(object sender, EventArgs e)
{
Output("Disconnecting...");

if (m_session != null && m_session.ConnectionStatus != ServerConnectionStatus.Disconnected)
{
try
{
var t = Task.Run(() => Session_Disconnect(m_session));
await t.ConfigureAwait(false);
}
catch(Exception ex)
{
Debug.WriteLine(ex.Message);
}
}

Output("Disconnected");
}

private void Session_Disconnect(Session session)
{
session.Disconnect(SubscriptionCleanupPolicy.Delete, session.DefaultRequestSettings);
}

User avatar
Support Team
Hero Member
Hero Member
Posts: 3064
Joined: 18 Mar 2011, 15:09

Re: Disconnecting client failure

Post by Support Team »

Hi,

the OPC UA has defined some shutdown event that the server must send to all it's connected clients when (gracefully) going down. The client will receive this shutdown event, which includes the "seconds till shutdown". Now big question is, what to do?
a) immediately disconnect b) wait until short before happening c) nothing, just ignore

Assuming the received "seconds to shutdown" is 60sec, your client immediately disconnects and enters in reconnect loop (which is 5 sec).
In that case you return latest after 6 seconds on the server, but it was not jet down? Guess what, the server again sends you "seconds till shutdown", but now only 54 sec, you again imediately shut down, and 5 sec later reconnect, ...

Best you can do is letting the SDK handle the issue and you not interfere with the reconnect logic implemented by the SDK.
Best regards
Unified Automation Support Team

Post Reply