Example #1
0
        private void HandleMessageThread(MessageContext messageContext, TaskEntry taskEntry)
        {
            ASSERT( messageContext != null, "Missing parameter 'messageContext'" );
            // ASSERT( taskEntry != null, "Missing parameter 'taskEntry'" ); => 'null' means "CallbackItem.CallbackThreaded == null"

            var message = messageContext.Message;
            var callbackItem = messageContext.CallbackItem;

            LOG( "HandleMessageThread(" + taskEntry + "," + message + ") - Start" );

            bool contextRestored = false;
            try
            {
                if(! callbackItem.IsThreaded )
                {
                    // Inline message handler => This thread is still the HTTP handler's thread => No need to restore thread's context

                    ASSERT( callbackItem.CallbackDirect != null, "If not 'IsThreaded' then 'callbackItem.CallbackDirect' is supposed to be set" );
                    ASSERT( callbackItem.CallbackThreaded == null, "If not 'IsThreaded' then 'callbackItem.CallbackThreaded' is supposed to be null" );

                    callbackItem.CallbackDirect( message );
                }
                else  // Threaded message handler => Must restore some of the HTTP handler's thread context
                {
                    ASSERT( callbackItem.CallbackDirect == null, "If 'IsThreaded' then 'callbackItem.CallbackDirect' is supposed to be null" );
                    ASSERT( callbackItem.CallbackThreaded != null, "If 'IsThreaded' then 'callbackItem.CallbackThreaded' is supposed to be set" );

                    // Restore message's context
                    messageContext.RestoreContext();
                    contextRestored = true;

                    callbackItem.CallbackThreaded( taskEntry, message );
                }
                LOG( "HandleMessageThread(" + taskEntry + "," + message + ") - Exit" );
            }
            catch( System.Reflection.TargetInvocationException ex )
            {
                LOG( "HandleMessageThread(" + taskEntry + "," + message + ") - TargetInvocationException" );
                SendMessageToConnection( message.SenderConnectionID, Message.CreateExceptionMessage(exception:ex.InnerException, sourceMessage:message) );
            }
            catch( System.Exception ex )
            {
                LOG( "HandleMessageThread(" + taskEntry + "," + message + ") - Exception" );
                SendMessageToConnection( message.SenderConnectionID, Message.CreateExceptionMessage(exception:ex, sourceMessage:message) );
            }
            finally
            {
                if( contextRestored && (ClearMessageContextObject != null) )
                    // Clear the restored message context
                    try { ClearMessageContextObject(); }
                    catch( System.Exception ex )  { FAIL( "'ClearMessageContextObject()' threw an exception (" + ex.GetType().FullName + "): " + ex.Message ); }
            }
        }