/// <summary> /// Creates an event consumer that begins consuming events from the specified URI. /// Events are delivered to the event handler. The disposable object that is /// returned is used to define the lifetime associated with the subscription. /// </summary> /// <param name="uri">The URI.</param> /// <param name="eventHandler">The event handler.</param> /// <returns></returns> public IDisposable Subscribe(Uri uri, EventHandler <UpdateEventArgs> eventHandler) { var pathParts = uri.Segments; var exchangePath = pathParts[1].TrimEnd('/'); var routingKey = pathParts[2]; // find the exchange path binding ExchangePathBinding exchangePathBinding; if (!_exchangePathBindingTable.TryGetValue(exchangePath, out exchangePathBinding)) { exchangePathBinding = new ExchangePathBinding(); exchangePathBinding.Connection = _connectionFactory.CreateConnection(); exchangePathBinding.Channel = exchangePathBinding.Connection.CreateModel(); _exchangePathBindingTable[exchangePath] = exchangePathBinding; } // find a queue for this routingKey RouteBinding routeBinding; if (!exchangePathBinding.RoutingKeyTable.TryGetValue(routingKey, out routeBinding)) { routeBinding = new RouteBinding(); routeBinding.Queue = exchangePathBinding.Channel.QueueDeclare(); routeBinding.Consumer = new EventingBasicConsumer(); routeBinding.Consumer.Received += routeBinding.HandleEvent; exchangePathBinding.RoutingKeyTable[routingKey] = routeBinding; exchangePathBinding.Channel.QueueBind(routeBinding.Queue, exchangePath, routingKey); exchangePathBinding.Channel.BasicConsume(routeBinding.Queue, true, routeBinding.Consumer); } routeBinding.Events += eventHandler; return(new TrackedDisposable( () => routeBinding.Events -= eventHandler)); }
/// <summary> /// Creates an event consumer that begins consuming events from the specified URI. /// Events are delivered to the event handler. The disposable object that is /// returned is used to define the lifetime associated with the subscription. /// </summary> /// <param name="uri">The URI.</param> /// <param name="eventHandler">The event handler.</param> /// <returns></returns> public IDisposable Subscribe(Uri uri, EventHandler<UpdateEventArgs> eventHandler) { var pathParts = uri.Segments; var exchangePath = pathParts[1].TrimEnd('/'); var routingKey = pathParts[2]; // find the exchange path binding ExchangePathBinding exchangePathBinding; if (!_exchangePathBindingTable.TryGetValue(exchangePath, out exchangePathBinding)) { exchangePathBinding = new ExchangePathBinding(); exchangePathBinding.Connection = _connectionFactory.CreateConnection(); exchangePathBinding.Channel = exchangePathBinding.Connection.CreateModel(); _exchangePathBindingTable[exchangePath] = exchangePathBinding; } // find a queue for this routingKey RouteBinding routeBinding; if (!exchangePathBinding.RoutingKeyTable.TryGetValue(routingKey, out routeBinding)) { routeBinding = new RouteBinding(); routeBinding.Queue = exchangePathBinding.Channel.QueueDeclare(); routeBinding.Consumer = new EventingBasicConsumer(); routeBinding.Consumer.Received += routeBinding.HandleEvent; exchangePathBinding.RoutingKeyTable[routingKey] = routeBinding; exchangePathBinding.Channel.QueueBind(routeBinding.Queue, exchangePath, routingKey); exchangePathBinding.Channel.BasicConsume(routeBinding.Queue, true, routeBinding.Consumer); } routeBinding.Events += eventHandler; return new TrackedDisposable( () => routeBinding.Events -= eventHandler); }