Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Text;
- using Newtonsoft.Json;
- using RabbitMQ.Client;
- using RabbitMQ.Client.Events;
- namespace ChatMicroservice;
- public class UserIdRequester
- {
- private readonly IConfiguration _configuration;
- private readonly ConnectionFactory _factory;
- public UserIdRequester(IConfiguration configuration)
- {
- _configuration = configuration;
- _factory = _configuration.GetSection("RabbitMQ").Get<ConnectionFactory>()!;
- }
- public async Task<Guid?> GetUserIdByUsernameAsync(string username)
- {
- using var connection = _factory.CreateConnection();
- using var channel = connection.CreateModel();
- var requestQueue = "user_id_request_queue";
- //var replyQueue = "user_id_reply_queue";
- //var correlationId = Guid.NewGuid().ToString();
- //var props = channel.CreateBasicProperties();
- //props.ReplyTo = replyQueue;
- //props.CorrelationId = correlationId;
- channel.QueueDeclare(queue: requestQueue, durable: true, exclusive: false, autoDelete: false, arguments: null);
- var message = new { Username = username };
- var body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message));
- channel.BasicPublish(exchange: string.Empty, routingKey: requestQueue, body: body);
- /*var tcs = new TaskCompletionSource<Guid?>();
- var consumer = new EventingBasicConsumer(channel);
- consumer.Received += (model, ea) =>
- {
- if (ea.BasicProperties.CorrelationId == correlationId)
- {
- var response = Encoding.UTF8.GetString(ea.Body.ToArray());
- if (Guid.TryParse(response, out var userId))
- {
- tcs.SetResult(userId);
- }
- else
- {
- tcs.SetResult(null);
- }
- }
- };
- channel.BasicConsume(queue: replyQueue, autoAck: true, consumer: consumer);
- */
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement