Handling an Unknown Message Edit on GitHub


In the case where your system receives a message with a message type that you cannot handle, Jasper gives you the ability to write your own policy about how to deal with that message with the IMissingHandler interface shown below:


public interface IMissingHandler
{
    Task Handle(Envelope envelope, IMessageContext context);
}

To create your own policy or fall through handler, implement your own version of that interface like this sample shown below:


public class MyMissingHandler : IMissingHandler
{
    public Task Handle(Envelope envelope, IMessageContext context)
    {
        return context
            .Advanced
            .SendFailureAcknowledgement("I don't know how to process this message");
    }
}

Lastly, to register your own policy, just add it to the application IoC container like so:


public class ConfigureMissingHandlerApp : JasperRegistry
{
    public ConfigureMissingHandlerApp()
    {
        // Just add your type to the IoC container
        Services.AddSingleton<IMissingHandler, MyMissingHandler>();
    }
}

Do note that you can add multiple IMissingHandler's in a single application and all of them will be executed.