Recent Posts

Posts RSS

Getting started with Windows Workflow Foundation (WWF)

Reactions: 

Windows Workflow Foundation (WWF) is one of the key pillars of the .NET Framework 3.0 (formerly known as WinFX), next to Windows Presentation Foundation (WPF), Windows Communication Foundation (WCF) and Windows CardSpace (WCS).

Now we ready to start a simple WWF example which demonstrated as bellow in steps. This example I demonstrated by using VS 2008 but you can do it in VS 2005 also by downloading the template from the bellow link:

  
STEP: 1 Start Visual Studio 2008 and select create new project and Choose for a Sequential Workflow Console Application. Let's named it HelloWorkflow . In the solution explorer, delete Workflow1.cs. Although this is not necessary it's interesting to create the workflow in another way, using "code separation" with XOML (which stands for eXtensible Object Markup Language).

Click on image to see in big size

STEP: 2 Then add a new item to the project and choose for Sequential Workflow (with code separation) and named it HelloWorkflow. As you can see, it has the extension .xoml.


Click on image to see in big size


STEP: 3 Workflows consist of activities that are executed in a well-defined order by the workflow runtime engine. These activities can be found in the Visual Studio 2008 toolbox, but it's also possible to create your own activities.The tool box looks like as follows:

Click on image to see in big size

STEP: 4 For this demo, drag and drop a Code (Activity) to the "Drop Activities to create a Sequential Workflow" part in the designer. Using the properties grid, change the name of the new activity from codeActivity1 to sayHello. The result should look like this:

Click on image to see in big size

The red exclamation mark tells you there's still something wrong with the activity's configuration. More specifically, the designer tells you "Property 'ExecuteCode' is not set.". Basically, ExecuteCode is an event handler which can be created by double-clicking on the code activity. (Note: for other activities too, always use the red exclamation mark assistance to configure an activity properly). The code for our activity will consist of a simple Console.WriteLine() method.

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;

namespace HelloWorkflow
{
    public partial class HelloWorkflow : SequentialWorkflowActivity
    {
        private void sayHello_ExecuteCode(object sender, EventArgs e)
        {
            Console.WriteLine("Hello, Workflow..!");
            Console.WriteLine("Please press any KEY to continue....");
            Console.ReadLine();
        }
    }
}

Notice the use of a partial class. The other part of the HelloWorkflow definition lives in the .xoml file. Right click the HelloWorkflow.xoml file in the Solution Explorer, choose Open With... and select the XML Editor:

<SequentialWorkflowActivity x:Class="HelloWorkflow.HelloWorkflow" x:Name="HelloWorkflow" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/workflow">
  <CodeActivity x:Name="sayHello" ExecuteCode="sayHello_ExecuteCode" />
SequentialWorkflowActivity>

STEP: 5 In order to execute a workflow, one need to host the workflow engine. However, no worries: Visual Studio 2008 has generated the code for us in Program.cs. It needs a little modification however because we did remove Workflow1.cs and replaced it by HelloWorkflow.cs. Here's the modified Main code (change indicated in yellow marked):

namespace HelloWorkflow
{
    class Program
    {
        static void Main(string[] args)
        {
            using(WorkflowRuntime workflowRuntime = new WorkflowRuntime())
            {
                AutoResetEvent waitHandle = new AutoResetEvent(false);
                workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e) {waitHandle.Set();};
                workflowRuntime.WorkflowTerminated += delegate(object sender, WorkflowTerminatedEventArgs e)
                {
                    Console.WriteLine(e.Exception.Message);
                    waitHandle.Set();
                };

                WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(HelloWorkflow));
                instance.Start();

                waitHandle.WaitOne();
            }
        }
    }
}
This code initializes the WorkflowRuntime and starts the workflow by calling the CreateWorkflow method. The anonymous method plumbing keeps the console application alive till the workflow has executed and also reports exceptions that terminated the workflow.


STEP: 6 Now we are ready to see the output of our effort. Press Ctrl+F5




Click on image to see in big size


Hope It Helps…!



kick it on DotNetKicks.com

0 comments

A Silverlight Application By Using ADO.NET DataEntities and WCF Service

Reactions: 

Before we start this application we have to install the following pre-requisite software:
  
  1. Visual Studio 2008 with SP1 or Visual Studio 2010
  2. SQL Server 2005 or later version
  3. Silverlight 3 or later version with full toolkits

After installed the above pre-requisites we are now ready to start our application.

STEP: 1. Ready the database which will be use in our application. Here I am using the local database called DG in which Tab_Emp is the table.

