0

I have a .Net console application that is supposed to be long running and continous , basically 24 hours a day. It's for a rabbitmq consumer client. I am opening 30 channels on 1 connection, and each channel is responsible for 7 different queues. Task creation:

            tokenSource2 = new CancellationTokenSource();
            cancellationToken = tokenSource2.Token;

            for (int i = 0; i < 30; i++) //MAX 100 MODEL 
            {
                List<string> partlist = tmpDBList.Take(7).ToList();
                tmpDBList = tmpDBList.Except(partlist).ToList();

                new Task(delegate { StartConsuming(partlist, cancellationToken); }, cancellationToken, TaskCreationOptions.LongRunning).Start();
            }

The consumer method:

internal void StartConsuming(List<string> dbNames, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            using (IModel channel = Consumer.CreateModel()) 
            {
                foreach (string item in dbNames)
                {
                  //Queue creation, exchange declare, bind, + basic eventhandler etc..
                    channel.BasicConsume(queue: item, 
                                            autoAck: true, 
                                            consumer: consumerEvent);
                }
                cancellationToken.ThrowIfCancellationRequested();
                while (!cancellationToken.IsCancellationRequested)
                {
                    cancellationToken.WaitHandle.WaitOne(5000);
                }
            }
        }

Since I want the task to never stop I have the endless while cycle at the end of the using statement, otherwise the task stops, and the channels are disposed.

                while (!cancellationToken.IsCancellationRequested)
                {
                    cancellationToken.WaitHandle.WaitOne(5000);
                }

Is this a an optimal solution?

Furthermore, each consumer event handler creates a DbContext of a specific database inside the

EventingBasicConsumer consumerEvent = new EventingBasicConsumer(channel);


                    consumerEvent.Received += (sender, basicDeliveryEventArgs) =>
                    {
                        cancellationToken.ThrowIfCancellationRequested();
//dbContext creation
}

event handler. Will the memory be freed after the eventhandler is finished ? Do I need to Dispose of the dbcontext and each class I am using inside the eventhandler?

  • The important question is when do you store data in a database or in a file. Because otherwise it is lost as soon as your application stops or crashes – Basile Starynkevitch Dec 14 '19 at 09:10
  • @BasileStarynkevitch data is stored in the database. And I also mentioned that too. – Jeremy Gyovai-Rafa Dec 14 '19 at 09:54
  • Relevant concepts: [tracing garbage collection](https://en.wikipedia.org/wiki/Tracing_garbage_collection), [persistence](https://en.wikipedia.org/wiki/Persistence_(computer_science)), [virtual address space](https://en.wikipedia.org/wiki/Virtual_address_space), [page cache](https://en.wikipedia.org/wiki/Page_cache). You'll find text books and academic conferences on all topics. – Basile Starynkevitch Dec 19 '19 at 16:40
  • Recommendation: read some [operating system](http://pages.cs.wisc.edu/~remzi/OSTEP/) textbook – Basile Starynkevitch Dec 19 '19 at 16:43
  • I suggest you to start using Autofac, which makes the lifetime management of objects very clear and easy. – Paolo Costa Dec 20 '19 at 12:02

0 Answers0