In my previous post on WorkflowServices, CanCreateInstance and Silverlight I shared with you some of the pain that I went through in building my first Silverlight / Workflow Services app. Much of this pain was just because I have not done a great deal of work with Silverlight and WCF. In this post I’m going to give you some advice based on what I learned in building my Tech-Ed 2011 State Machine demo application.
Download Sample Code Windows Workflow Foundation (WF4) - Silverlight / State Machine Workflow Service
Tip: Get Microsoft.Activities and use SilverlightFaultBehavior
My colleagues on the Silverlight team told me about the SilverlightFaultBehavior that they included in the Silverlight-Enabled WCF Service template. The trick of this behavior is that it sets the response code of Faults to 200 (OK) so that Silverlight can pass the fault along to your code.
There is a great deal of WCF Judo that goes on to make this work but here is the essential piece that sets the response to HttpStatusCode.OK
Typically WCF Code applies this behavior with an attribute but with WorkflowServices you can’t do that. So I created the SilverlightFaultElement class that allows you to setup the behavior with configuration.
Tip: While Developing use Microsoft.Activities.Diagnostics.WorkflowServiceTraceBehavior
There I was trying to run my workflow service and wondering why it was not working. I had not written a unit test for it yet and I was wishing for the nice tracking information I got with the tracking extensions from Microsoft.Activities.UnitTesting. Then I had this idea… what if I built a WCF behavior that would setup tracking on the WorkflowServiceHost and then output the tracking info to the Visual Studio debug window.
So I created the WorkflowServiceTraceBehavior and WorkflowServiceTraceElement classes to enable the behavior via configuration.
How To Configure These Behaviors in your app
Step 1: Create a new Silverlight Application
- Create a new Silverlight Application and add a Web project
- Add a new WCF Workflow Service to the web project (Do not set CanCreateInstance true on the receive activity for now)
- Add a button to the Silverlight markup and add a click handler
Step 2: Add WCF Configuration to your service
Add the following configuration to your web.config. At this point we won’t add the behaviors so you can see the problem.
Step 3: Add a Service Reference in the Silverlight app
- Right click and select Add Service Reference
- Discover the WCF Workflow Service you added to the web app
- Add a service reference
- Add the code to call the service
Step 4: Ready for the Pain?
Debug the application and click the test button. Here is what you will get
System.ServiceModel.CommunicationException was unhandled by user code
Message=The remote server returned an error: NotFound.
Huh? Not found? Don’t believe it my friends… that is not the real problem. Now let’s enable some behaviors and see what happens.
Step 5: Use NuGet to add Microsoft.Activities to your project
NuGet is so awesome – if you aren’t using it yet, get it now.
- Open the Library Package Manager Console (all the cool kids use the console but there is also a UI)
- Set the Default Project to your web project
- PM>Install-Package Microsoft.Activities
Now your web app will have all the goodness of Microsoft.Activities.
Step 6: Add the SilverlightFaultBehavior and WorkflowServiceTraceBehavior
Modify the web config to add the behaviors
Step 7: Test SilverlightFaultBehavior
Debug the application and click the test button again. This time you will get the real error.
Now you get the correct exception
System.ServiceModel.FaultException was unhandled by user code
Message=There is no context attached to the incoming message for the service and the current operation is not marked with "CanCreateInstance = true". In order to communicate with this service check whether the incoming binding supports the context protocol and has a valid context initialized.
Step 8: Test WorkflowServiceTraceBehavior
Now open your workflow service and set CanCreateInstance=true on the receive activity. Now your app should work correctly. Debug it again and in the output window you will see tracking information about everything that is happening in your service.
I don’t know how I ever did WorkflowService development without the WorkflowServiceTraceBehavior. It makes it so easy to figure out exactly what is going on with my service. You can use it with any kind of WorkflowService so check it out.