Using v2013.1.8 I get a NullReferenceException when calling .Flush() on a ShellStream. I'm caching the ShellStream so that I don't have to reopen it constantly, so this may be the cause.
The object itself is instantiated and normally works the first time around, but all following times where Flush() is called then a NullReferenceException occurs.
I've roughly got it setup as follows (note that RunTelnetCommand() is called every 5 seconds):
-------------------------------------------------------
private static Dictionary shellStreams;
protected override void OnStartup(StartupEventArgs startupEventArgs)
{
shellStreams = new Dictionary();
}
private void RunTelnetCommand()
{
var sshClient = new SshClient(new PasswordConnectionInfo(ipAddress, "root", "password")
{
Timeout = TimeSpan.FromSeconds(30)
});
sshClient.ErrorOccurred += (sender, exceptionEventArgs) => Global.Logger.ErrorException(exceptionEventArgs.Exception.Message, exceptionEventArgs.Exception);
sshClient.Connect();
if (!sshClient.IsConnected)
{
Global.Logger.Error(string.Format(CultureInfo.CurrentCulture, Extensions.Shared.Globalisation.Resources.ValueIsInvalid, "sshClient.IsConnected"));
continue;
}
if (!shellStreams.Keys.Contains(identifier))
{
shellStreams.Add(identifier, sshClient.CreateShellStream("dumb", 80, 24, 800, 600, 1024));
}
var shellStream = shellStreams.SingleOrDefault(keyValuePair => keyValuePair.Key.Equals(identifier)).Value;
if (shellStream == null)
{
Global.Logger.Error(string.Format(CultureInfo.CurrentCulture, Extensions.Shared.Globalisation.Resources.ValueIsNullOrEmpty, "shellStream"));
return;
}
shellStream.Flush();
}
Hopefully that makes sense! I've had to rename some variables and methods for confidentiality sake.
After the explicit .Flush() I create a StreamWriter and StreamReader, I did have the Autoflush property set to true when creating the StreamWriter using the shellStream instance as a base, but this also threw the NullReferenceException, so I pulled it out to make it clearer that this was the issue.
Cheers!
The object itself is instantiated and normally works the first time around, but all following times where Flush() is called then a NullReferenceException occurs.
I've roughly got it setup as follows (note that RunTelnetCommand() is called every 5 seconds):
-------------------------------------------------------
private static Dictionary shellStreams;
protected override void OnStartup(StartupEventArgs startupEventArgs)
{
shellStreams = new Dictionary();
}
private void RunTelnetCommand()
{
var sshClient = new SshClient(new PasswordConnectionInfo(ipAddress, "root", "password")
{
Timeout = TimeSpan.FromSeconds(30)
});
sshClient.ErrorOccurred += (sender, exceptionEventArgs) => Global.Logger.ErrorException(exceptionEventArgs.Exception.Message, exceptionEventArgs.Exception);
sshClient.Connect();
if (!sshClient.IsConnected)
{
Global.Logger.Error(string.Format(CultureInfo.CurrentCulture, Extensions.Shared.Globalisation.Resources.ValueIsInvalid, "sshClient.IsConnected"));
continue;
}
if (!shellStreams.Keys.Contains(identifier))
{
shellStreams.Add(identifier, sshClient.CreateShellStream("dumb", 80, 24, 800, 600, 1024));
}
var shellStream = shellStreams.SingleOrDefault(keyValuePair => keyValuePair.Key.Equals(identifier)).Value;
if (shellStream == null)
{
Global.Logger.Error(string.Format(CultureInfo.CurrentCulture, Extensions.Shared.Globalisation.Resources.ValueIsNullOrEmpty, "shellStream"));
return;
}
shellStream.Flush();
}
Hopefully that makes sense! I've had to rename some variables and methods for confidentiality sake.
After the explicit .Flush() I create a StreamWriter and StreamReader, I did have the Autoflush property set to true when creating the StreamWriter using the shellStream instance as a base, but this also threw the NullReferenceException, so I pulled it out to make it clearer that this was the issue.
Cheers!