Witam serdecznie.
Mam taki kod:

RabbitMQ:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RabbitMQ.Client;

namespace InsuranceService.BusinessLogic.Logic.Carlnsurance
{
    public class Rabbit
    {
        public IConnection GetConnection(string hostName, string userName, string password)
        {
            ConnectionFactory connectionFactory = new ConnectionFactory();
            connectionFactory.HostName = "localhost";
            connectionFactory.UserName = "guest";
            connectionFactory.Password = "guest";
            connectionFactory.Port = 5672; // 5672 for regular connections, 5671 for connections that use TLS
            return connectionFactory.CreateConnection();
        }

        public  void Send(string queue, string data)
        {
            using (IConnection connection = new ConnectionFactory().CreateConnection())
            {
                using (IModel channel = connection.CreateModel())
                {
                    channel.QueueDeclare(queue, false, false, false, null);
                    channel.BasicPublish(string.Empty, queue, null, Encoding.UTF8.GetBytes(data));
                }
            }
        }

        public  void Receive(string queue)
        {
            using (IConnection connection = new ConnectionFactory().CreateConnection())
            {
                using (IModel channelconsumer = connection.CreateModel())
                {
                    channelconsumer.QueueDeclare(queue, false, false, false, null);
                    BasicGetResult result = channelconsumer.BasicGet(queue, true);
                    if (result != null)
                    {
                        string data =
                        Encoding.UTF8.GetString(result.Body);
                        Console.WriteLine(data);
                    }
                }
            }
        }
    }
}

Main.CS


using RabbitMQ.Client;
using System.Text;
using System;
using InsuranceService.BusinessLogic.Logic.Carlnsurance;

namespace InsuranceService.ConsoleApp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            // Test RabbitMQ
            Random rnd = new Random();
            int num = rnd.Next(10000);

            var sender = new Rabbit();
            sender.Send("TestService", "Test Message ID: " + num);
            sender.Receive("TestService");
            Console.ReadLine();
        }
    }
}

Kod działa poprawnie. Dodaje komunikaty do Rabbita.

Mam teraz plik Works.CS:


using Newtonsoft.Json;
using System;
using System.Linq;
using System.Net;
using System.Text;

namespace Main.Sample
{
    using System.Collections.Generic;
    using System.Web.Script.Serialization;

    using InsuranceService.Common.Helpers;

    public class LogicMain
    {
        public CalculationResult GenerateResult(CalculationTask work, int resultNo, int resultCount, MotorContract result)
        {
            var result1 = new CalculationResult
            {
                Id = work.Id,
                ResultNo = resultNo,
                ResultCount = resultCount,
                Result = new Result()
                {
                    Data = result
                }
            };
            return result1;
        }

        public ListOfCalculationTasks GetCalculationWorkList()
        {
            using (var webClient = new WebClient() { Encoding = Encoding.UTF8 })
            {
                var str = webClient.DownloadString(Settings.CalculationWorkerGetUrl);

                Console.WriteLine(str);

                return result;
            }
        }

        public ListOfPolicyTasks GetPolicyWorkList()
        {
            using (var webClient = new WebClient() { Encoding = Encoding.UTF8 })
            {
                var str = webClient.DownloadString(Settings.PolicyWorkerGetUrl);
                Console.WriteLine(str);

                return result;
            }
        }

        private void HandleError(string uploadResultUrl, dynamic task, Exception ex, string baseDir)
        {
            Console.WriteLine('error');
        }

    }
}

Powyższy kod zapisuje i wyświetla różne rzeczy na ekranie. To mi działa poprawnie.

Chciałbym teraz w każdym miejscu, gdzie mam Console.WriteLine zrobić zapis tego, co wyświetla
konsola do Rabbita.

W pliku głównym aplikacji nawiązuję połączenie. Czy da się zrobić dodawanie z poziomu tego Works.cs
danych z połączenia z pliku głównego? Jeśli tak, to w jaki sposób?

Bardzo proszę o pomoc.