3

I am trying to use mass transit for request response handling. Most examples for mass transit are for console application or web application and I don't know how to start or stop bus on producer when I use it in class library.

Because in examples for web application bus started on application start but for class library there are no such a thing like startup.cs.

My question is where to start bus or stop when I use class library for connecting to bus?

My producer code looks like

IBusControl busControl = CreateBus();
TaskUtil.Await(() => busControl.StartAsync());
IRequestClient<IAccountingRequest, IAccountingResponse> client = CreateRequestClient(busControl);
IAccountingResponse response = null;
AccountingRequest accountingRequest = MapToAccountingRequest(accountingIntegration);

Task.Run(async () =>
{
    response = await client.Request(accountingRequest);          
}).Wait();
busControl.Stop();

But I think starting and stopping bus for every request is not good.

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Fikret Asma
  • 121
  • 2
  • 6

1 Answers1

0

You should provide methods in your class library to Start/Stop the bus. You can abstract them however you want, but allow the developer the ability to startup and shutdown the bus. Many other libraries to this, typically via a method to Stop, Shutdown, Close, etc.

The fact that you're also hiding the Task in the above example and blocking/waiting makes me thing you're stuck within something super legacy that you can't get avoid. In this case, well, I had to say this, but manage the reference to the bus in some static singleton (yuck), and Start it the first time it's used (double yuck), then, try to find a hook on application exit to stop it clean (good luck).

The best solution is to give the developers a call into your library to shut it down so they can free the connection and resources.

Chris Patterson
  • 28,659
  • 3
  • 47
  • 59
  • Thank you a lot.By Class library I mean Business Layer.I use mass transit for replacing legacy dll and I use request/response pattern for this.Because I use bus in business layer i dont have control over start or stop bus on application lifecyle so my question is it ok to start before client.Request and stop after that? If it is not a good idea to start and stop bus for calling client.Request on producer side can I mange bus start or stop on Dependency Injector side? – Fikret Asma May 23 '18 at 15:46