Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of <see cref="CommittedEventStreamCoordinator"/>
        /// </summary>
        /// <param name="serializer"><see cref="ISerializer"/> to use for deserializing <see cref="IEvent">events</see></param>
        /// <param name="applicationResourceIdentifierConverter"><see cref="IApplicationResourceIdentifierConverter"/> used for converting resource identifiers</param>
        /// <param name="applicationResourceResolver"><see cref="IApplicationResourceResolver"/> used for resolving types from <see cref="IApplicationResourceIdentifier"/></param>
        /// <param name="connectionStringProvider"><see cref="ICanProvideConnectionStringToReceiver">Provider</see> of connection string</param>
        public CommittedEventStreamReceiver(
            ISerializer serializer,
            IApplicationResourceIdentifierConverter applicationResourceIdentifierConverter,
            IApplicationResourceResolver applicationResourceResolver,
            ICanProvideConnectionStringToReceiver connectionStringProvider)
        {
            _serializer = serializer;
            _applicationResourceIdentifierConverter = applicationResourceIdentifierConverter;
            _applicationResourceResolver            = applicationResourceResolver;

            var connectionString = connectionStringProvider();

            _queueClient = new QueueClient(connectionString, Constants.QueueName, ReceiveMode.PeekLock);
            _queueClient.RegisterMessageHandler(Receive);
        }
        /// <summary>
        /// Initializes a new instance of <see cref="CommittedEventStreamCoordinator"/>
        /// </summary>
        /// <param name="serializer"><see cref="ISerializer"/> to use for deserializing <see cref="IEvent">events</see></param>
        /// <param name="applicationResourceIdentifierConverter"><see cref="IApplicationResourceIdentifierConverter"/> used for converting resource identifiers</param>
        /// <param name="applicationResourceResolver"><see cref="IApplicationResourceResolver"/> used for resolving types from <see cref="IApplicationResourceIdentifier"/></param>
        /// <param name="connectionStringProvider"><see cref="ICanProvideConnectionStringToReceiver">Provider</see> of connection string</param>
        public CommittedEventStreamReceiver(
            ISerializer serializer,
            IApplicationResourceIdentifierConverter applicationResourceIdentifierConverter,
            IApplicationResourceResolver applicationResourceResolver,
            ICanProvideConnectionStringToReceiver connectionStringProvider)
        {
            _serializer = serializer;
            var factory = new ConnectionFactory();

            factory.Uri = connectionStringProvider();

            var connection   = factory.CreateConnection();
            var exchangeName = "Events";
            var routingKey   = "RoutingKey";
            var queueName    = "Events";

            var model = connection.CreateModel();

            model.ExchangeDeclare(exchangeName, ExchangeType.Direct);
            model.QueueDeclare(queueName, false, false, false, null);
            model.QueueBind(queueName, exchangeName, routingKey, null);

            var consumer = new EventingBasicConsumer(model);

            consumer.Received += (ch, ea) =>
            {
                try
                {
                    var dynamicEventsAndEnvelopes = new List <dynamic>();
                    var json = System.Text.Encoding.UTF8.GetString(ea.Body);

                    _serializer.FromJson(dynamicEventsAndEnvelopes, json);

                    var eventsAndEnvelopes = new List <EventAndEnvelope>();

                    foreach (var dynamicEventAndEnvelope in dynamicEventsAndEnvelopes)
                    {
                        var env = dynamicEventAndEnvelope.Envelope;

                        var correlationId              = (TransactionCorrelationId)Guid.Parse(env.CorrelationId.ToString());
                        var eventId                    = (EventId)Guid.Parse(env.EventId.ToString());
                        var sequenceNumber             = (EventSequenceNumber)long.Parse(env.SequenceNumber.ToString());
                        var sequenceNumberForEventType = (EventSequenceNumber)long.Parse(env.SequenceNumberForEventType.ToString());
                        var generation                 = (EventGeneration)long.Parse(env.Generation.ToString());
                        var @event        = applicationResourceIdentifierConverter.FromString(env.Event.ToString());
                        var eventSourceId = (EventSourceId)Guid.Parse(env.EventSourceId.ToString());
                        var eventSource   = applicationResourceIdentifierConverter.FromString(env.EventSource.ToString());
                        var version       = (EventSourceVersion)EventSourceVersion.FromCombined(double.Parse(env.Version.ToString()));
                        var causedBy      = (CausedBy)env.CausedBy.ToString();
                        var occurred      = DateTimeOffset.Parse(env.Occurred.ToString());

                        var envelope = new EventEnvelope(
                            correlationId,
                            eventId,
                            sequenceNumber,
                            sequenceNumberForEventType,
                            generation,
                            @event,
                            eventSourceId,
                            eventSource,
                            version,
                            causedBy,
                            occurred
                            );

                        var eventType     = applicationResourceResolver.Resolve(@event);
                        var eventInstance = Activator.CreateInstance(eventType, new object[] { eventSourceId }) as IEvent;
                        var e             = dynamicEventAndEnvelope.Event.ToString();

                        _serializer.FromJson(eventInstance, e);

                        eventsAndEnvelopes.Add(new EventAndEnvelope(envelope, eventInstance));
                    }

                    var stream = new CommittedEventStream(eventsAndEnvelopes.First().Envelope.EventSourceId, eventsAndEnvelopes);
                    Received(stream);

                    model.BasicAck(ea.DeliveryTag, false);
                }
                catch
                {
                }
                finally
                {
                }
            };

            Task.Run(() => model.BasicConsume(queueName, false, consumer));
        }