title: Can not attach console
body: <p>I'm trying to attach a console to a Service if im <strong>running it from the debugger</strong>.
  I've read up a couple of "working" solutions, but those seem not to really work. Here is he Code I use:</p>
  
  <pre><code>    public static void RunService(Func&lt;ServiceBase&gt; factory)
      {
          if (Debugger.IsAttached)
          {
              Utils.AttachConsole();
              Console.Write($"Starting service ");
              var instance = factory();
              Console.WriteLine(instance.GetType().Name);
              //Invoke start Method
              Console.WriteLine("Press [ENTER] to exit");
              Console.ReadLine();
              //Stop service
          }
          else
          {
              ServiceBase.Run(factory());
          }
      }
  </code></pre>
  
  <p>Alloc Console is:</p>
  
  <pre><code>    public static void AttachConsole()
      {
          var ret = NativeMethods.AllocConsole();
          IntPtr currentStdout = NativeMethods.GetStdHandle(NativeMethods.STD_OUTPUT_HANDLE);
          NativeMethods.SetStdHandle(NativeMethods.STD_OUTPUT_HANDLE, new IntPtr(7));
          TextWriter writer = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true };
          Console.SetOut(writer);
      }
  </code></pre>
  
  <p>and the Interop includes:</p>
  
  <pre><code>internal static class NativeMethods
  {
      internal const uint STD_OUTPUT_HANDLE = 0xFFFFFFF5;
  
      [DllImport("kernel32.dll")]
      [return: MarshalAs(UnmanagedType.Bool)]
      internal static extern bool AllocConsole();
  
      [DllImport("kernel32.dll")]
      internal static extern IntPtr GetStdHandle(uint nStdHandle);
  
      [DllImport("kernel32.dll")]
      internal static extern void SetStdHandle(uint nStdHandle, IntPtr handle);
  }
  </code></pre>
  
  <p>What happens is, a console is created and attached, but there is no output. Can't be to difficult, but I'm to dumb to see it :(</p>
  
  <p><strong>EDIT:</strong>
  The Issue is Visual Studio, it is not the code "itself". Without VS, I can get a console and receive the expected output there. There is some kind of redirection in VS, which I am looking to overcome here.</p>
  
  <p><strong>EDIT</strong> <em>just for Hans</em> - here is the "full code"</p>
  
  <pre><code>static void Main(string[] args)
  {
      ServiceLauncher.RunService(() =&gt; new FactoryService();
  }
  </code></pre>
  
  <p>The project is set to application type windows.</p>