public static void DeleteLink(this SftpClient client, string path) { Type sftpClientType = client.GetType(); FieldInfo sftpSessionField = sftpClientType.GetField("_sftpSession", BindingFlags.NonPublic | BindingFlags.Instance); object sftpSession = sftpSessionField.GetValue(client); Type sftpSessionType = sftpSession.GetType(); MethodInfo requestRemoveMethod = sftpSessionType.GetMethod("RequestRemove", BindingFlags.NonPublic | BindingFlags.Instance); requestRemoveMethod.Invoke(sftpSession, new object[] { path }); }
public void Dispose() { if (_client == null) { return; } Task.Run(() => { // _log.Debug("Disposing _client"); var timer = new System.Timers.Timer { Interval = 2000, AutoReset = false }; timer.Elapsed += (s, e) => { try { var sessionField = _client.GetType().GetProperty("Session", BindingFlags.NonPublic | BindingFlags.Instance); if (sessionField != null) { var session = sessionField.GetValue(_client); if (session != null) { var socketField = session.GetType().GetField("_socket", BindingFlags.NonPublic | BindingFlags.Instance); if (socketField != null) { var socket = (Socket)socketField.GetValue(session); if (socket != null) { // _log.Debug($"Socket state: Connected = {socket.Connected}, Blocking = {socket.Blocking}, Available = {socket.Available}, LocalEndPoint = {socket.LocalEndPoint}, RemoteEndPoint = {socket.RemoteEndPoint}"); // _log.Debug("Set _socket to null"); try { socket.Dispose(); } catch (Exception ex) { // _log.Debug("Exception disposing _socket", ex); } socketField.SetValue(session, null); } else { // _log.Debug("_socket was null"); } } var messageListenerCompletedField = session.GetType().GetField("_messageListenerCompleted", BindingFlags.NonPublic | BindingFlags.Instance); var messageListenerCompleted = (EventWaitHandle)messageListenerCompletedField.GetValue(session); if (messageListenerCompleted != null) { var waitHandleSet = messageListenerCompleted.WaitOne(0); // _log.Debug($"_messageListenerCompleted was set = {waitHandleSet}"); if (!waitHandleSet) { // _log.Debug($"Calling Set()"); messageListenerCompleted.Set(); } } else { // _log.Debug("_messageListenerCompleted was null"); } } else { // _log.Debug("Session was null"); } } } catch (Exception ex) { // _log.Debug($"Exception in Timer event handler", ex); } }; timer.Start(); _client.Dispose(); // _log.Info("Disposed _client"); }); }