Amnesia.UI.ProcessRequest C# (CSharp) Method

ProcessRequest() public static method

public static ProcessRequest ( HttpContext ctx ) : void
ctx System.Web.HttpContext
return void
        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>");
            }
        }

Usage Example

Beispiel #1
0
        void IHttpHandler.ProcessRequest(HttpContext ctx)
        {
            try
            {
                if (ctx.Request.Url.GetComponents(UriComponents.Path, UriFormat.Unescaped).ToLower().EndsWith("/ui"))
                {
                    UI.ProcessRequest(ctx);
                }
                else
                {
                    string reqPayload;

                    using (StreamReader reader = new StreamReader(ctx.Request.InputStream))
                        reqPayload = reader.ReadToEnd();

                    ICommand command  = (ICommand)SerializationUtil.DeserializeBase64(reqPayload);
                    Response response = command.Execute(ctx);

                    ctx.Response.Write(SerializationUtil.SerializeBase64(response));
                }
            }
            catch (Exception error)
            {
                var errorResponse = new ErrorResponse()
                {
                    Message = error.Message, ExceptionType = error.GetType().FullName, StackTrace = error.StackTrace
                };
                ctx.Response.Write(SerializationUtil.SerializeBase64(errorResponse));
            }
        }