Пример #1
0
        public async ValueTask AddAsync(QueueMessage message, TimeSpan?timeToLive = null, TimeSpan?visibilityTimeout = null, TimeSpan?initialVisibilityDelay = null)
        {
            long?messageId = null;

            SqlServerQueueClient.EnsureValidQueueMessage(message, ref messageId);

            visibilityTimeout = visibilityTimeout ?? SqlServerQueueClient.MinimumVisibilityTimeout;

            if (visibilityTimeout < SqlServerQueueClient.MinimumVisibilityTimeout)
            {
                visibilityTimeout = SqlServerQueueClient.MinimumVisibilityTimeout;
            }
            else if (visibilityTimeout > SqlServerQueueClient.MaximumVisibilityTimeout)
            {
                visibilityTimeout = SqlServerQueueClient.MaximumVisibilityTimeout;
            }

            timeToLive = timeToLive ?? SqlServerQueueClient.DefaultTimeToLive;

            initialVisibilityDelay = initialVisibilityDelay ?? TimeSpan.Zero;

            QueueMessageEntity entity = new QueueMessageEntity();

            entity.Environment       = this.Context.Name;
            entity.TimestampUtc      = DateTime.UtcNow.AddSeconds(initialVisibilityDelay.Value.TotalSeconds);
            entity.VisibilityTimeout = (int)visibilityTimeout.Value.TotalSeconds;
            entity.RetrievedOnUtc    = null;
            entity.Content           = this.MessageSerializer.Serialize(message);
            entity.Flags             = message.Flags;
            entity.Command           = message.CommandType;
            entity.TimeToLive        = (int)timeToLive.Value.TotalSeconds;

            await this.DbContext.AddAsync(entity);

            await this.DbContext.SaveChangesAsync();

            this.DbContext.Entry(entity).State = EntityState.Detached;

            message.SetId(Convert.ToString(entity.Id));
        }