0

I have a fully working MassTransit saga, which runs some commands and then executes a request/response call to query a database and then ultimately return a response to the calling controller.

Locally this all works now 99% of the time (thanks to a lot of support I've received on here). However, when deployed to my Azure VM, which has a local copy of RabbitMQ and the 2 ASP.NET Core services running on it, the first call to the saga goes through straight away but all subsequent calls timeout.

I feel like it might be related to the fact that I'm using an InMemorySagaRepository (which in theory should be fine for my use case).

The saga is configured initially like so:

InstanceState(s => s.CurrentState);

Event(() => RequestLinkEvent, x => x.CorrelateById(context => context.Message.LinkId));
Event(() => LinkCreatedEvent, x => x.CorrelateById(context => context.Message.LinkId));
Event(() => CreateLinkGroupFailedEvent, x => x.CorrelateById(context => context.Message.LinkId));
Event(() => CreateLinkFailedEvent, x => x.CorrelateById(context => context.Message.LinkId));
Event(() => RequestLinkFailedEvent, x => x.CorrelateById(context => context.Message.LinkId));

Request(() => LinkRequest, x => x.UrlRequestId, cfg =>
{
            cfg.ServiceAddress = new Uri($"{hostAddress}/{nameof(SelectUrlByPublicId)}");
            cfg.SchedulingServiceAddress = new Uri($"{hostAddress}/{nameof(SelectUrlByPublicId)}");
            cfg.Timeout = TimeSpan.FromSeconds(30);
});

It's worth noting that my LinkId is ALWAYS a unique Guid as it is created in the controller before the message is sent.

ALSO when I restart the apppool it works again for the first call and then starts timing out again.

I feel like something might be locking somewhere but I can't reproduce it locally!

1 Answer 1

0

So I wanted to post my solution to my own problem here in the hopes that it will aide others in the future.

I made 3 fundamental changes which either in isolation or combination solved this issue and everything now flys and works 100% of the time whether I use an InMemorySagaRepository, Redis or MongoDB.

Issue 1

As detailed in another question I posted here:

MassTransit saga with Redis persistence gives Method Accpet does not have an implementation exception

In my SagaStateMachineInstance class I had mistakenly declared the CurrentState property as a 'State' type when it should have been a string as such:

public string CurrentState { get; set;}

This was a fundamental issue and it came to light as soon as I started trying to add persistence so it may have been causing troubles when using the InMemorySagaRepository too.

Issue 2

In hindsight I suspect this was probably my main issue and I'm not completely convinced I've solved it in the best way but I'm happy with how things are.

I made sure my final event is managed in all states. I think what was happening was my request/response was finishing before the CurrentState of the saga had been updated. I realised this was happening by experimenting with using MongoDB as my persistence and seeing that I had sagas not completing stuck in the penultimate state.

Issue 3

This should be unnecessary but I wanted to add it as something to consider/try for those having issues.

I removed the request/response step from my saga and replaced it with a publish/subscribe. To do this I published an event to my consumer which when complete publishes an event with the CorrelationId (as suggested by @alexey-zimarev in my other issue). So in my consumer that does the query (i.e. reuqest) I do the following after it completes:

context.Publish(new LinkCreatedEvent { ... , CorrelationId = context.Message.CorrelationId })

Because the CorrelationId is in there my saga picks it up and handles the event as such:

When(LinkCreatedEvent )
   .ThenAsync(HandleLinkCreatedEventAsync)
   .TransitionTo(LinkCreated)

I'm really happy with how it all works now and feel confident about putting the solution live.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.