It’s now time to wrap up this series on the Workflow Studio SDK and talk about a pretty handy topic for debugging the activities that you create within Visual Studio.  If you have developed code in the past, you know that one of the most handy ways to debug your code is to step through your code in real-time as the program is running.  As you step through your code, you can check the programming logic and make sure it is executing as expected.  You can also “watch” variables as they are declared and populated with values.  Watching variables is especially important if they are being filled from backend database calls or APIs. 

In this article, I’m going to show you how you can step through your Visual Studio code in real-time as your activity is being executed within the Workflow Studio Designer.  As always, I’ll keep the process simple and break it down into the following steps:
• Step 1 – Initial setup
• Step 2 – Place break points in the Visual Studio project
• Step 3 – Verify the project build settings
• Step 4 – Attach Visual Studio to the WorkflowExpressRuntime.exe process
• Step 5 – Start the workflow within the Workflow Studio Designer
• Step 6 – Step through your code in Visual Studio when your break points are hit
 

Step 1 – Initial setup
Workflow Studio needs to be installed on your Visual Studio development machine.  This allows Visual Studio to attach to the various Workflow Studio processes for stepping through your code (as discussed in a later step).

Next, when you are ready to test your workflow and step through your code, you essentially need two programs running.  First, you need your Visual Studio project opened so you can step through your code.  Second, you need the Workflow Studio Designer opened so you can place your activity in a workflow and initiate the test.  If you do not know how to add your activity to the Workflow Studio Designer, please refer to my last blog (Part 4) on how to test your new Activity DLL within Workflow Studio. 

I’m going to use the same “AdvancedDelay” example that I discussed in the last blog to illustrate the step-through process.  My workflow is shown below.  The activity I’m trying to step-through is the AdvancedDelay activity.  This activity inserts a delay into the workflow for a specified amount of time (about 5000 milliseconds in this example).  When this activity completes, a message box will pop up via the MessageBox activity.


Step 2 – Place break points in the Visual Studio project
Once you have your activity placed in a workflow within the Workflow Studio Designer, you can place break points in your Visual Studio project.  In this example, I placed a breakpoint in the line of code that is actually performing the delay here.  Within the execution section of my activity project, the Sleep() call is actually performing the delay.  I want to step through this code myself to make sure it works while my workflow is running.


Step 3 – Verify the Project Build settings.
Your Visual Studio project needs to be set up in a certain way in order for you to step through your code in real-time.  The following two screen shots show how I have my project set up.  You can access these same screens by going to the Properties of the activity project. 

I actually did not have to make any changes to the project settings as is shown here – my project was already set up correctly based on the project template.  Your project is most likely already set up this same way as well.  I really wanted to give you these screens here for reference in case you were playing with these properties during the development stage.  A few things you’ll need to check at the least:
• Your project should be in “Debug” mode instead of “Release” mode. 
• The “Optimize code” setting should be disabled
• In the Advanced dialog, the “Debug Info” setting should be set to “full“.







Step 4 – Attach Visual Studio to the WorkflowExpressRuntime.exe process
Next, navigate to Tools–>Attach to Process within Visual Studio.  In the Attach to Process dialog, select “WorkflowExpressRuntime.exe“.  This is the process used by the Workflow Studio Designer for executing workflows.  If you do not see this process listed, it means that you do not have the Workflow Studio Designer open.   

If you are curious about what the “WorkflowRuntimeHostService.exe” process is, that is the process used by Workflow Studio for executing regular jobs.  If you want to step through your code when executing a job instead of using the Workflow Studio Designer, you can attach to that process instead and then run a job containing this activity.  It’s pretty cool we have some flexibility here for stepping through our code in either situation!

Step 5 – Start the workflow within the Workflow Studio Designer
Once Visual Studio is attached to the correct process, we are ready to begin our test.  Start the workflow within the Workflow Studio Designer.  When our custom activity is executing (AdvancedDelay1), any break points that are hit within our code will automatically bring up Visual Studio at those break points.


Step 6 – Step through your code in Visual Studio when the break points are hit
Our break points are now getting hit and we can begin stepping through our code.  The standard way to step through your code in C# is with the following keys – Step Over (F10), Step Into (F11), Step Out (Shift + F11), and Continue Execution (F5).

In our example below, we set a break point when our Sleep() command actually performs the delay.  If you execute this statement, the sleep will actually occur for the specified amount of time as specified within the activity (which is 5 seconds here). 


Final remarks:
You should now be well on your way for creating custom activities within Visual Studio, testing them, and stepping through your code for advanced debugging.  I hope this series was helpful and stay tuned for more information on using the SDK.  If there are certain topics that you would like us to focus on in the future, please let us know!

Blogs in this series:
Getting Started
Setting up your Visual Studio development environment
Anatomy of a Workflow Studio activity project
Testing your Workflow Studio activity project
• Stepping through your Visual Studio code (this one)