antonyvorontsov / RabbitMQ.Client.Core.DependencyInjection

.Net Core library-wrapper of RabbitMQ.Client for Dependency Injection.
MIT License
111 stars 36 forks source link

what is the particular reason (constraint) to have examples for consuming and producing in different projects? #91

Closed MaslovD closed 3 years ago

MaslovD commented 3 years ago

where to find simple demonstration of how to inject RabbitMQ client into service?

antonyvorontsov commented 3 years ago

There is a directory with examples.

You can inject IProducingService inside your service or controller simply doing this

namespace Examples.AdvancedConfiguration.Controllers
{
    [ApiController]
    [Route("api/example")]
    public class ExampleController : ControllerBase
    {
        private readonly ILogger<ExampleController> _logger;
        private readonly IProducingService _producingService;

        public ExampleController(
            IProducingService producingService,
            ILogger<ExampleController> logger)
        {
            _producingService = producingService;
            _logger = logger;
        }

        [HttpGet]
        public async Task<IActionResult> Get()
        {
            _logger.LogInformation($"Sending messages with {typeof(IProducingService)}.");
            var message = new { message = "text" };
            await _producingService.SendAsync(message, "consumption.exchange", "routing.key");
            return Ok(message);
        }
    }
}

But if for some reason you want to use IConsumingService you can inject it the same way as a IProducingService.

antonyvorontsov commented 3 years ago

I actually wanna add few more examples both for the documentation section and for examples directory.