Add activity output example

This commit is contained in:
Sipke Schoorstra 2019-10-24 17:56:36 +02:00
parent c7c41d10a5
commit dc0a68c37d
6 changed files with 117 additions and 0 deletions

View file

@ -87,6 +87,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elsa.Persistence.DocumentDb
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample16", "src\samples\Sample16\Sample16.csproj", "{DFE72CF4-C77C-4932-A00F-A8D325E603CE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample17", "src\samples\Sample17\Sample17.csproj", "{8854DF17-D566-49C9-8210-68AB8F5659FA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -221,6 +223,10 @@ Global
{DFE72CF4-C77C-4932-A00F-A8D325E603CE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DFE72CF4-C77C-4932-A00F-A8D325E603CE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DFE72CF4-C77C-4932-A00F-A8D325E603CE}.Release|Any CPU.Build.0 = Release|Any CPU
{8854DF17-D566-49C9-8210-68AB8F5659FA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8854DF17-D566-49C9-8210-68AB8F5659FA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8854DF17-D566-49C9-8210-68AB8F5659FA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8854DF17-D566-49C9-8210-68AB8F5659FA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -263,6 +269,7 @@ Global
{79E85459-EF54-44D6-9508-1E68B8238D00} = {B43B546E-23F3-46E8-ACB7-D04F05CDA180}
{8C51A528-F9B5-464E-AAD1-1ADC44697085} = {600C4AE0-0585-4181-9CEC-2BB43430C713}
{DFE72CF4-C77C-4932-A00F-A8D325E603CE} = {5E5E1E84-DDBC-40D6-B891-0D563A15A44A}
{8854DF17-D566-49C9-8210-68AB8F5659FA} = {5E5E1E84-DDBC-40D6-B891-0D563A15A44A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8B0975FD-7050-48B0-88C5-48C33378E158}

View file

@ -0,0 +1,38 @@
using System.Threading;
using System.Threading.Tasks;
using Elsa.Expressions;
using Elsa.Extensions;
using Elsa.Results;
using Elsa.Services;
using Elsa.Services.Models;
using Sample17.Models;
namespace Sample17.Activities
{
public class CreateDocument : Activity
{
private readonly IWorkflowExpressionEvaluator expressionEvaluator;
public CreateDocument(IWorkflowExpressionEvaluator expressionEvaluator)
{
this.expressionEvaluator = expressionEvaluator;
}
public WorkflowExpression<string> TitleExpression
{
get => GetState<WorkflowExpression<string>>();
set => SetState(value);
}
protected override async Task<ActivityExecutionResult> OnExecuteAsync(
WorkflowExecutionContext context,
CancellationToken cancellationToken)
{
var title = await expressionEvaluator.EvaluateAsync(TitleExpression, context, cancellationToken);
var document = new Document { Title = title};
Output["Document"] = document;
return Done();
}
}
}

View file

@ -0,0 +1,20 @@
using Elsa.Activities.Console.Activities;
using Elsa.Expressions;
using Elsa.Services;
using Elsa.Services.Models;
using Sample17.Activities;
namespace Sample17
{
public class CreateDocumentWorkflow : IWorkflow
{
public void Build(IWorkflowBuilder builder)
{
builder
.StartWith<WriteLine>(x => x.TextExpression = new LiteralExpression("Enter a title"))
.Then<ReadLine>(id: "DocumentTitleInput")
.Then<CreateDocument>(x => x.TitleExpression = new JavaScriptExpression<string>("DocumentTitleInput.Input"), id: "CreateDocument")
.Then<WriteLine>(x => x.TextExpression = new JavaScriptExpression<string>("`A new document was created with title \"${CreateDocument.Document.Title}\"`"));
}
}
}

View file

@ -0,0 +1,7 @@
namespace Sample17.Models
{
public class Document
{
public string Title { get; set; }
}
}

View file

@ -0,0 +1,30 @@
using System;
using System.Threading.Tasks;
using Elsa.Activities.Console.Extensions;
using Elsa.Services;
using Microsoft.Extensions.DependencyInjection;
using Sample17.Activities;
namespace Sample17
{
internal static class Program
{
private static async Task Main(string[] args)
{
var services = BuildServices();
var workflowInvoker = services.GetRequiredService<IWorkflowInvoker>();
await workflowInvoker.StartAsync<CreateDocumentWorkflow>();
}
private static IServiceProvider BuildServices()
{
return new ServiceCollection()
.AddWorkflows()
.AddWorkflow<CreateDocumentWorkflow>()
.AddConsoleActivities()
.AddActivity<CreateDocument>()
.BuildServiceProvider();
}
}
}

View file

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\activities\Elsa.Activities.Console\Elsa.Activities.Console.csproj" />
<ProjectReference Include="..\..\core\Elsa.Core\Elsa.Core.csproj" />
<ProjectReference Include="..\..\persistence\Elsa.Persistence.MongoDb\Elsa.Persistence.MongoDb.csproj" />
</ItemGroup>
</Project>