Skip to content

Instantly share code, notes, and snippets.

@phatboyg
Last active January 28, 2019 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phatboyg/90fb6fd765bd0bc2f7d2c4abeefbf86f to your computer and use it in GitHub Desktop.
Save phatboyg/90fb6fd765bd0bc2f7d2c4abeefbf86f to your computer and use it in GitHub Desktop.
Showing that exception works
using System;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using Automatonymous;
using MassTransit;
using MassTransit.Saga;
namespace StateMachineTest
{
class Program
{
static async Task Main(string[] args)
{
TestStateMachine stateMachine = new TestStateMachine();
InMemorySagaRepository<TestInstance> sagaRepository = new InMemorySagaRepository<TestInstance>();
var busControl = Bus.Factory.CreateUsingInMemory(cfg =>
{
cfg.ReceiveEndpoint("input-queue", e => { e.StateMachineSaga(stateMachine, sagaRepository); });
});
await busControl.StartAsync();
try
{
await busControl.Publish<Started>(new
{
CorrelationId = NewId.NextGuid()
});
await Task.Delay(5000);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
finally
{
await busControl.StopAsync();
}
}
}
public class TestInstance :
SagaStateMachineInstance
{
public Guid CorrelationId { get; set; }
public State CurrentState { get; set; }
}
public class TestStateMachine :
MassTransitStateMachine<TestInstance>
{
public TestStateMachine()
{
Initially(
When(InstanceStarted)
.Then(context => Console.WriteLine("Started"))
.Then(context => throw new ValidationException("Well, that sucks"))
.ThenAsync(context => Console.Out.WriteLineAsync("Still started"))
.TransitionTo(Running)
.Publish(context => new TestInstanceCreated())
.Catch<ValidationException>(ex => ex
.Then(context => Console.WriteLine("Bad things happened"))
.TransitionTo(Faulted)));
}
public State Running { get; private set; }
public State Faulted { get; private set; }
public Event<Started> InstanceStarted { get; private set; }
}
public class TestInstanceCreated
{
}
public interface Started :
CorrelatedBy<Guid>
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment