2019-08-16 11:47:57 +02:00
using System ;
using ExampleLib ;
using Messaging.TapetiExample ;
using Tapeti.Annotations ;
using Tapeti.Flow ;
using Tapeti.Flow.Annotations ;
namespace _03_FlowRequestResponse
{
[MessageController]
[DynamicQueue("tapeti.example.03")]
2019-08-17 14:19:29 +02:00
public class SimpleFlowController
2019-08-16 11:47:57 +02:00
{
private readonly IFlowProvider flowProvider ;
private readonly IExampleState exampleState ;
// Shows how multiple values can be passed to a start method
public struct StartData
{
public DateTime RequestStartTime ;
public int Amount ;
}
// Private and protected fields are lost between method calls because the controller is
// recreated when a response arrives. When using a persistent flow repository this may
// even be after a restart of the application.
private bool nonPersistentState ;
// Public fields will be stored.
public DateTime RequestStartTime ;
2021-07-18 13:27:10 +02:00
// Be sure not to accidentally use any public fields that aren't serializable, for example:
//public TaskCompletionSource<bool> SerializationFail = new TaskCompletionSource<bool>();
//
// In the Newtonsoft.Json version at the time of writing, this will not result in an exception but instead hang the flow!
2019-08-16 11:47:57 +02:00
2019-08-17 14:19:29 +02:00
public SimpleFlowController ( IFlowProvider flowProvider , IExampleState exampleState )
2019-08-16 11:47:57 +02:00
{
this . flowProvider = flowProvider ;
this . exampleState = exampleState ;
}
[Start]
public IYieldPoint StartFlow ( StartData startData )
{
nonPersistentState = true ;
RequestStartTime = startData . RequestStartTime ;
return flowProvider . YieldWithRequestSync < QuoteRequestMessage , QuoteResponseMessage > (
new QuoteRequestMessage
{
Amount = startData . Amount
} ,
HandleQuoteResponse ) ;
}
[Continuation]
public IYieldPoint HandleQuoteResponse ( QuoteResponseMessage message )
{
if ( nonPersistentState )
2019-08-17 14:19:29 +02:00
Console . WriteLine ( "[SimpleFlowController] This is not supposed to show. NonPersistentState should not be retained. Someone please check http://www.hasthelargehadroncolliderdestroyedtheworldyet.com." ) ;
2019-08-16 11:47:57 +02:00
2019-08-17 14:19:29 +02:00
Console . WriteLine ( "[SimpleFlowController] Request start: " + RequestStartTime . ToLongTimeString ( ) ) ;
Console . WriteLine ( "[SimpleFlowController] Response time: " + DateTime . Now . ToLongTimeString ( ) ) ;
Console . WriteLine ( "[SimpleFlowController] Quote: " + message . Quote ) ;
2019-08-16 11:47:57 +02:00
2021-03-16 10:54:15 +01:00
// Test for issue #21: Same request/response twice in flow does not continue
return flowProvider . YieldWithRequestSync < QuoteRequestMessage , QuoteResponseMessage > (
new QuoteRequestMessage
{
Amount = 42
} ,
HandleQuoteResponse2 ) ;
//exampleState.Done();
//return flowProvider.End();
}
[Continuation]
public IYieldPoint HandleQuoteResponse2 ( QuoteResponseMessage message )
{
Console . WriteLine ( "[SimpleFlowController] Quote 2: " + message . Quote ) ;
2019-08-16 11:47:57 +02:00
exampleState . Done ( ) ;
return flowProvider . End ( ) ;
}
2021-03-16 10:54:15 +01:00
2019-08-16 11:47:57 +02:00
}
}