public static void ProcessRequest(HttpContext ctx)
{
bool silent = !string.IsNullOrEmpty(ctx.Request.QueryString["silent"]);
if (ctx.Request.QueryString["cmd"] == "start")
{
if (scopeThread != null)
throw new InvalidOperationException("Session is already started");
scopeThread = new Thread(arg =>
{
// create new scope
using (rootScope = new TransactionScope(TransactionScopeOption.Required, TimeSpan.Zero))
{
rootTransaction = Transaction.Current;
// notify request thread that scope is created
requestThreadProceed.Set();
// wait for session to end
scopeThreadProceed.WaitOne();
}
// return control to request thread
requestThreadProceed.Set();
});
// wait for the the helper thread create the root scope
scopeThreadProceed.Reset();
requestThreadProceed.Reset();
scopeThread.Start();
requestThreadProceed.WaitOne();
// use the transaction created by the scopeThread
(new Handler.StartSessionRequest() { Transaction = rootTransaction }).Execute(HttpContext.Current);
}
else if (ctx.Request.QueryString["cmd"] == "end")
{
try
{
(new Handler.EndSessionRequest()).Execute(HttpContext.Current);
}
finally
{
if (scopeThread != null)
{
// notify the root thread that the scope can be released
requestThreadProceed.Reset();
scopeThreadProceed.Set();
requestThreadProceed.WaitOne();
scopeThread = null;
}
}
}
if (!silent)
{
// Output UI based on new state
ctx.Response.Write(@"
<html>
<head><title>Amnesia UI</title></head>
<body>");
if (!Session.IsActive && !Session.IsRollbackPending)
ctx.Response.Write(@"<a href='?cmd=start'>Start Session</a>");
else
{
ctx.Response.Write(string.Format(@"<a href='?cmd=end'>End Session</a><br/>"));
ctx.Response.Write(string.Format(@"ID: {0}<br/>", Session.ID));
if (Session.Transaction != null)
ctx.Response.Write(string.Format(@"Transaction: {0}<br/>", Session.Transaction.TransactionInformation.Status));
}
ctx.Response.Write(@"<br /><br /><a href='?cmd=status'>Refresh Status</a>");
ctx.Response.Write(@"
</body>
</html>");
}
}