Closed bkershaw closed 7 months ago
I resolved the error by changing the access modifiers of the state properties from private to public in the OrderStateMachine class.
Essentially MassTransit TransitionExtension class could not access the State properties such as "Name" resulting in a Null Reference exception.
Before
public class OrderStateMachine : MassTransitStateMachine<OrderStateInstance>
{
private readonly ILogger _logger;
// Commands
private Event<ICreateOrderMessage> CreateOrderMessage { get; set; }
// Events
private Event<IStockReservedEvent> StockReservedEvent { get; set; }
private Event<IStockReservationFailedEvent> StockReservationFailedEvent { get; set; }
private Event<IPaymentCompletedEvent> PaymentCompletedEvent { get; set; }
private Event<IPaymentFailedEvent> PaymentFailedEvent { get; set; }
// States
private State OrderCreated { get; set; }
private State StockReserved { get; set; }
private State StockReservationFailed { get; set; }
private State PaymentCompleted { get; set; }
private State PaymentFailed { get; set; }
// remaining code
}
After
public class OrderStateMachine : MassTransitStateMachine<OrderStateInstance>
{
private readonly ILogger _logger;
// Commands
private Event<ICreateOrderMessage> CreateOrderMessage { get; set; }
// Events
public Event<IStockReservedEvent> StockReservedEvent { get; set; }
public Event<IStockReservationFailedEvent> StockReservationFailedEvent { get; set; }
public Event<IPaymentCompletedEvent> PaymentCompletedEvent { get; set; }
public Event<IPaymentFailedEvent> PaymentFailedEvent { get; set; }
// States
public State OrderCreated { get; set; }
public State StockReserved { get; set; }
public State StockReservationFailed { get; set; }
public State PaymentCompleted { get; set; }
public State PaymentFailed { get; set; }
// remaining code
That was a good catch. I've made a new update based on your findings. Thanks.
I encountered an issue while following the MassTransit getting started guide. After running the containers using the provided docker-compose file, I observed that all containers started successfully except for the MassTransit Saga Orchestration State Machine container. Upon inspecting the logs, I found that the state machine crashes on startup.
Error Log Details
`Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object. at MassTransit.TransitionExtensions.TransitionTo[TSaga,TMessage](EventActivityBinder`2 source, State toState) in /_/src/MassTransit/SagaStateMachine/TransitionExtensions.cs:line 51 at SagaOrchestrationStateMachine.StateMachines.OrderStateMachine..ctor() in /Users/local/Projects/Demo/SuuCat/src/StateMachines/SagaOrchestrationStateMachine/StateMachines/OrderStateMachine.cs:line 44 at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor) at System.Reflection.ConstructorInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr) at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(Type serviceType) at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory) at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) at MassTransit.Configuration.DependencyInjectionSagaStateMachineRegistrationExtensions.SagaRegistrar`2.<>c.