Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
DZone MVB

Links

Social

Example: Creating workflow to request songs from radio station

Today I played with WF 4.0 declarative workflows. This posting shows you simple workflow that you can extend to add songs to online radio station queue. My main focus here is workflow and I will provide here no logic for actual queue implementation. You can find here also Visual Studio 2010 sample solution.

Windows Workflow Foundation 4.0 that came with Visual Studio 2010 introduces many improvements over previous version like new workflow designer, flowchart workflows and better integration with WCF. This example is based on new project type in Visual Studio 2010: WCF Workflow Service Application. Basically this is type of WCF application that hosts workflows (and other stuff you add to the project). There is no need to manually make these two to work together – everything is already done.

Workflow: add song to queue

Let’s take a quick look at workflow to see what we are talking about. Take this screenshot also as example of new workflow designer. This screenshot is taken simply by clicking on flowchart and selecting Copy to clipboard. So it is very easy to add workflow diagrams also to your documents, wikis, etc.

Workflow: Add song to queue
Click on image to see at original size.

The workflow is simple. It takes artist and song name in its first step and these parameters are coming in through WCF service. Workflow itself is just XML file. If song is not found then workflow returns “Song not found” message. If song exists then workflow checks if song is already in queue. If it is then “Song already queued” message will be returned. If song is not in queue then song will be queued and “Song added to queue” message will be returned.

Bridging between WCF and WF

First activity of workflow is “Receive message from service”. This activity is bridge between WCF service and workflow for input data. If you click on “View paramters” part of receive activity in sample application you can see dialog like this.

Receive activity parameters

I defined input parameters artist and song. Also I defined global parameters with same name in flowchart scope. This way I can use input parameters everywhere in my workflow.

Using static method in FlowDecision

The next interesting piece in workflow is how to use external classes in decisions. Click on decision, press F4 to open properties window and click on ellipses right after Condition. You should see something like this.

Flowchart Decision with expression
Click on image to see it at original size.

Condition is boolean condition. PlaylistClient is my custom class that serves as bridge between workflow and external system or data source. You can see that I can use parameters defined in flowchart scope. We will see my methods as next thing.

PlaylistClient – bridge between workflow and playlist

As this kind of workflow needs to communicate with external code because information about songs and queue is located in some other system. To play with this scenario I created simple class with static methods. You can put some real logic there to try out scenarios with real data or real systems.


public class PlaylistClient
{
    public static bool SongInQue(string artist, string song)
    {
        return false;
    }

    public static bool SongExists(string artist, string song)
    {
        return true;
    }

    public static void AddToQueue(string artist, string song)
    {

    }
}

I made these methods static as they are basically functions that retrieve data from some other source and they don’t carry any logic. These methods are just gateways to other system for workflow.

Adding songs to queue

Now let’s see how to call AddToQueue() method form workflow. This is done by using InvokeMethod activity. InvokeMethod activity is perfect fit – it is able to call static and instance methods and also provide methods with parameters.

InvokeMethod activity with method parameters
Click on image to see it at original size.

After adding song to queue workflow finishes it’s job and returns message to user.

Testing workflow

Select workflow before hitting F5You can use Visual Studio 2010 to test how workflow works with WCF. Visual Studio has WCF Test Client that is able to connect to WCF services and it lets you to call service methods with parameters you manually provide.

NB! You must select service in solution explorer before hitting F5. If you don’t then project is run as usual web application on built-in web server. Take a look at image on right.

When you run your service then WCF Test Client will be opened and is shows you structure of your service in left pane. If you select some method then method call editor will be shown in right pane.

Here is the screenshot with sample request and response. Notice the second line in request definition. When you open RequestSong() method then there is now value in Value column. You have to select this tempuri.org.RequestSong from dropdown to see artist and song selection.

WCF Test Client: Testing RequestSong workflow
Click on image to see it at original size.

Conclusion

It is not hard to create web based workflows using Visual Studio 2010 project template called WCF Workflow Service Application. A lot of things are handled and managed automatically for user and developers can focus on functionality they have to create. Visual Studio 2010 tools are enough in most cases and these tools are fast and easy to use.

Sample solution

MyWcfWorkflowService.zip MyWcfWorkflowService.zip
VS2010 | 30KB

Comments

habdulrauf said:

Thanks, nice stuff.

# August 13, 2010 1:44 AM

weblogs.asp.net said:

Example creating workflow to request songs from radio station.. Corking :)

# June 21, 2011 9:35 AM

Poyraz said:

HiDoes this workflow also  supprot  the MS CRM pre-filtered report? e.g. CRM_FilterText etc.I mean to say that, can my existing & built  in crm report can also be exported through this workflow, if yes how can i pass parameters of current record e.g Report for Current Account (using CRM_FilterText)Thanks.

# August 27, 2012 11:06 AM