0
votes

I have created an activity to return dataset as below:

public class DbQueryDataSet : AsyncCodeActivity<DataSet>
    {
        // private variables
        IDictionary<string, Argument> parameters;
        DbHelper dbHelper;


        // public arguments
        [DefaultValue(null)]
        public InArgument<string> ProviderName { get; set; }

        [DefaultValue(null)]
        public InArgument<string> ConnectionString { get; set; }

        [DefaultValue(null)]
        public InArgument<string> ConfigName { get; set; }

        [DefaultValue(null)]
        public CommandType CommandType { get; set; }

        [RequiredArgument]
        public InArgument<string> Sql { get; set; }

        [DependsOn("Sql")]
        [DefaultValue(null)]
        public IDictionary<string, Argument> Parameters
        {
            get
            {
                if (this.parameters == null)
                {
                    this.parameters = new Dictionary<string, Argument>();
                }
                return this.parameters;
            }
        }


        /*public DbQueryDataSet()
        {
            this.CommandType = CommandType.Text;
        }*/

        protected override IAsyncResult BeginExecute(AsyncCodeActivityContext context, AsyncCallback callback, object state)
        {
            // configure the helper object to access the database
            dbHelper = new DbHelper();
            dbHelper.ConnectionString = this.ConnectionString.Get(context);
            dbHelper.ProviderName = this.ProviderName.Get(context);
            dbHelper.ConfigName = this.ConfigName.Get(context);
            dbHelper.Sql = this.Sql.Get(context);
            dbHelper.CommandType = this.CommandType;
            dbHelper.Parameters = this.parameters;
            dbHelper.Init(context);

            // create the action for doing the actual work
            Func<DataSet> action = () => dbHelper.GetDataSet();
            context.UserState = action;

            return action.BeginInvoke(callback, state);
        }

        protected override DataSet EndExecute(AsyncCodeActivityContext context, IAsyncResult result)
        {
            Func<DataSet> action = (Func<DataSet>)context.UserState;
            DataSet dataSet = action.EndInvoke(result);


            // dispose the database connection
            dbHelper.Dispose();
            Result.Set(context, dataSet);
            // return the state
            return dataSet;
        }
    }

Then created a workflow sample and dropped this activity on workflow and set the required properties. But when I am trying to access this activity to get dataset as a result, it is returning nothing. If I debug out the code, then it goes in the EndExecute method and fills the dataset properly. but nothing is returned to the workflow from where we are calling it, below is code used in program.cs of workflow application sample:

    Activity oActivity = new Workflow1();
    Dictionary<string, object> result = WorkflowInvoker.Invoke(oActivity);

Here it returns 0 keys in dictionary object result. Can anyone help on how can I get Dataset back here?
I am using .Net 4.0

2

2 Answers

0
votes

WorkflowInvoker.Invoke() returns the OutAurgument and InOutArguments from the Workflow.

I think you just have add an Argument with a direction of "Out" and hook up the result of your activity with that argument.

0
votes

John Vottero is correct but has not supplied all the steps explicitly.

Create an In/Out Argument in your workflow called myWorkflowArgument (for example).

Create an In/Out Argument in your activity called myActivityArgument.

At the workflow level, in the properties for your activity, add myWorkflowArgument to the field for myActivityArgument

Now make sure that your activity code fills myActivityArgument. This will mean that the values of myActivityArgument are available at workflow level as myWorkflowArgument.

You can then use myWorkflowArgument with other activities or retrieve the value from Dictionary result, when the workflow completes using myWorkflowArgument as the key.

The argument names are up to you. I generally use the same name for the workflow argument as I use for the activity argument so that I can tell when they are supposed to hold.