coolblue2000
coolblue2000

Reputation: 4908

Sql Server Agent Fails to run .net console application

I have a console app that runs absolutely fine manually, but when it is executed by SQL Server Agent it fails to run at all and also spits out a strange error message as follows:-

Executed as user: I01SVTD21\SYSTEM.
Unhandled Exception: System.IO.IOException: The handle is invalid.       
     at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)     
     at System.Console.GetBufferInfo(Boolean throwOnNoConsole, Boolean& succeeded)
     at System.Console.Clear()
     at ActiveDirectoryImport.Program.SendReports()
     at ActiveDirectoryImport.Program.GetUserInfo()
     at ActiveDirectoryImport.Program.Main(String[] args).
Process Exit Code 255.  The step failed.

Now this may not appear like a strange error message at first, until you realise that ActiveDirectoryImport.Program.SendReports() is the last method called by ActiveDirectoryImport.Program.GetUserInfo() so it seems like most of the code has run. However this is not the case as one of the first things the code does is update records in a database table, which has not happened.....

So I am left with two questions. Why is the job failing at all when it runs fine from the same location manually? and, Why is it telling me that it is failing late on in the code when it clearly has not got that far?

The console app does write to the console screen but takes no user input. It basically writes the status of the application to the screen and this is seemingly erroring....

Any help is appreciated.

Upvotes: 1

Views: 1375

Answers (1)

Christian.K
Christian.K

Reputation: 49280

Maybe this should be a comment, but from the exception (and a bit of psychic debugging) it is quite "evident":

The System.Console.Clear() will cause the exception you see, when the process' standard output is not attached to the console's output. Easiest reproducible when you invoke your application from the command line, and redirect its output:

MyConsoleApplication.exe > NUL

Check that your SQL Agent command line doesn't contain such a redirection - actually, I'm not sure about it, but SQL Agent might automatically redirect your process' output to show it in it's log. So best thing would be to either remove the System.Console.Clear() call altogether , arguably it doesn't make a lot of sense in this scenario anyway. Alternatively, simply wrap it like this

   try
   {
      System.Console.Clear();
   }
   catch (IOException)
   {
   }

To ignore this particular error. Note that other Console functions/properties like CursorVisible, may also throw an IOException in that case.

BTW, CursorVisible is actually a "good" candidate to write such a helper function:

 bool ConsoleInputIsRedirected
 {
     get
     {
         try
         {
             bool f = System.Console.CursorVisible;
             return false;
         }
         catch (IOException)
         {
             return true;
         }
     }
 }

If you need to, you can then use this function to conditionally execute the code that does only work when the output is not redirected (like Clear()).

Upvotes: 2

Related Questions