appccelerate / statemachine

A .net library that lets you build state machines (hierarchical, async with fluent definition syntax and reporting capabilities).
Apache License 2.0
496 stars 129 forks source link

AsyncPasiveStateMachine dependency injection singleton #55

Closed cr0k0 closed 4 years ago

cr0k0 commented 5 years ago

I have the following line in Startup.cs:

services.AddSingleton(typeof(IAsyncStateMachine<WorkflowState, WorkflowEvent>), typeof(AsyncPassiveStateMachine<WorkflowState, WorkflowEvent>));

I'm also using some guards to execute different flows based on different flags. The first state machine workflow execution runs without any issues but if I try to execute the workflow again, I'm getting an error:

Exception in workflow: The transition without guard has to be the last defined transition because state machine checks transitions in order of declaration.

Do you know what can cause this?

ursenzler commented 5 years ago

When you define transitions with guards, the transition without a guard has to be the last one.

So in your state machine definition, where you define the transitions, there must somewhere be at least one transition with a guard and one without a guard. Define the ones with guards first. Also, make sure that you define the transitions only once and not within every request (because you added the state machine as a singleton that survives multiple requests).

In the next version of the state machine, there will be the possibility to store the definition of the state machine as a singleton and the create a state machine from it in the request.

cr0k0 commented 5 years ago

Thank you for your answer. Also, is it possible to “reset” the state of the state machine to the first (or a custom) state after the workflow is completed?

ursenzler commented 5 years ago

I think it would be best to start with a new machine for a new workflow. Alternatively, you could introduce a global reset transition that goes back to the initial state. However, beware of history state in case you used super states.

ursenzler commented 5 years ago

In v5, it will be much easier to spawn new machines from a already given definition.