fix: opcua连接状态

This commit is contained in:
2248356998 qq.com
2025-09-24 11:03:23 +08:00
parent ff1f632de2
commit 6d7d0e468a
2 changed files with 16 additions and 9 deletions

View File

@@ -1,9 +1,9 @@
<Project> <Project>
<PropertyGroup> <PropertyGroup>
<PluginVersion>10.11.67</PluginVersion> <PluginVersion>10.11.68</PluginVersion>
<ProPluginVersion>10.11.67</ProPluginVersion> <ProPluginVersion>10.11.68</ProPluginVersion>
<DefaultVersion>10.11.67</DefaultVersion> <DefaultVersion>10.11.68</DefaultVersion>
<AuthenticationVersion>10.11.6</AuthenticationVersion> <AuthenticationVersion>10.11.6</AuthenticationVersion>
<SourceGeneratorVersion>10.11.6</SourceGeneratorVersion> <SourceGeneratorVersion>10.11.6</SourceGeneratorVersion>
<NET8Version>8.0.20</NET8Version> <NET8Version>8.0.20</NET8Version>

View File

@@ -212,10 +212,13 @@ public class OpcUaMaster : IAsyncDisposable
/// </summary> /// </summary>
public ApplicationConfiguration AppConfig => m_configuration; public ApplicationConfiguration AppConfig => m_configuration;
/// <summary> /// <summary>
/// 连接状态 /// 连接状态
/// </summary> /// </summary>
public bool Connected => m_session?.Connected == true; public bool Connected => m_session?.Connected == true && connected;
private bool connected = false;
/// <summary> /// <summary>
/// OpcUaMaster /// OpcUaMaster
@@ -403,11 +406,7 @@ public class OpcUaMaster : IAsyncDisposable
public async Task DisconnectAsync() public async Task DisconnectAsync()
{ {
await PrivateDisconnectAsync().ConfigureAwait(false); await PrivateDisconnectAsync().ConfigureAwait(false);
// disconnect any existing session.
if (m_session != null)
{
m_session = null;
}
} }
public async ValueTask DisposeAsync() public async ValueTask DisposeAsync()
@@ -937,6 +936,7 @@ public class OpcUaMaster : IAsyncDisposable
userIdentity, userIdentity,
null, cancellationToken null, cancellationToken
).ConfigureAwait(false); ).ConfigureAwait(false);
connected = true;
m_session.KeepAliveInterval = OpcUaProperty.KeepAliveInterval == 0 ? 60000 : OpcUaProperty.KeepAliveInterval; m_session.KeepAliveInterval = OpcUaProperty.KeepAliveInterval == 0 ? 60000 : OpcUaProperty.KeepAliveInterval;
m_session.KeepAlive += Session_KeepAlive; m_session.KeepAlive += Session_KeepAlive;
@@ -958,6 +958,7 @@ public class OpcUaMaster : IAsyncDisposable
{ {
bool state = m_session?.Connected == true; bool state = m_session?.Connected == true;
connected = false;
if (m_reConnectHandler != null) if (m_reConnectHandler != null)
{ {
try { m_reConnectHandler.Dispose(); } catch { } try { m_reConnectHandler.Dispose(); } catch { }
@@ -1353,6 +1354,8 @@ public class OpcUaMaster : IAsyncDisposable
{ {
try try
{ {
Log(2, null, "Reconnected : success");
if (!Object.ReferenceEquals(sender, m_reConnectHandler)) if (!Object.ReferenceEquals(sender, m_reConnectHandler))
{ {
return; return;
@@ -1366,6 +1369,8 @@ public class OpcUaMaster : IAsyncDisposable
{ {
var session = m_session; var session = m_session;
m_session = m_reConnectHandler.Session; m_session = m_reConnectHandler.Session;
connected = true;
Utils.SilentDispose(session); Utils.SilentDispose(session);
} }
} }
@@ -1393,6 +1398,8 @@ public class OpcUaMaster : IAsyncDisposable
if (ServiceResult.IsBad(e.Status)) if (ServiceResult.IsBad(e.Status))
{ {
connected = false;
if (m_reConnectHandler == null) if (m_reConnectHandler == null)
{ {
Log(3, null, "Reconnecting : {0}", e.Status.ToString()); Log(3, null, "Reconnecting : {0}", e.Status.ToString());