What is the best way to get error messages from a WF4 workflow back to a hosting ASP.NET MVC application? I need the workflow to not terminate, but continue to be active, and then pass a message back to the hosting app regarding the error, so the user can take an alternative action, but I'm not sure how to do that.
Three ways that I can think of...
WorkflowApplication.OnUnhandledException is there to let you know when an unhandled exception is thrown, but I'm not sure if you can recover from this. Doesn't seem like a good road to go down.
WorkflowApplication.PersistableIdle lets you know when a workflow activity has created a bookmark and idled the workflow. At this point the workflow is waiting for you to return with more information to pass back into the workflow when you resume from the bookmark. This might be your best bet as it is relatively simple to implement and use.
Another twist on this is to create an extension that your activities can get from the workflow context. Extensions give you a more flexible way to communicate outside of the workflow, although you have to code them up and ensure they work as expected. Bookmark + extension would be your most flexible option.
In order to keep your workflow alive you need to catch the exception in your workflow. Add a TryCatch activity to you workflow and in a Catch block you can use either a Send or a custom activity to send the data to the host application.
The one exception is to use the WorkflowApplication.OnUnhandledException with persistence and specify abort. In that case the in memory state of the workflow is just removed and the workflow can be reloaded for the last persisted state. If you go this way you need to make sure, using the Perist activity, that your workflow is saved whenever something that cannot be redone.
TryCatch is not really enough when it comes to WF4. Also, handling the UnhandledException event from your workflow host doesn't really tell you much about which activity failed and why.
A suggested approapch is to use TryCatch and Activity tracking within WF4. A good summary of this can be found here: http://msmvps.com/blogs/theproblemsolver/archive/2009/11/27/trycatch-activity-in-wf4.aspx
You can extend your workflow host with Tracking participants and with a catch handler that encapsulates your activity that might fault, handle the exception and create a new TrackingRecord that can better illustrate what happened.