Introduction
The code below will help you retrieve a DataSet
object from a Workflow Foundation Instance synchronously using properties.
Background
This article is in contrast to all the heavy plumbing work we do in Workflow Foundation to return a Dataset
.
Using the Code
Let me begin the code straight away.
- Create an Empty Solution. Add an ASP.NET project to it.
- On the Default page, Add a
GridView
or any other data bound control you like. - Add another project, WWF Sequential Library to the solution.
- Add only a single Code Activity to the designer of WWF.
- Name the Handler of the CodeActivity "
CallMe
". - Open your Workflow1.designer.cs file.
- Right after the class name, add a
public
Property with return type DataSet
, name it Result
. - Open the Workflow.cs file, inside the
partial
class, find the method we defined CallMe
, add the below code to CallMe
.
string conn = ConfigurationManager.ConnectionStrings["Database"].ConnectionString;
SqlConnection sqlcon = new SqlConnection(conn);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("select top 10 firstname,
lastname from Employees", sqlcon);
da.Fill(ds);
Result = ds;
Now, you are done with the Workflow1.cs class. After this, add appropriate ConnectionString
Section to the config file to retrieve the connection string.
- Compile your
Workflow
Library. - Now switch back to your ASP.NET application.
- Add Global.ascx file to it. Add the below code to the
Application_Start
:
System.Workflow.Runtime.WorkflowRuntime workflowRuntime
= new System.Workflow.Runtime.WorkflowRuntime();
System.Workflow.Runtime.Hosting.ManualWorkflowSchedulerService manualService
= new System.Workflow.Runtime.Hosting.ManualWorkflowSchedulerService();
workflowRuntime.AddService(manualService);
workflowRuntime.StartRuntime();
Application["WorkflowRuntime"] = workflowRuntime;
- Now add the below code to
Application_End
.
System.Workflow.Runtime.WorkflowRuntime workflowRuntime
= Application["WorkflowRuntime"] as System.Workflow.Runtime.WorkflowRuntime;
workflowRuntime.StopRuntime();
- That's it. Open your Default.aspx.cs file.
- Reference the Workflow sequential Library you created before in the web application along with
System.Workflow.Runtime
, System.Workflow.Runtime.Hosting,System.Workflow.Activities; System.Threading, System.Data
namespaces to your file. - Copy and paste the following code to your code behind:
public partial class _Default : System.Web.UI.Page
{
WorkflowRuntime workflowRuntime;
AutoResetEvent autoWaitHandler = new AutoResetEvent(false);
protected void Page_Load(object sender, EventArgs e)
{
try
{
workflowRuntime = Application["WorkflowRuntime"] as WorkflowRuntime;
ManualWorkflowSchedulerService scheduler
= workflowRuntime.GetService(typeof(ManualWorkflowSchedulerService))
as ManualWorkflowSchedulerService;
workflowRuntime.WorkflowCompleted
+= new EventHandler<WorkflowCompletedEventArgs>
(workflowRuntime_WorkflowCompleted);
WorkflowInstance instance
= workflowRuntime.CreateWorkflow(typeof(ASPNetWorkflow.Workflow1));
instance.Start();
scheduler.RunWorkflow(instance.InstanceId);
autoWaitHandler.WaitOne();
}
catch (Exception ex)
{
Response.Write("Error :" + ex.Message);
}
}
void workflowRuntime_WorkflowCompleted
(object sender, WorkflowCompletedEventArgs e)
{
if (e.OutputParameters.ContainsKey("Result"))
{
DataSet ds = (DataSet)e.OutputParameters["Result"];
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
autoWaitHandler.Set();
}
}
That is all. Set your web application as the Start up project and run it.
History
- 21st January, 2011: Initial post