protected virtual IEnumerable <SQLSubscription> SelectSubscriptions() { var subscriptions = new List <SQLSubscription>(); var connection = ConnectionProvider.GetConnection(); try { var commandBuilder = _commandBuilders.NewSelectSubscriptionsCommandBuilder(); commandBuilder.CutoffDate = DateTime.UtcNow; using (var scope = new TransactionScope(TransactionScopeOption.Required, TransactionScopeAsyncFlowOption.Enabled)) { using (var command = commandBuilder.BuildDbCommand(connection)) { using (var reader = command.ExecuteReader()) { while (reader.Read()) { var topicName = (TopicName)reader.GetString("TopicName"); var subscriber = new Uri(reader.GetString("Subscriber")); var expires = reader.GetDateTime("Expires").GetValueOrDefault(DateTime.MaxValue); var subscription = new SQLSubscription(topicName, subscriber, expires); subscriptions.Add(subscription); } } } scope.Complete(); } } finally { ConnectionProvider.ReleaseConnection(connection); } return(subscriptions); }
protected virtual Task <SQLSubscription> InsertOrUpdateSubscription(TopicName topic, Uri subscriber, DateTime expires) { SQLSubscription subscription; var connection = ConnectionProvider.GetConnection(); try { var insertBuilder = _commandBuilders.NewInsertSubscriptionCommandBuilder(); insertBuilder.TopicName = topic; insertBuilder.Subscriber = subscriber.ToString(); insertBuilder.Expires = expires; using (var scope = new TransactionScope(TransactionScopeOption.Required)) { using (var insertCommand = insertBuilder.BuildDbCommand(connection)) { var rowsAffected = insertCommand.ExecuteNonQuery(); if (rowsAffected == 0) { var updateBuilder = _commandBuilders.NewUpdateSubscriptionCommandBuilder(); updateBuilder.TopicName = topic; updateBuilder.Subscriber = subscriber.ToString(); updateBuilder.Expires = expires; using (var updateCommand = updateBuilder.BuildDbCommand(connection)) { updateCommand.ExecuteNonQuery(); } } } subscription = new SQLSubscription(topic, subscriber, expires); scope.Complete(); } } finally { ConnectionProvider.ReleaseConnection(connection); } return(Task.FromResult(subscription)); }