Ejemplo n.º 1
0
        public async Task DoTransactionAsync(Func <CancellationToken, Task> txnAction, Action <Behavior> transactionBehaviorAction = default(Action <Behavior>), Action <SessionBehavior> sessionBehaviorAction = default(Action <SessionBehavior>), IClientSessionHandle parentSession = default(IClientSessionHandle), CancellationToken cancellationToken = default(CancellationToken))
        {
            var sessionBehavior = new SessionBehavior();

            sessionBehaviorAction?.Invoke(sessionBehavior);
            sessionBehavior = sessionBehaviorAction == null ? this.configurationSource.Model.SessionBehavior : default(SessionBehavior);
            using (var session = await this.configurationSource.Source.StartSessionAsync(sessionBehavior.ToClientSessionOptions(), cancellationToken).ConfigureAwait(false))
            {
                try
                {
                    this.clientSessionHandle = session;
                    var task = transactionRetryPolicy.ExecuteAsync((cToken) => RunTransactionAsync(txnAction, session, parentSession, transactionBehaviorAction, cToken), cancellationToken);
#if NETFULL
                    await task.ConfigureAwait(false);
#else
                    await task;
#endif
                }
                catch (Exception)
                {
                    this.clientSessionHandle = default(IClientSessionHandle);
                    throw;
                }
            }
            this.clientSessionHandle = default(IClientSessionHandle);
        }
Ejemplo n.º 2
0
        public static ClientSessionOptions ToClientSessionOptions(this SessionBehavior sessionBehavior)
        {
            if (sessionBehavior == null)
            {
                return(default(ClientSessionOptions));
            }

            return(new ClientSessionOptions
            {
                CausalConsistency = sessionBehavior.CasualConsistency,
                DefaultTransactionOptions = new TransactionOptions(sessionBehavior.ReadConcern, sessionBehavior.ReadPreference, sessionBehavior.WriteConcern)
            });
        }
Ejemplo n.º 3
0
        public async Task <IClientSessionHandle> BeginSessionAsync(Action <SessionBehavior> sessionBehaviorAction = default(Action <SessionBehavior>), CancellationToken cancellationToken = default(CancellationToken))
        {
            var sessionBehavior = new SessionBehavior();

            sessionBehaviorAction?.Invoke(sessionBehavior);
            sessionBehavior = sessionBehaviorAction == null ? this.configurationSource.Model.SessionBehavior : default(SessionBehavior);

            var task = this.configurationSource.Source.StartSessionAsync(sessionBehavior.ToClientSessionOptions(), cancellationToken);

#if NETFULL
            this.clientSessionHandle = await task.ConfigureAwait(false);
#else
            this.clientSessionHandle = await task;
#endif
            return(this.clientSessionHandle);
        }
Ejemplo n.º 4
0
        internal void ApplyDefaultBehavior()
        {
            if (_isDefaultDatabaseBehavior)
            {
                DatabaseBehavior = new Behavior
                {
                    ReadPreference = ReadPreference.Secondary,
                    ReadConcern    = ReadConcern.Linearizable,
                    WriteConcern   = WriteConcern.WMajority
                };
            }
            if (_isDefaultCollectionBehavior)
            {
                CollectionBehavior = new Behavior
                {
                    ReadPreference = ReadPreference.Secondary,
                    ReadConcern    = ReadConcern.Linearizable,
                    WriteConcern   = WriteConcern.WMajority
                };
            }
            if (_isDefaultSessionBehavior)
            {
                SessionBehavior = new SessionBehavior
                {
                    CasualConsistency = true,
                    ReadPreference    = ReadPreference.Primary,
                    ReadConcern       = ReadConcern.Linearizable,
                    WriteConcern      = WriteConcern.WMajority
                };
            }
            if (_isDefaultTransactionBehavior)
            {
                TransactionBehavior = new Behavior
                {
                    ReadPreference = ReadPreference.Primary,
                    ReadConcern    = ReadConcern.Linearizable,
                    WriteConcern   = WriteConcern.WMajority
                };
            }

            _apply(this);
        }