WcfTestClient with WCF Workflow Service wont send DateTime message properties

Posted : Tuesday, 29 November 2011 11:20:00

A while ago I did the MCTS exam for version 3.5 of Windows Workflow Foundation and quite liked it but haven’t had recourse to use it in a production system. Recently a project came up that I thought would be good candidate but since I got my MCTS there has been a new version of Workflow released with quite a few changes so I thought I better get up to speed with the new tech before deciding whether or not to use it. I came up against a pretty nasty problem and couldn’t find anything on the net to suggest a fix, it took a while but I found the cause of the problem. What follows is a brief explanation of the problem followed by the fix that worked for me.

So I created a Workflow Service using the project template and named it SimpleWorkflowService.


This service simply takes two dates and calculates the difference between them in days then returns that value to the caller. Obviously this is a trivial example and doesn’t highlight the strengths of the Workflow ecosystem but that is not the point of this post Smile. Next I added a class to the project to represent the Message that will be sent to the service, its basically a simple DTO object with two DateTime properties:

    6     [DataContract]

    7     public class DaysBetweenServiceRequest

    8     {

    9         [DataMember]

   10         public DateTime FromDate { get; set; }

   11         [DataMember]

   12         public DateTime ToDate { get; set; }

   13     }

Next I added a CodeActivity<Int32> derived class which takes an instance of the DaysBetweenServiceRequest class called CalculateDaysDiffActivity. The code is very simple and just calls the Subtract operation on ToDate using FromDate as the method parameter, this returns an instance of a Timespan object representing the difference between the two dates. The Execute operation of the CodeActivity object returns the Days property of the TimeSpan returned by the Subtract operation:

   11     public class CalculateDaysDiffActivity : CodeActivity<int>

   12     {

   13         [RequiredArgument]

   14         public InArgument<DaysBetweenServiceRequest> ServiceRequest { get; set; }


   16         protected override int Execute(CodeActivityContext context)

   17         {

   18             DateTime date1 = ServiceRequest.Get(context).FromDate.Date;

   19             DateTime date2 = ServiceRequest.Get(context).ToDate.Date;


   21             return date2.Subtract(date1).Days;

   22         }

  • (This will not appear on the site)