This sample shows how to repair a malformed message by redirecting them to a temporary endpoint. The sample uses the Learning Transport and a portable version of the Particular Service Platform tools. Installing ServiceControl is not required.
This sample includes a portable version of the Particular Service Platform tools. These tools are available only on Windows environments but are not necessary to run the sample.
Running the project
Running the project will result in 4 console windows. Wait a moment until the ServicePulse window opens in the browser.
- Press Enter in the Sender console window
- Observe the error log output in the Receiver console window: the endpoint cannot process the message because the
Id
field is malformed -- it contains lowercase characters. - Switch to ServicePulse. It should show one failed message. Press the retry button and wait until the message is retried.
- Observe the error log output in the Receiver console window. The endpoint still can't process the message.
- Switch to ServicePulse again. Go to the
Configuration
tab and selectRetry redirects
. - Create a redirect from
FixMalformedMessages.
toReceiver FixMalformedMessages.
.MessageRepairingEndpoint - Go back to the
Failed Messages
tab, select the failed message and press the retry button again. - Switch to the MessageRepairingEndpoint console window and observe the log entries saying the message has been repaired and forwarded.
- Switch to the Receiver console window and observer the successful processing notification.
Code walk-through
The Receiver project
Retries are disabled in the sample for simplicity; messages are immediately moved to the error queue after a processing failure:
var recoverability = endpointConfiguration.Recoverability();
recoverability.Delayed(
customizations: retriesSettings =>
{
retriesSettings.NumberOfRetries(0);
});
recoverability.Immediate(
customizations: retriesSettings =>
{
retriesSettings.NumberOfRetries(0);
});
This endpoint processes messages of type SimpleMessage
and expects the Id
field to not contain any lowercase characters. Messages with lowercase characters are rejected (and sent to the error queue).
log.Info($"Received message with Id = {message.Id}.");
if (message.Id.Any(char.IsLower))
{
throw new Exception("Lowercase characters are not allowed in message Id.");
}
log.Info($"Successfully processed message with Id = {message.Id}.");
return Task.CompletedTask;
Sender
Sends messages of type, SimpleMessage
, and emulates a bug by sending malformed Id
field values.
MessageRepairingEndpoint
This is a disposable one-time endpoint used to repair malformed messages sent by the Sender endpoint. In a real-life scenario it can be removed as soon as the bug in Sender is fixed. It contains a handler for SimpleMessage
that repairs the messages and forwards them to the Receiver.
log.Info($"Repairing message with Id = {message.Id}.");
message.Id = message.Id.ToUpperInvariant();
log.Info($"Forwarding repaired message with Id = {message.Id} to the Receiver.");
return context.Send("FixMalformedMessages.Receiver", message);