Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/guide/http/sagas.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,15 @@ public static Reservation Post2(StartReservation start)
<sup><a href='https://github.com/JasperFx/wolverine/blob/main/src/Http/WolverineWebApi/SagaExample.cs#L32-L44' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_start_saga_from_http_endpoint_empty_body' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


## Returning a Saga as the Response Only

Wolverine treats *any* returned `Saga` as a new saga to insert. If the endpoint has already started the saga (say through
`IMessageBus.InvokeAsync()`) and only wants to hand the saga back as the response body, the second insert fails. Mark
the endpoint with `[SagaIsResponseOnly]` and Wolverine will skip the saga persistence for that method's return values:

```cs
[WolverinePost("/reservation3")]
[SagaIsResponseOnly]
public static async Task<Reservation> Post3(StartReservation start, IMessageBus bus) { ... }
```
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,19 @@ await Host
var @event = await result.ReadAsJsonAsync<ReservationBooked>();
@event.ReservationId.ShouldBe("dinner");
}
}

[Fact]
public async Task returned_saga_marked_response_only_is_not_inserted_again()
{
await Store.Advanced.Clean.DeleteDocumentsByTypeAsync(typeof(Reservation), TestContext.Current.CancellationToken);

var result = await Host.Scenario(x =>
{
x.Post.Json(new StartReservation("lunch")).ToUrl("/reservation3");
x.StatusCodeShouldBeOk();
});

var body = await result.ReadAsJsonAsync<Reservation>();
body.Id.ShouldBe("lunch");
}
}
18 changes: 18 additions & 0 deletions src/Http/WolverineWebApi/SagaExample.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using JasperFx.Core;
using Wolverine;
using Wolverine.Http;
using Wolverine.Persistence.Sagas;

namespace WolverineWebApi;

Expand Down Expand Up @@ -42,6 +43,23 @@ public static Reservation Post2(StartReservation start)
}

#endregion

#region sample_saga_returned_as_response_only
[WolverinePost("/reservation3")]

// The saga is started elsewhere (here by the Marten session), so this
// directs Wolverine to treat the returned Reservation as the response body only
[SagaIsResponseOnly]
public static async Task<Reservation> Post3(StartReservation start, IMessageBus bus,
global::Marten.IDocumentSession session)
{
var reservation = new Reservation { Id = start.ReservationId };
session.Store(reservation);
await session.SaveChangesAsync();
return reservation;
}

#endregion
}

public record BookReservation(string Id);
Expand Down
6 changes: 5 additions & 1 deletion src/Wolverine/Configuration/SagaPersistenceChainPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ public void Apply(IReadOnlyList<IChain> chains, GenerationRules rules, IServiceC

foreach (var chain in chains)
{
var returnedSagas = chain.ReturnVariablesOfType<Saga>();
var returnedSagas = chain.HandlerCalls()
.Where(call => !call.Method.HasAttribute<SagaIsResponseOnlyAttribute>() &&
!call.HandlerType.HasAttribute<SagaIsResponseOnlyAttribute>())
.SelectMany(call => call.Creates)
.Where(x => x.VariableType.CanBeCastTo<Saga>());
foreach (var saga in returnedSagas)
{
if (!attachSagaPersistenceFrame(container, providers, saga, chain))
Expand Down
10 changes: 10 additions & 0 deletions src/Wolverine/Persistence/Sagas/SagaIsResponseOnlyAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Wolverine.Persistence.Sagas;

/// <summary>
/// Marks a handler or HTTP endpoint method whose returned <see cref="Saga"/> is only a value to hand back
/// to the caller (an HTTP response body, for example) and is NOT a new saga to start. By default Wolverine
/// treats any returned <see cref="Saga"/> as a new saga and inserts it, which fails with a duplicate key
/// when the saga was already started some other way (e.g. by IMessageBus.InvokeAsync()).
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public class SagaIsResponseOnlyAttribute : Attribute;