Repository navigation
Events
Behaviors receive events by CLR type. For contracts that should be defined by name at configuration time rather than compiled into a CLR event class, Stateflows also supports structural events. Each structural event has a registered name and a set of typed fields. State Machine transitions and Activity accept-event nodes can match these events by name and receive a StructuralEvent.
Register event definitions during Stateflows configuration with AddEvents:
using System.ComponentModel.DataAnnotations;
using Microsoft.Extensions.DependencyInjection;
using Stateflows;
using Stateflows.Common;
using Stateflows.Events;
services.AddStateflows(builder => builder
.AddEvents(events => events
.AddEvent("submit-order", e => e
.AddField("orderId", FieldType.String, f =>
f.AddValidation(new RequiredAttribute()))
.AddField("quantity", FieldType.Int32, f =>
f.AddValidation(new RangeAttribute(1, 10)))
.AddObjectField("shippingAddress", address => address
.AddField("city", FieldType.String))
.AddCollectionField("tags", FieldType.String))
.AddRequest("order-status", "order-status-result", e => e
.AddField("orderId", FieldType.String))
.AddEvent("order-status-result", e => e
.AddField("status", FieldType.String))));Event and field names are case-sensitive. Primitive field types are String, Boolean, Int32, Int64, Decimal, Double, Guid, DateTime, DateTimeOffset, TimeSpan, DateOnly, TimeOnly, Binary, and Enum. Enum fields declare allowed string values with AddEnumValues. Fields can also be nested objects or collections of primitive/object values; field and item validation attributes are configured on the field builder.
Resolve IEventFactory from dependency injection and create a structural event using its registered name. Set accepts typed values; SetObject and SetObjectCollection create nested values:
var eventFactory = serviceProvider.GetRequiredService<IEventFactory>();
var request = eventFactory.Create("submit-order", e => e
.Set("orderId", "A-1042")
.Set("quantity", 2)
.SetObject("shippingAddress", address => address
.Set("city", "London"))
.Set("tags", new[] { "priority", "gift" }));
await behavior.SendAsync(request);Within a transition or accept-event callback, StructuralEvent exposes Name, Fields, ContainsField, Get<T>(fieldName), TryGet<T>(fieldName, out value), and GetCollection<T>(fieldName). Get<T> throws if a field is missing or has a different runtime type; use TryGet when a field is optional. Nested objects are read as StructuralValue.
The registry normalizes values to their declared schema and validates them before Behavior dispatch. Unknown fields are discarded during normalization. Validation failures are returned in the regular Stateflows event validation result and the event is not processed. Missing fields are not automatically rejected unless the definition includes a suitable validation attribute, such as RequiredAttribute.
Use AddRequest to declare a structural request and its response contract. The response can be another registered structural event, or a CLR response type via AddRequest<TResponse>.
var result = await behavior.RequestAsync(
eventFactory.Create("order-status", e => e.Set("orderId", "A-1042")));
var status = result.Response.Get<string>("status");The responding Behavior calls Respond on the request. For a structural response, the name and fields must satisfy the declared response definition; for a CLR response, the declared CLR response type is enforced.
Structural event names are used directly by the string overloads of AddTransition and AddAcceptEventAction. Those callbacks receive StructuralEvent, not a generated CLR event class. See Transitions and Accept Event Action Node for complete usage examples.
Home page Support Code licensed under an MIT-style License. Documentation licensed under CC BY 4.0. © by Mikołaj Milewski, 2026
Overview
Installation
Runtimes
Orleans runtime
Concepts
Events and Structural Events
Messaging Patterns
Validation
Scheduling
Observability
Behaviors
Behavior Values
Dependency Injection
Behavioral Groups
State Machines
Concepts
State Identity
In-State Event Deferrals
Evaluation of Transitions
Exception Handling
Building blocks
Regions
States
State
Composite State
Orthogonal State
Final State
Pseudostates
Choice
Junction
Fork
Join
History
Transitions
Transition
Default Transition
Internal Transition
Activities
Concepts
Exception Handling
Implicit Fork and Join
Tokens
Building blocks
Nodes
Action Node
Decision Node
Merge Node
Initial Node
Final Node
Input Node
Output Node
Fork Node
Join Node
Accept Event Action Node
Send Event Action Node
Publish Event Action Node
Time Event Action Node
Data Store Node
Structured Activity Node
Iterative Activity Node
Parallel Activity Node
Flows
Data Flow
Control Flow
Actions
Entities
Fields
Mutations
Projections
Extensions
ClearScript
FluentValidation
JSON definitions
Minimal APIs
OneOf
OpenTelemetry
PlantUML
Scheduling