workflow-core
workflow-core copied to clipboard
How to get structure of workflow?
Hi, it's possible to get the structure of workflow? For example:
{ "Id": "HelloWorld", "Version": 1, "Steps": [ { "Id": "Hello", "StepType": "MyApp.HelloWorld, MyApp", "NextStepId": "Bye" }, { "Id": "Bye", "StepType": "MyApp.GoodbyeWorld, MyApp" } ] }
It's possible to get the structure, with inputs from workflow?
Hi @jsolanas7 , yes, there is a way. Get a dependency of IWorkflowRegistry
, and assuming you keep it in a variable called workflowRegistry
, you could do:
var def = workflowRegistry.GetDefinition("HelloWorld", 1);
var result = new
{
Id = def.Id,
Version = def.Version,
Steps = def
.Steps
.Select(s => new
{
Id = s.Id,
StepType = s.BodyType.FullName,
})
.ToList(),
};
Note that here, the Id
prop of each step will be an int
, unlike things like "Hello"
and "Bye"
you've included as examples.
I assume you want to include a human-readable text to describe that step. If that's what you want to do, then you must use .Name("Hello")
in the workflow builder to give the steps you are interested in a name. Then, you could replace s.Id
with s.Name
to get that name. If you don't use the .Name("...")
method in the builder, this will be null.
Also, if you want to name things such as .If(...)
s, .While(...)
s and similar, wrapper constructs, you can only call .Name(...)
after the body of the wrapped activities. E.g.:
builder
.If(d => d.ShouldGoIntoConditionalPath)
.Do(shouldGoIntoConditionalPath => shouldGoIntoConditionalPath
.StartWith(ctx => ExecutionResult.Next())
.Name("Simulate doing stuff"))
.Name("Do stuff conditionally"); // you name the .If(...) here