Search code examples
c#.netajaxwebserverembeddedwebserver

C# Web Server for displaying Console Output?


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?


Solution

  • How to re-route console output

    You will need to write your own TextWriter and make Console use it via Console.SetOut. This writer should notify connected web clients, as well as the original Console.Out.

    How to host a COMET-like server

    You can use HttpListener and some basic async programming to do this. If you wrap the HttpListenerContext.Response.OutputStream in a StreamWriter (with AutoFlush set to true) and set HttpListenerContext.Response.SendChunked to true clients 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:

    netsh http add urlacl url=http://+:9090/ user=domain\username
    

    Code?

    I couldn't resist it; I have written a (poorly tested and mostly incomplete) sample.