Question

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?

Was it helpful?

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.

OTHER TIPS

That is incorrect.

AJAX is a client side technique relying on JavaScript. As long as the web server can respond to an HTTP request, it can server AJAX content (whatever that might mean:-).

I would use this UtilDev Cassini as me embedded web server of choice, it is based on the code from the embedded Visual Studio dev server and can run pretty much anything that runs in IIS. AJAX is a browser technology not a server technology, the server just sees http requests the same as any other. My last point would that it seems slightly odd to embed a web server in a console application. It would be more usual to do this with a windows service. Have you considered converting the console app into a windows service?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top