コード例 #1
0
        /// <summary>
        /// Invoked when a web request arrives.
        /// </summary>
        public void ProcessRequest(HttpContext context)
        {
            try
            {
                // bind ourselves to the context
                Context = context;

                // request was for a resource
                if (Request[ResourceQueryKey] != null)
                {
                    // handle it and return, no need to deal with workflow
                    ProcessResourceRequest(Request[ResourceQueryKey]);
                    return;
                }

                // obtain our activity instance
                Activity = CreateActivity();

                // configure application
                WorkflowApplication = Request[InstanceIdQueryKey] == null ? new WorkflowApplication(Activity, GetArguments()) : new WorkflowApplication(Activity);
                WorkflowApplication.Extensions.Add <ITwilioContext>(() => this);
                WorkflowApplication.SynchronizationContext = new SynchronizedSynchronizationContext();
                WorkflowApplication.InstanceStore          = CreateInstanceStore();
                WorkflowApplication.Aborted              = OnAborted;
                WorkflowApplication.Completed            = OnCompleted;
                WorkflowApplication.Idle                 = OnIdle;
                WorkflowApplication.OnUnhandledException = OnUnhandledException;
                WorkflowApplication.PersistableIdle      = OnPersistableIdle;
                WorkflowApplication.Unloaded             = OnUnloaded;

                // attempt to resolve current instance id and reload workflow state
                if (Request[InstanceIdQueryKey] != null)
                {
                    WorkflowApplication.Load(Guid.Parse(Request[InstanceIdQueryKey]), Timeout);
                }

                // postback to resume a bookmark
                if (Request[BookmarkQueryKey] != null)
                {
                    WorkflowApplication.ResumeBookmark(Request[BookmarkQueryKey], GetPostData(), Timeout);
                }
                else
                {
                    // begin running the workflow from the start
                    WorkflowApplication.Run(Timeout);
                }

                // throw exception
                if (UnhandledExceptionInfo != null)
                {
                    UnhandledExceptionInfo.Throw();
                }

                // strip off temporary attributes
                foreach (var element in TwilioResponse.DescendantsAndSelf())
                {
                    foreach (var attribute in element.Attributes())
                    {
                        if (attribute.Name.Namespace == tmpNs)
                        {
                            attribute.Remove();
                        }
                    }
                }

                // write finished twilio output
                Response.ContentType = "text/xml";
                using (var wrt = XmlWriter.Create(Response.Output))
                    TwilioResponse.WriteTo(wrt);

                // if we've reached the end, no need to force unload
                WorkflowApplication = null;
            }
            finally
            {
                // clean up application if possible
                if (WorkflowApplication != null)
                {
                    try
                    {
                        WorkflowApplication.Unload(Timeout);
                        WorkflowApplication = null;
                    }
                    catch
                    {
                        // ignore
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Publishes the specified ex.
        /// </summary>
        /// <param name="ex">The ex.</param>
        /// <param name="additionalParameters">The additional parameters.</param>
        public void Publish(Exception ex, NameValueCollection additionalParameters)
        {
            int occurrences;

            if (!TimeEventTracker.CanEvent(ex.Message, _eventTracking, out occurrences))
            {
                return;
            }

            additionalParameters = additionalParameters ?? new NameValueCollection();

            if (occurrences != 0)
            {
                additionalParameters = additionalParameters ?? new NameValueCollection();
                additionalParameters["Occurrences"] = occurrences.ToString("N0");
            }

            lock (_publishers)
            {
                try
                {
                    try
                    {
                        _unhandledExceptions.Clear();
                        _unhandledExceptions.Capacity = 0;

                        foreach (PublisherInfo publisher in _publishers.ToArray())
                        {
                            try
                            {
                                publisher.Publisher.Publish(ex, additionalParameters);
                            }
                            catch (Exception unhandledEx)
                            {
                                MiscHelper.DisposeObject(publisher);
                                _publishers.Remove(publisher);

                                var unEx = new UnhandledExceptionInfo()
                                {
                                    PublisherAttributes = publisher.PublisherAttributes,
                                    Exception           = unhandledEx
                                };

                                _unhandledExceptions.Add(unEx);
                            }
                        }

                        foreach (UnhandledExceptionInfo eInfo in _unhandledExceptions)
                        {
                            var eMsg       = string.Format("The following Publisher: '{0}' caused an Unhandled exception", eInfo.PublisherAttributes.Attributes["name"]);
                            var internalEx = new Exception(eMsg, eInfo.Exception);

                            var exMessage = ExceptionFormatter.ConstructMessage(internalEx);
                            MiscHelper.WriteToEventLog(_eventLogSource, exMessage, EventLogEntryType.Error);
                        }
                    }
                    catch (Exception excep)
                    {
                        var exMessage = ExceptionFormatter.ConstructMessage(excep);
                        MiscHelper.WriteToEventLog(_eventLogSource, exMessage, EventLogEntryType.Error);
                    }
                }
                catch (Exception)
                {
                    // not much we can do here.
                }
            }
        }