STEP: 2. Create a new Silverlight project, named SilverlightWcfEntities, and accept the default of creating an ASP.NET Web Application.

Visual Studio will create one solution with two projects:

  • SilverlightWcfEntities (the Silverlight project)
  • SilverlightWcfEntities.Web (the Web project)
The second project will house both the ADO.NET Entity model and the web service, while the first will hold the Silverlight DataGrid.

STEP: 3. To create the Data Model, right click on SilverlightWcfEntitities (the web project) and select Add >>> New Item. Choose Ado.Net Entity Data Model, name it EmployeeDataModel.edmx and click the Add button.
In the next dialog click on the Generate from Database button – this will, ultimately, because the data model to be created for you, a wonderful thing saving you endless boring work. The next dialog will ask you to choose the data connection you’d like to use.

And once the connection is made and the data structure retrieved, you’ll be asked to select which tables, views and stored procedures you’d like included in your model. Here I selected just one table: Tab_Emp. We can select stored procedure and views also.

When we click Finish, our entity data model is generated. Spend a couple minutes looking at the result, it is quite cool. Note there may be a window hiding at the bottom of Visual Studio named Mapping Details. Examine the result of that window.


STEP: 4. With the Entity Data Model in place we’re ready to create a web service to make the data available to the Silverlight application that will be running on the client.

Once again right-click on SilverlightWcfEntitities (the web project) and chose Add >>> New Item, this time selecting Silverlight-Enabled WCF Service. Name the new service EmployeeWebService and click Add

Open EmployeeWebService.svc.cs where we will find a stub method, DoWork() which we will modify to fetch the list of employees.

[ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class EmployeeWebService
    {
        [OperationContract]
        public List<Tab_Emp> GetEmployees()
        {
            EmployeeEntities empEntity = new EmployeeEntities();

            return empEntity.Tab_Emp.Where(emp => emp.DeptName.Equals("Developer")).ToList();
        }

        // Add more operations here and mark them with [OperationContract]
    }

The first line of the GetEmployees() method creates an instance of the Data Entity that we defined earlier.

The second line asks the data entity instance for the Employee objects, but since there are a lot of them, it adds a where clause, using a lambda expression to narrow the return set and then calls ToList() on the result effectively casting the result set to a List.


STEP: 5. Lets now move to silverlight application named SilverlightWcfEntities (the Silverlight project) and right click on that project and select reference >>> Add Service Reference. On that dialog click on Discover. The Web Service we created earlier (EmployeeWebService) will appear in the list. Click on it and then rename the namespace to EmployeeWebService




After getting the service reference we’ll start working on MainPage.xaml of the SilverlightWcfEntities (the Silverlight project). First we drag a datagrid from the toolbox which makes the code like this:

<UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  x:Class="EntitiesWebSvcGrid.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
    <Grid x:Name="LayoutRoot" Background="White">
       <data:DataGrid x:Name="dg" >data:DataGrid>
    Grid>
UserControl>

Then switch to the MainPage.xaml.cs and there we have to to create an instance of the web service client and then use that client to call the GetEmployees method on the web service. Write this method in Page_Loaded() event.

public MainPage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(Page_Loaded);
        }

        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            EmployeeWebService.EmployeeWebServiceClient ws = new EntitiesWebSvcGrid.EmployeeWebService.EmployeeWebServiceClient();
           
        }

Because Silverlight applications run in the browser, however, we cannot make direct method calls (you must not block the browser) but rather must make asynchronous calls. In fact, Intellisense not only offers an async alternative, it does not even offer the synchronous method we created. So we have to call the GetEmployeeAsync method for that. After which we have to define another event called GetEmployeeCompleted where we set the data source for our grid in runtime.

So please don’t worry just compare your whole code as shown bellow:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace EntitiesWebSvcGrid
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(Page_Loaded);
        }

        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            EmployeeWebService.EmployeeWebServiceClient ws = new EntitiesWebSvcGrid.EmployeeWebService.EmployeeWebServiceClient();
            ws.GetEmployeesCompleted += new EventHandlerGetEmployeesCompletedEventArgs>(ws_GetEmployeesCompleted);
            ws.GetEmployeesAsync();
        } 
        void ws_GetEmployeesCompleted(object sender, EntitiesWebSvcGrid.EmployeeWebService.GetEmployeesCompletedEventArgs e)
        {
            dg.ItemsSource = e.Result;
        }
    }
}

Now its its time to see the output of the whole process. So just press F5 and here we go…




Note: number of rows may be differing according to the data you have.

Hope It Helps..!

kick it on DotNetKicks.com

0 comments