Creating WCF Service using Workflow 4.0 Beta 2

Posted by: Mahesh Sabnis , on 1/5/2010, in Category Windows Communication Foundation (WCF)
Views: 34171
Abstract: By now most of you might have started using VS 2010 Beta 2. In this article we will see how to create a WCF service using Workflow 4.0 Beta 2. This article is based on a Beta version and could possibly change in the future. Assuming you have installed VS 2010 Beta 2, Open VS 2010 and create a Workflow project. Following are the activities provided
By now most of you might have started using VS 2010 Beta 2. In this article we will see how to create a WCF service using Workflow 4.0 Beta 2. This article is based on a Beta version and could possibly change in the future. Assuming you have installed VS 2010 Beta 2, Open VS 2010 and create a Workflow project. Following are the activities provided:

The activities shown above are used to develop various categories of applications. In this article, we will see how to expose WCF service using Messaging activities provided by Workflow 4.0.
Step 1: Open VS 2010 and create a blank solution > name it as ‘WF40_CreatingWCF_Service’. In this solution, right click and add a new ‘WCF Workflow Application’, name it as ‘WCF_DataService’ as shown below:
Once you add this project, you will get the following Workflow in designer as shown below. This consists of the ‘Receive’ and ‘SendReply’ activities. These activities are present under namespace ‘System.ServiceModel.Activities’. The workflow is as shown below:
This WCF service has the extension ‘xamlx’. This is newly provided by .NET in VS 2010. The Receive Activity already defines ‘GetData’ operation for the WCF service. On opening the property of the Receive activity, you will see a UI as shown below:
- ‘ServiceContractName’ defines ‘IService’ service contract to be published by the WCF service.
- ‘CanCreateInstance’ defines activation for the WCF service.
- ‘Protection Level’ defines message protection e.g. Sign, EncryptedAnSign.
- ‘SerializationOption’ defines the default serialization for user defined classes from WCF service to consumer.
- ‘Content’ property defines the input values contained into the message when client application of WCF makes request message to WCF service.
Step 2: Right click on the ‘WCF_DataService’ and add the following class ‘clsDataAccess’ and write the following code in it:
public class Employee
    public int EmpNo { get; set; }
    public string EmpName { get; set; }
    public int Salary { get; set; }
    public int DeptNo { get; set; }
public class clsDataAccess
    public Employee[] GetAllEmployee()
        Employee[] arrEmp = null;
        SqlConnection Conn = new SqlConnection("Data Source=.;Initial Catalog=Company;Integrated Security=SSPI");
        SqlCommand Cmd = new SqlCommand();
        Cmd.Connection = Conn;
        Cmd.CommandText = "Select * from Employee";
        SqlDataReader Reader = Cmd.ExecuteReader();
        DataTable DtEmp = new DataTable();
        arrEmp = new Employee[DtEmp.Rows.Count];
        int i = 0;
        foreach (DataRow Dr in DtEmp.Rows)
            arrEmp[i] = new Employee();
            arrEmp[i].EmpNo = Convert.ToInt32(Dr["EmpNo"]);
            arrEmp[i].EmpName = Dr["EmpName"].ToString();
            arrEmp[i].Salary = Convert.ToInt32(Dr["Salary"]);
            arrEmp[i].DeptNo = Convert.ToInt32(Dr["DeptNo"]);
            i = i + 1;
        return arrEmp;
Public Class Employee
      Private privateEmpNo As Integer
      Public Property EmpNo() As Integer
                  Return privateEmpNo
            End Get
            Set(ByVal value As Integer)
                  privateEmpNo = value
            End Set
      End Property
      Private privateEmpName As String
      Public Property EmpName() As String
                  Return privateEmpName
            End Get
            Set(ByVal value As String)
                  privateEmpName = value
            End Set
      End Property
      Private privateSalary As Integer
      Public Property Salary() As Integer
                  Return privateSalary
            End Get
            Set(ByVal value As Integer)
                  privateSalary = value
            End Set
      End Property
      Private privateDeptNo As Integer
      Public Property DeptNo() As Integer
                  Return privateDeptNo
            End Get
            Set(ByVal value As Integer)
                  privateDeptNo = value
            End Set
      End Property
End Class
Public Class clsDataAccess
      Public Function GetAllEmployee() As Employee()
            Dim arrEmp() As Employee = Nothing
            Dim Conn As New SqlConnection("Data Source=.;Initial Catalog=Company;Integrated Security=SSPI")
            Dim Cmd As New SqlCommand()
            Cmd.Connection = Conn
            Cmd.CommandText = "Select * from Employee"
            Dim Reader As SqlDataReader = Cmd.ExecuteReader()
            Dim DtEmp As New DataTable()
            arrEmp = New Employee(DtEmp.Rows.Count - 1){}
            Dim i As Integer = 0
            For Each Dr As DataRow In DtEmp.Rows
                  arrEmp(i) = New Employee()
                  arrEmp(i).EmpNo = Convert.ToInt32(Dr("EmpNo"))
                  arrEmp(i).EmpName = Dr("EmpName").ToString()
                  arrEmp(i).Salary = Convert.ToInt32(Dr("Salary"))
                  arrEmp(i).DeptNo = Convert.ToInt32(Dr("DeptNo"))
                  i = i + 1
            Next Dr
            Return arrEmp
      End Function
End Class
The above class contains the method ‘GetAllEmployee’ which returns an array of all employees of type ‘Employee’ class.
Step 3: Rename ‘Service1.xamx’ to ‘CService.xamlx’. Drag and drop the ‘InvokeMethod’ activity from ‘Primitives’ activity from the toolbox, right below the ‘ReceiveRequest’ activity. Change the OperationName of the ‘ReceiveRequest’ activity from ‘GetData’ to ‘GetAllEmp‘. From the lower left corner of the designer, you will find ‘Variables’ > click on it and add the following variable types:
(Note: All expression types in WF 4.0 accept code with VB Syntax)
‘objData’ is defined to make a call using the ‘InvokeMethod’ activity to the clsDataAccess’ class. ‘arrEmp’ is declared since it is the return type from the method.
Step 4: Set following properties of the ‘InvokeMethod’ activity:
Property Name
Now the workflow will be as shown below:
Step 5: Right click on ‘CService.xamlx’ and click on ‘View In Browser’. The following output should be displayed:
Step 6: Build the project and publish it on IIS. (Note that the virtual directory is in the application pool configured for ASP.NET 4.0).
Now you can create a client application where you can consume this WCF service. The important thing here is that it uses ‘basicHttpBinding’ by default for communication (it is the default used in WCF 4.0). So if you want to have a secure communication, you must make changes in the Web.Config file.
Conclusion: VS2010 beta has provided codeless WCF exposure using WF 4.0. For the developer, it is now necessary to just design the Data access layer and business layer using WCF template (WF 4.0) which makes it easier to make the business logic distributable with less efforts. The entire source code of this article can be downloaded over here

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Mahesh Sabnis is a DotNetCurry author and a Microsoft MVP having over two decades of experience in IT education and development. He is a Microsoft Certified Trainer (MCT) since 2005 and has conducted various Corporate Training programs for .NET Technologies (all versions), and Front-end technologies like Angular and React. Follow him on twitter @maheshdotnet or connect with him on LinkedIn

Page copy protected against web site content infringement 	by Copyscape

Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by dotted on Wednesday, January 6, 2010 12:00 AM
Great stuff. The image looks a bit stretched.
Comment posted by Sameer on Thursday, January 7, 2010 9:36 PM
Thank you. This article was easy to read and understand.