I have just finished writing my c# console application, and I am contemplating embedding a web server into it (probably this one http://webserver.codeplex.com). I don’t do much in the way over advanced web coding though, so I am not sure if I can do what I need to.
Basically, I would like to allow users to view the console output of my application in realtime just by visiting the site being served by my application. If I understand correctly, to do something like this it would require AJAX, which a simple C# Web Server wouldn’t be able to handle.
Is this correct or is there an easy way to do this I am missing?
How to re-route console output
You will need to write your own
TextWriterand makeConsoleuse it viaConsole.SetOut. This writer should notify connected web clients, as well as the originalConsole.Out.How to host a COMET-like server
You can use
HttpListenerand some basic async programming to do this. If you wrap theHttpListenerContext.Response.OutputStreamin aStreamWriter(withAutoFlushset totrue) and setHttpListenerContext.Response.SendChunkedtotrueclients will receive partial results – this means you can even do it in an IFRAME.You will need to add rights to the URL for yourself if UAC is enabled:
Code?
I couldn’t resist it; I have written a (poorly tested and mostly incomplete) sample.