public void Publish(Event @event) { var policy = Policy.Handle <Exception>() .WaitAndRetry(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (ex, time) => { }); using (var channel = _connection.CreateModel()) { var eventName = @event.GetType() .Name; channel.ExchangeDeclare(exchange: _connection.Config.Exchange, type: _connection.Config.ExchangeType, true); var settings = NewtonsoftJsonUtil.UseCustomNewtonsoftSettings(); var message = JsonConvert.SerializeObject(@event, settings); var body = Encoding.UTF8.GetBytes(message); policy.Execute(() => { channel.BasicPublish(exchange: _connection.Config.Exchange, routingKey: eventName, basicProperties: null, body: body); }); } }
public async Task Handle(string message, IServiceProvider provider) { var settings = NewtonsoftJsonUtil.UseCustomNewtonsoftSettings(); var eventData = JsonConvert.DeserializeObject(message, EventType, settings); var handler = provider.GetService(HandlerType); await(Task) HandlerType.GetMethod("Handle") .Invoke(handler, new[] { eventData }); }