Saturday, 2 February 2013

Real-Time Write to WebBrowser showing WCF REST API page, using HttpContent.Response, from C# REPL Script

In this post I will show a powerful technique for ASP.NET Debugging/Programming, where the Web C# REPL that I recently added to TeamMentor is triggered from a WCF REST API, in such a way that the HttpContext can be programmed in real-time, with the added bonus that the HttpContext.Response OutputStream stays open (and can be written to multiple times).

It all starts with a new TeamMentor REST API method called ‘/admin/scripts/{name}’ :

image

which is powered by:

image

and is basically a reflection engine to compile and invoke the strings returned by this class:

image

This means that in practice:

The /admin/scripts/ping REST call will return the execution result of the string provided by O2_Script_Library.ping() method:

image

The /admin/scripts/list REST call will return the execution result of the string provided by O2_Script_Library.list() method:

image

The /admin/scripts/openLogVewer REST call will return the execution result of the string provided by O2_Script_Library.openLogViewer() method:

image

Note that in screenshot above, the LogViewer is a .NET WinForms created by the Cassini Process (the one hosting the WCF REST API).

The really interesting script/method, is the O2_Script_Library.script_Me() :

image

This script, when invoked via the /admin/scripts/script_Me REST call, will provide a C# REPL environment, with a live HttpContext object available as a programmable variable :)

image

Even better, because of the use of .waitForClose() in the httpContext.Current.script_Me().waitForClose() command, we can write in real-time to the browser (ie. the HttpContext.Response OutputStream is still alive)

image

This means that we have a nice REPL environment, from C# into a web browser:

image

And in the C# REPL we have full access to all the C# programming techniques, like for example a for-loop

image

What I really like about this technique, is that it is going to allow me to write custom monitoring and debugging scripts in C# that interact with the target webpage :)

Interestingly the browser will keep the connection open for quite a while, which give us plenty of time to write and debug scripts.

Finally, to release the browser connection, use Response.End() :

image

Similar Techniques:

Post a Comment