fix: 使用 CommunityToolkit.Mvvm
This commit is contained in:
@@ -52,20 +52,29 @@ public sealed class GrpcService : IPrintBackendClient
|
||||
throw new ArgumentException("API Key 不能为空", nameof(apiKey));
|
||||
}
|
||||
|
||||
_apiKey = apiKey;
|
||||
if (address.StartsWith("http://", StringComparison.OrdinalIgnoreCase) && AllowInsecureGrpc)
|
||||
{
|
||||
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
|
||||
}
|
||||
|
||||
_channel?.Dispose();
|
||||
_channel = GrpcChannel.ForAddress(address);
|
||||
_printClient = new Cloudprint.PrintService.PrintServiceClient(_channel);
|
||||
_libraryClient = new Cloudprint.LibraryService.LibraryServiceClient(_channel);
|
||||
_fileClient = new Cloudprint.FileService.FileServiceClient(_channel);
|
||||
_adminClient = new Cloudprint.AdminService.AdminServiceClient(_channel);
|
||||
IsConnected = true;
|
||||
Log(AppLogLevel.Success, $"已连接 gRPC 服务:{address}");
|
||||
ResetConnectionState(clearCredentials: true);
|
||||
|
||||
try
|
||||
{
|
||||
_apiKey = apiKey;
|
||||
_channel = GrpcChannel.ForAddress(address);
|
||||
_printClient = new Cloudprint.PrintService.PrintServiceClient(_channel);
|
||||
_libraryClient = new Cloudprint.LibraryService.LibraryServiceClient(_channel);
|
||||
_fileClient = new Cloudprint.FileService.FileServiceClient(_channel);
|
||||
_adminClient = new Cloudprint.AdminService.AdminServiceClient(_channel);
|
||||
IsConnected = true;
|
||||
Log(AppLogLevel.Success, $"已连接 gRPC 服务:{address}");
|
||||
}
|
||||
catch
|
||||
{
|
||||
ResetConnectionState(clearCredentials: true);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<AdminSession> LoginAdminAsync(string username, string password, CancellationToken cancellationToken = default)
|
||||
@@ -240,14 +249,7 @@ public sealed class GrpcService : IPrintBackendClient
|
||||
|
||||
public void Disconnect()
|
||||
{
|
||||
_subscriptionCts?.Cancel();
|
||||
IsConnected = false;
|
||||
_channel?.Dispose();
|
||||
_channel = null;
|
||||
_printClient = null;
|
||||
_libraryClient = null;
|
||||
_fileClient = null;
|
||||
_adminClient = null;
|
||||
ResetConnectionState(clearCredentials: true);
|
||||
Log(AppLogLevel.Warning, "已断开 gRPC 连接");
|
||||
}
|
||||
|
||||
@@ -283,7 +285,28 @@ public sealed class GrpcService : IPrintBackendClient
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log(AppLogLevel.Error, $"打印任务订阅中断:{ex.Message}");
|
||||
IsConnected = false;
|
||||
ResetConnectionState(clearCredentials: false);
|
||||
}
|
||||
}
|
||||
|
||||
private void ResetConnectionState(bool clearCredentials)
|
||||
{
|
||||
_subscriptionCts?.Cancel();
|
||||
_subscriptionCts?.Dispose();
|
||||
_subscriptionCts = null;
|
||||
_channel?.Dispose();
|
||||
_channel = null;
|
||||
_printClient = null;
|
||||
_libraryClient = null;
|
||||
_fileClient = null;
|
||||
_adminClient = null;
|
||||
IsConnected = false;
|
||||
PrinterId = 0;
|
||||
Token = string.Empty;
|
||||
|
||||
if (clearCredentials)
|
||||
{
|
||||
_apiKey = string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -359,4 +382,4 @@ public sealed class GrpcService : IPrintBackendClient
|
||||
Message = message
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user