/// <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 } } } }