Пример #1
0
        public void SaveEvents(string aggregateId, IEnumerable <IEvent> newEvents, Type aggregateType)
        {
            if (GetAggregateEventCount(aggregateId) == 0)
            {
                SaveAggregate(aggregateId, aggregateType);
            }

            Retries.Retry(() => InternalSaveEvents(aggregateId, newEvents), new TimeSpan(0, 0, 2), 5);
        }
        // service bus best practices for performance:
        // http://msdn.microsoft.com/en-us/library/windowsazure/hh528527.aspx
        public void Send(ISendContext context)
        {
            _connectionHandler
            .Use(connection =>
            {
                // don't have too many outstanding at same time
                SpinWait.SpinUntil(() => _messagesInFlight < _settings.MaxOutstanding);

                using (var body = new MemoryStream())
                {
                    context.SerializeTo(body);

                    // the envelope is re-usable, so let's capture it in the below closure
                    // as a value
                    var envelope = new MessageEnvelope(body.ToArray());

                    var sending = Retries.Retry(FaultPolicies.FinalAzurePolicy,
                                                new Func <AsyncCallback, object, IAsyncResult>((cb, state) =>
                    {
                        return(SendMessage(connection, () =>
                        {
                            var brokeredMessage = new BrokeredMessage(envelope);

                            if (!string.IsNullOrWhiteSpace(context.CorrelationId))
                            {
                                brokeredMessage.CorrelationId = context.CorrelationId;
                            }

                            if (!string.IsNullOrWhiteSpace(context.MessageId))
                            {
                                brokeredMessage.MessageId = context.MessageId;
                            }

                            return brokeredMessage;
                        }, 1, cb, state));
                    }),
                                                (IAsyncResult ar) =>
                    {
                        var state = (StateHolder)ar.AsyncState;
                        Interlocked.Decrement(ref _messagesInFlight);

                        try
                        {
                            state.Sender.EndSend(ar);
                            Address.LogEndSend(state.Message.MessageId);
                        }
                        finally
                        {
                            // always dispose the message; it's only good once
                            state.Message.Dispose();
                        }
                    });
                    sending.Wait();
                }
            });
        }