public IConventions Get(Type type) { if (_conventions.TryGetValue(type, out var conventions)) { return(conventions); } conventions = new MessageConventions(type, _builder.GetRoutingKey(type), _builder.GetExchange(type), _builder.GetQueue(type)); _conventions.TryAdd(type, conventions); return(conventions); }
public Task PublishAsync <T>(T message) where T : class { using (var channel = _connection.CreateModel()) { var exchanges = AppDomain.CurrentDomain .GetAssemblies() .SelectMany(a => a.GetTypes()) .Where(t => t.IsDefined(typeof(MessageAttribute), false)) .Select(t => t.GetCustomAttribute <MessageAttribute>().Exchange) .Distinct() .ToList(); if (_options.Exchange.Declare) { channel.ExchangeDeclare( exchange: _options.Exchange.Name, type: _options.Exchange.Type, durable: _options.Exchange.Durable, autoDelete: _options.Exchange.AutoDelete ); } foreach (var exchange in exchanges) { if (exchange.Equals(_options.Exchange?.Name, StringComparison.InvariantCultureIgnoreCase)) { continue; } channel.ExchangeDeclare(exchange, "topic", true); } var json = JsonConvert.SerializeObject(message); var body = Encoding.UTF8.GetBytes(json); channel.BasicPublish( exchange: _conventionsBuilder.GetExchange(typeof(T)), routingKey: _conventionsBuilder.GetRoutingKey(typeof(T)), basicProperties: null, body: body); } return(Task.CompletedTask); }
public Task Subscribe <T>() where T : class { using (var channel = _connection.CreateModel()) { // var exchanges = AppDomain.CurrentDomain // .GetAssemblies() // .SelectMany(a => a.GetTypes()) // .Where(t => t.IsDefined(typeof(MessageAttribute), false)) // .Select(t => t.GetCustomAttribute<MessageAttribute>().Exchange) // .Distinct() // .ToList(); if (_options.Exchange.Declare) { channel.ExchangeDeclare( exchange: _options.Exchange.Name, type: _options.Exchange.Type, durable: _options.Exchange.Durable, autoDelete: _options.Exchange.AutoDelete ); } // foreach (var exchange in exchanges) // { // if (exchange.Equals(_options.Exchange?.Name, // StringComparison.InvariantCultureIgnoreCase)) // { // continue; // } // channel.ExchangeDeclare(exchange, "topic", true); // } channel.QueueBind( queue: _conventionsBuilder.GetQueue(typeof(T)), exchange: _conventionsBuilder.GetExchange(typeof(T)), routingKey: _conventionsBuilder.GetRoutingKey(typeof(T))); channel.BasicQos( _options.Qos.PrefetchSize, _options.Qos.PrefetchCount, _options.Qos.Global); var consumer = new EventingBasicConsumer(channel); consumer.Received += async(model, ea) => { var body = ea.Body; var message = Encoding.UTF8.GetString(body.ToArray()); var handler = _serviceScopeFactory.CreateScope() .ServiceProvider.GetService(typeof(IEventHandler <>)); var eventType = typeof(T); var conreteType = typeof(IEventHandler <>).MakeGenericType(eventType); var @event = JsonConvert.DeserializeObject(message, eventType); await(Task) conreteType.GetMethod("Handle").Invoke(handler, new object[] { @event }); }; channel.BasicConsume( queue: _conventionsBuilder.GetQueue(typeof(T)), autoAck: true, consumer: consumer); } return(Task.CompletedTask); }