diff --git a/CoreAgent.Domain/Contexts/CellularNetworkContext.cs b/CoreAgent.Domain/Contexts/CellularNetworkContext.cs
index 5a53d16..2dc67c4 100644
--- a/CoreAgent.Domain/Contexts/CellularNetworkContext.cs
+++ b/CoreAgent.Domain/Contexts/CellularNetworkContext.cs
@@ -24,6 +24,11 @@ public class CellularNetworkContext : ICellularNetworkContext, IDisposable
///
public CancellationTokenSource TokenSource => _token;
+ ///
+ /// 是否已初始化
+ ///
+ public bool IsInitialized => _isInitialized;
+
public CellularNetworkContext(
IOptions networkCommandConfig,
IOptions appSettings)
diff --git a/CoreAgent.Domain/Interfaces/Network/ICellularNetworkContext.cs b/CoreAgent.Domain/Interfaces/Network/ICellularNetworkContext.cs
index c33dbdc..333121a 100644
--- a/CoreAgent.Domain/Interfaces/Network/ICellularNetworkContext.cs
+++ b/CoreAgent.Domain/Interfaces/Network/ICellularNetworkContext.cs
@@ -13,6 +13,11 @@ public interface ICellularNetworkContext
///
CancellationTokenSource TokenSource { get; }
+ ///
+ /// 是否已初始化
+ ///
+ bool IsInitialized { get; }
+
///
/// 初始化上下文
///
diff --git a/CoreAgent.Infrastructure/Services/Network/CellularNetworkService.cs b/CoreAgent.Infrastructure/Services/Network/CellularNetworkService.cs
index 3790c15..8996b9a 100644
--- a/CoreAgent.Infrastructure/Services/Network/CellularNetworkService.cs
+++ b/CoreAgent.Infrastructure/Services/Network/CellularNetworkService.cs
@@ -40,8 +40,14 @@ public class CellularNetworkService : ICellularNetworkService
_context = context ?? throw new ArgumentNullException(nameof(context));
}
+ ///
+ /// 启动蜂窝网络
+ ///
+ /// 网络配置键
+ /// 启动结果
public async Task StartAsync(string key)
{
+ // 1. 参数验证
if (string.IsNullOrEmpty(key))
{
_logger.LogError("启动蜂窝网络失败:配置键为空");
@@ -50,21 +56,29 @@ public class CellularNetworkService : ICellularNetworkService
try
{
+ // 2. 获取启动锁,防止并发启动
if (!await _startLock.WaitAsync(TimeSpan.FromSeconds(LockTimeoutSeconds)))
{
_logger.LogWarning("蜂窝网络启动操作被锁定,可能已有其他启动操作正在进行");
return CellularNetworkOperationResult.Failure("蜂窝网络启动操作被锁定,可能已有其他启动操作正在进行");
}
- // 检查网络状态
- var stateCheckResult = CheckNetworkState(key);
- if (!stateCheckResult.IsSuccess)
+ // 3. 状态检查
+ // 如果是第二次启动(已初始化),需要先检查网络状态
+ if (_context.IsInitialized)
{
- return stateCheckResult;
+ // 检查网络状态,确保没有冲突的配置
+ var stateCheckResult = CheckNetworkState(key);
+ if (!stateCheckResult.IsSuccess)
+ {
+ return stateCheckResult;
+ }
}
- // 初始化网络上下文
+ // 4. 初始化网络上下文
_context.Initialize(key);
+
+ // 5. 启动网络
var result = await StartNetworkAsync(key);
if (!result.IsSuccess)
{