<PackageReference Include="Relativity.Transfer.Client" Version="7.2.26" />

ConnectionServiceBase

public abstract class ConnectionServiceBase : IDisposable
using Relativity.Transfer.Resources; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.Threading; using System.Threading.Tasks; namespace Relativity.Transfer { public abstract class ConnectionServiceBase : IDisposable { private readonly ConcurrentDictionary<string, object> healthCheckDetail; private TempDirectory tempDir; private bool disposed; public abstract Guid ClientId { get; } public abstract string ClientDisplayName { get; } protected RelativityConnectionInfo ConnectionInfo { get; } protected ClientConfiguration Configuration { get; } protected DiagnosticsContext Context { get; } protected IFileSystemService FileSystemService { get; } protected Guid TempFileId { get; } protected string TempFile { get; } protected ITransferLog Log { get; } protected ConnectionServiceBase(RelativityConnectionInfo connectionInfo, ClientConfiguration configuration, IFileSystemService fileSystemService, ITransferLog log, DiagnosticsContext context) { if (connectionInfo == null) throw new ArgumentNullException("connectionInfo"); if (configuration == null) throw new ArgumentNullException("configuration"); if (fileSystemService == null) throw new ArgumentNullException("fileSystemService"); if (log == null) throw new ArgumentNullException("log"); if (context == null) throw new ArgumentNullException("context"); ConnectionInfo = connectionInfo; Configuration = configuration; Context = context; Log = log; FileSystemService = fileSystemService; healthCheckDetail = new ConcurrentDictionary<string, object>(); TempFileId = Guid.NewGuid(); TempFile = FileSystemService.GetTempFileName(); } public Task<IConnectionCheckResult> ExecConnectionCheckAsync() { return ExecConnectionCheckAsync(CancellationToken.None); } public virtual async Task<IConnectionCheckResult> ExecConnectionCheckAsync(CancellationToken token) { string initMessage = string.Format(CultureInfo.CurrentCulture, CoreStrings.ConnectionCheckStartMessage, ClientDisplayName); WriteStatusMessage(initMessage, Array.Empty<object>()); try { await RunPreTestsAsync(token).ConfigureAwait(false); await RunUploadTestAsync(token).ConfigureAwait(false); await RunDownloadTestAsync(token).ConfigureAwait(false); await RunPostTestsAsync(token).ConfigureAwait(false); string resultMessage3 = string.Format(CultureInfo.CurrentCulture, CoreStrings.ConnectionCheckPassedMessage, ClientDisplayName); WriteStatusMessage(resultMessage3, Array.Empty<object>()); return new ConnectionCheckResult(healthCheckDetail) { ClientId = ClientId, IsSuccessful = true }; } catch (TransferException ex) { WriteErrorMessage(ex.Message, Array.Empty<object>()); string resultMessage2 = string.Format(CultureInfo.InvariantCulture, CoreStrings.ConnectionCheckFailedMessage, ClientDisplayName); WriteStatusMessage(resultMessage2, Array.Empty<object>()); return new ConnectionCheckResult(healthCheckDetail) { ClientId = ClientId, IsSuccessful = false, Message = ex.Message }; } catch (OperationCanceledException) { string message2 = string.Format(CultureInfo.CurrentCulture, CoreStrings.ConnectionCheckCancellationRequestMessage, Array.Empty<object>()); WriteStatusMessage(message2, Array.Empty<object>()); return new ConnectionCheckResult(healthCheckDetail) { ClientId = ClientId, IsSuccessful = false, Message = message2 }; } catch (Exception e) { string message = string.Format(CultureInfo.CurrentCulture, CoreStrings.ConnectionCheckExceptionMessage, ClientDisplayName); WriteStatusMessage(message, Array.Empty<object>()); WriteErrorMessage(e.Message, Array.Empty<object>()); string resultMessage = string.Format(CultureInfo.CurrentCulture, CoreStrings.ConnectionCheckFailedMessage, ClientDisplayName); WriteStatusMessage(resultMessage, Array.Empty<object>()); return new ConnectionCheckResult(healthCheckDetail) { ClientId = ClientId, IsSuccessful = false, Message = e.Message }; } finally { WriteStatusMessage(string.Empty, Array.Empty<object>()); } } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected async Task RunUploadTestAsync(CancellationToken token) { string uploadMessage = string.Format(CultureInfo.CurrentCulture, CoreStrings.ConnectionCheckUploadMessage, ClientDisplayName); WriteStatusMessage(uploadMessage, Array.Empty<object>()); CreateTestUploadFile(); await TestUploadAsync(token).ConfigureAwait(false); string uploadResultMessage = string.Format(CultureInfo.InvariantCulture, CoreStrings.ConnectionCheckUploadSuccessMessage, ClientDisplayName); WriteStatusMessage(uploadResultMessage, Array.Empty<object>()); } protected async Task RunDownloadTestAsync(CancellationToken token) { string downloadMessage = string.Format(CultureInfo.CurrentCulture, CoreStrings.ConnectionCheckDownloadMessage, ClientDisplayName); WriteStatusMessage(downloadMessage, Array.Empty<object>()); await TestDownloadAsync(token).ConfigureAwait(false); string downloadResultMessage = string.Format(CultureInfo.CurrentCulture, CoreStrings.ConnectionCheckDownloadSuccessMessage, ClientDisplayName); WriteStatusMessage(downloadResultMessage, Array.Empty<object>()); } protected virtual Task RunPreTestsAsync(CancellationToken token) { return Task.FromResult(true); } protected virtual Task RunPostTestsAsync(CancellationToken token) { return Task.FromResult(true); } protected abstract Task TestUploadAsync(CancellationToken token); protected abstract Task TestDownloadAsync(CancellationToken token); protected void WriteStatusMessage(string message, params object[] args) { string message2 = message; if (args != null && args.Length != 0) try { message2 = string.Format(CultureInfo.CurrentCulture, message, args); } catch (FormatException) { Debug.WriteLine("Check the incoming message arguments."); } Context?.PublishStatusMessage(ClientId, message2); } protected void WriteErrorMessage(string message, params object[] args) { string str = message; if (args != null && args.Length != 0) try { str = string.Format(CultureInfo.CurrentCulture, message, args); } catch (FormatException) { Debug.WriteLine("Check the incoming message arguments."); } Context?.PublishStatusMessage(ClientId, "Error : " + str); } protected TempDirectory GetTempDirectory() { if (tempDir == null) { tempDir = new TempDirectory(); tempDir.Create(); } return tempDir; } protected void AddClientInfoToHealthCheck(string key, object value) { if (string.IsNullOrEmpty(key)) throw new ArgumentNullException("key"); healthCheckDetail.TryAdd(key, value); } protected void AddClientInfoToHealthCheck(IDictionary<string, object> keyValuePair) { if (keyValuePair == null) throw new ArgumentNullException("keyValuePair"); foreach (KeyValuePair<string, object> item in keyValuePair) { healthCheckDetail.TryAdd(item.Key, item.Value); } } private void CreateTestUploadFile() { int num = 8000 / "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.".Length; string contents = string.Join(Environment.NewLine, Array.ConvertAll(new int[num], (int i) => "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")); FileSystemService.WriteAllText(TempFile, contents); } private void Dispose(bool disposing) { if (!disposed) { if (disposing) CleanTempFile(); disposed = true; } } private void CleanTempFile() { try { if (tempDir != null) tempDir.Dispose(); } finally { if (FileSystemService.FileExists(TempFile)) FileSystemService.DeleteFile(TempFile); } } } }