Send Message with Eventual Reply Edit on GitHub


In some cases you will send a message and expect a reply at some point from the downstream system, but you don't need the response right away and you don't care which node in your application handles the eventual reply. For that scenario, Jasper provides this functionality with the syntax shown below:


internal async Task using_global_request_and_reply(IMessageContext messaging)
{
    // Send a question to another application, and request that the handling
    // service send back an answer
    await messaging.SendAndExpectResponseFor<Answer>(new Question());
}

[Fact]
public async Task invoke_expecting_a_response()
{
    StartTheReceiver(x => { x.Handlers.IncludeType<QuestionAndAnswer>(); });

    var answer = await theReceiver.Messaging.Invoke<Answer>(new Question {One = 3, Two = 4});

    answer.Sum.ShouldBe(7);
    answer.Product.ShouldBe(12);
}

[Fact]
public async Task invoke_with_expected_response_when_there_is_no_receiver()
{
    StartTheReceiver(x => { x.Handlers.IncludeType<QuestionAndAnswer>(); });
    await Should.ThrowAsync<ArgumentOutOfRangeException>(async () =>
    {
        await theReceiver.Messaging.Invoke<Answer>(new QuestionWithNoHandler());
    });
}

[Fact]
public async Task invoke_with_no_known_response_do_not_blow_up()
{
    StartTheReceiver(x => { x.Handlers.IncludeType<QuestionAndAnswer>(); });
    (await theReceiver.Messaging.Invoke<Answer>(new QuestionWithNoAnswer()))
        .ShouldBeNull();
}