private static Activity CreateWf() { // variables. var pipeline = new DelegateInArgument<PSObject>() { Name = "Pipeline" }; var processName = new DelegateOutArgument<string>() { Name = "Process Name" }; var processes1 = new Variable<Collection<Process>> { Name = "Processes 1" }; var processes2 = new Variable<Collection<Process>> { Name = "Processes 2" }; var processes3 = new Variable<Collection<Process>> { Name = "Processes 3" }; var processNames = new Variable<Collection<string>> { Name = "Process Names" }; var errors = new Variable<Collection<ErrorRecord>> { Name = "Errors" }; var error = new DelegateInArgument<ErrorRecord> { Name = "Error" }; Sequence body = new Sequence() { Variables = { processes1, processes2, processes3, processNames, errors }, Activities = { // Simple PowerShell invocation. new WriteLine() { Text = "Simple PowerShell invocation. Launching notepad." }, new InvokePowerShell() { CommandText = "notepad" }, new WriteLine(), // Using PowerShell<T> to capture output. new WriteLine() { Text = "Getting process and then pass the output to a Collection." }, new InvokePowerShell<Process>() { CommandText = "Get-Process", Output = processes1, }, CreatePrintProcessActivity(processes1), // Do some post-processing after invocation. new WriteLine() { Text = "Getting the names of the processes." }, new InvokePowerShell<string>() { CommandText = "Get-Process", Output = processNames, InitializationAction = new ActivityFunc<PSObject,string> { Argument = pipeline, Result = processName, Handler = new Assign<string> { To = processName, Value = new LambdaValue<string>(ctx => ((Process) pipeline.Get(ctx).BaseObject).ToString()) } }, }, new WriteLine() { Text = new InArgument<string>(ctx => "The first process returned is: " + processNames.Get(ctx)[0]), }, new WriteLine(), // Passing data using an input pipeline. new WriteLine() { Text = "Passing data using an input pipeline and then extracting unique process names." }, new InvokePowerShell<Process>() { CommandText = "Get-Unique", Input = processes1, Output = processes2, }, CreatePrintProcessActivity(processes2), // Passing in a parameter to the command. new WriteLine() { Text = "Reverse sorting." }, new InvokePowerShell<Process>() { CommandText = "Sort-Object", Input = processes2, Output = processes3, Parameters = { { "descending", new InArgument<Boolean>(true) } } }, CreatePrintProcessActivity(processes3), // Run a command that results in errors. new WriteLine() { Text = "Returning errors." }, new InvokePowerShell<string>() { CommandText = "Write-Error", Errors = errors, Parameters = { { "message", new InArgument<string>("a short error message") } } }, new ForEach<ErrorRecord>() { Values = errors, Body = new ActivityAction<ErrorRecord> { Argument = error, Handler = new WriteLine() { Text = new InArgument<string>(ctx => "Error returned: " + error.Get(ctx).ToString()), }, } } } }; return body; }
private static Activity CreateIntResourceService() { var request1 = new DelegateInArgument<HttpRequestMessage>() { Name = "Request1" }; var response1 = new DelegateOutArgument<object>() { Name = "Response1" }; var parameters1 = new DelegateInArgument<IDictionary<string, string>> { Name = "Parameters1" }; var request2 = new DelegateInArgument<HttpRequestMessage>() { Name = "Request2" }; var response2 = new DelegateOutArgument<object>() { Name = "Response2" }; var parameters2 = new DelegateInArgument<IDictionary<string, string>> { Name = "Parameters2" }; var num = new Variable<int>() { Name = "Num" }; return new Sequence { Variables = { num }, Activities = { new HttpReceive { Method = "POST", UriTemplate = "/{Num}", CanCreateInstance = true, Body = new ActivityFunc<HttpRequestMessage, IDictionary<string, string>, object> { Argument1 = request1, Argument2 = parameters1, Result = response1, Handler = new Sequence { Activities = { new Assign { DisplayName = "Store arg in variable", To = new OutArgument<object>(num), Value = new InArgument<int>( // new VisualBasicValue<int>("Request.Content.ReadAs(Of Int32)()")) new VisualBasicValue<int>("CType(Parameters1(\"Num\"), Int32)")) } } } } }, new HttpReceive { Method = "GET", UriTemplate = "/{Num}", CanCreateInstance = true, Body = new ActivityFunc<HttpRequestMessage, IDictionary<string, string>, object> { Argument1 = request2, Argument2 = parameters2, Result = response2, Handler = new Sequence { Activities = { new Assign { DisplayName = "Assign response message", To = new OutArgument<object>(response1), Value = new InArgument<int>(num) } } } } }, } }; }
// Retrieve all roles from the database. Uses an ActivityFunc<DataReader, Role> to map the results // Performance decreases (since the mapping is done in multiple pulses) but mapping can be serialized // to Xaml and authored declaratively in the the designer. static void GetAllRolesUsingActivityFuncMapping() { DelegateInArgument<DbDataReader> reader = new DelegateInArgument<DbDataReader>() { Name = "readerInArgument" }; DelegateOutArgument<Role> roleOutArg = new DelegateOutArgument<Role>() { Name = "roleOutArgument" }; Activity dbQuery = new DbQuery<Role>() { ConfigName = "DbActivitiesSample", Sql = "SELECT * FROM Roles", MapperFunc = new ActivityFunc<System.Data.Common.DbDataReader,Role> { Argument = reader, Handler = new Sequence { Activities = { new Assign<Role> { To = roleOutArg, Value = new InArgument<Role>(c => new Role()) }, new Assign<string> { To = new OutArgument<string>(c => roleOutArg.Get(c).Code), Value = new InArgument<string>(c => reader.Get(c)["code"].ToString()) }, new Assign<string> { To = new OutArgument<string>(c => roleOutArg.Get(c).Name), Value = new InArgument<string>(c => reader.Get(c)["name"].ToString()) } } }, Result = roleOutArg } }; IDictionary<string, object> results = WorkflowInvoker.Invoke(dbQuery); IList<Role> roles = (IList<Role>)results["Result"]; foreach (Role role in roles) { Console.WriteLine(role.ToString()); } }
private static Activity CreateRequestReplyService() { var request = new DelegateInArgument<HttpRequestMessage>(); var response = new DelegateOutArgument<object>(); var parameters = new DelegateInArgument<IDictionary<string, string>> { Name = "parameters" }; return new Sequence { Activities = { new HttpReceive { Method = "GET", UriTemplate = "/{arg1}/{arg2}?q1={q1}&q2={q2}", CanCreateInstance = true, Body = new ActivityFunc<HttpRequestMessage, IDictionary<string, string>, object> { Argument1 = request, Argument2 = parameters, Result = response, Handler = new Sequence { Activities = { new Assign { DisplayName = "Assign response content", To = new OutArgument<object>(response), Value = new InArgument<string>( new VisualBasicValue<string>( "string.Format(\"Arg1 = {0}, Arg2 = {1}, Q1 = {2}, Q2 = {3}\", parameters(\"ARG1\"), parameters(\"ARG2\"), parameters(\"Q1\"), parameters(\"Q2\"))")) } } } } }, } }; }