Silverlight 4: Consuming Workflow Service
Posted by: Mahesh Sabnis
in Category Silverlight 2, 3, 4 and 5
Abstract: This article demonstrates how to expose your Workflow based business processes as a Service and use a Browser client like Silverlight 4 to subscribe to it
While discussing Workflow 4.0 integration with WCF in one of my recent training programs, I was asked a question about consuming a WCF Workflow service in Silverlight. Initially I thought that it won’t be possible, because the “xamlx” extension of the WCF Workflow Service may not be accepted by the Silverlight client application. But it was an interesting question. So I decided to try it and it worked! I am sharing the solution in the form of this article.
The scenario we are discussing is as shown below:
The advantage of using the Workflow Services is that, since Workflow targets the business process applications, organization can create and publish these core processes based functionality and their client can subscribe to them. Since Silverlight applications provide a lightweight Rich Browser application development capability, its integration with Workflow Services provides additional capability for Rich UX and SOA based applications.
Creating Workflow 4.0 Service
Step 1: Open VS2010 and create blank solution, name it as ‘WF_WCF_Service’. In this solution, add a WCF Workflow Service Application, name it as ‘WF_WCF_Sample_Serv’. You will see the ‘Sequence’ activity with ‘Send’ and ‘SendReplyToReceive’.
Step 2: In this project, add a new class file, name it as ‘DataClasses.cs’ and add the following class in it:
Step 3: In the Workflow Designer, on the lower left corner, locate ‘Variables’ and delete the default ‘data’ variable and add a new ‘Employees’ variable of the type ‘EmployeeCollection’ as below:
Step 4: Change properties of the activity as below:
The Workflow design will be as shown below:
Step 5: Publish the app on the IIS 7.5 web server. (Note: The Web site where the Workflow Service is hosted must have clientaccesspolicy.xml as shown below)
Creating The Silverlight Client Application
Step 1: In the same solution, add a new Silverlight client application and name it as ‘SL4_ClientApp’. Add the Workflow Service reference in this project.
Step 2: Open MainPage.xaml and add a DataGrid and Button in it. Make sure that the ‘AutoGeneratedColumns’ property of the DataGrid is set to ‘True’. The UI will be as below:
Step 3: Write the following code in the Click event of the ‘Get Data’ Button:
The above code makes an asynchronous call to Workflow Service just like calling a normal WCF service.
Step 4: Run the application and click on the ‘Get Data’ button. The result will be as below:
Conclusion: The above facility helps any organization to expose their Workflow based business processes as a Service and the Browser client like Silverlight can subscribe to it.
The entire source code of this article can be downloaded over here