public override async Task <T> BuildAndPersistAsync(IAsyncRepository repository = null)
        {
            if (repository == null)
            {
                var executionStrategy = new SqlAzureExecutionStrategy();

                SqlAzureDbConfiguration.SuspendExecutionStrategy = true;

                var entity = await executionStrategy.ExecuteAsync(() => base.BuildAndPersistAsync(null), CancellationToken.None);

                SqlAzureDbConfiguration.SuspendExecutionStrategy = false;

                return(entity);
            }

            return(await base.BuildAndPersistAsync(repository));
        }
Ejemplo n.º 2
0
        //public static void QueryMultipleResiliently(string sql, dynamic param = null, CommandType? commandType = null, Action<Dapper.SqlMapper.GridReader> action = null)
        //{
        //    try
        //    {
        //        var executionStrategy = new SqlAzureExecutionStrategy();
        //        executionStrategy.Execute(() =>
        //        {
        //            using (var connection = GetOpenConnection())
        //            {
        //                var reader = SqlMapper.QueryMultiple(connection, sql, param: param, transaction: null, commandTimeout: null, commandType: commandType);

        //                if (action != null)
        //                {
        //                    action(reader);
        //                }
        //            }
        //        });
        //    }
        //    catch (SqlException ex)
        //    {
        //        throw StripException(ex);
        //    }
        //}

        public static async Task QueryMultipleResilientlyAsync(string sql, dynamic param = null, CommandType?commandType = null, Action <Dapper.SqlMapper.GridReader> action = null)
        {
            var executionStrategy = new SqlAzureExecutionStrategy();
            await executionStrategy.ExecuteAsync(
                async() =>
            {
                using (var connection = await GetOpenConnectionAsync())
                {
                    var reader = await SqlMapper.QueryMultipleAsync(connection, sql, param: param, transaction: null, commandTimeout: null, commandType: commandType);

                    if (action != null)
                    {
                        action(reader);
                    }
                }
            },
                CancellationToken.None
                );
        }
Ejemplo n.º 3
0
        public static async Task <IEnumerable <T> > QueryResilientlyAsync <T>(string sql, dynamic param = null, CommandType?commandType = null)
        {
            IEnumerable <T> result            = null;
            var             executionStrategy = new SqlAzureExecutionStrategy();
            await executionStrategy.ExecuteAsync(
                async() =>
            {
                using (var connection = await GetOpenConnectionAsync())
                {
                    // QueryAsync is marked async in source code, but is not in metadata. +https://code.google.com/p/dapper-dot-net/source/browse/Dapper+NET45/SqlMapperAsync.cs
                    result = await SqlMapper.QueryAsync <T>(connection, sql, param: param, transaction: null, commandTimeout: null, commandType: commandType);
                }
            },
                // Apparently, CancellationToken is not used. See +https://github.com/Icehunter/entityframework/blob/master/src/EntityFramework.SqlServer/DefaultSqlExecutionStrategy.cs
                CancellationToken.None
                );

            return(result);
        }
        /// <summary>
        /// Creates the proxy generic.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="target">The target.</param>
        /// <returns>T.</returns>
        public T CreateProxyGeneric <T>(T target) where T : class
        {
            return(Proxy.CreateProxy(target, async invocation =>
            {
                var flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy;
                var targetMethod = target.GetType().GetMember(invocation.Method.Name, flags).First();
                if (IsMarkedNonTransactional(invocation, targetMethod))
                {
                    return await invocation.Proceed();
                }
                else
                {
                    object result = null;

                    if (DynamicExecutionStrategyDbConfiguration.SuspendExecutionStrategy)
                    {
                        using (var trans = _transactionScopeFactory.GetTransactionScope())
                        {
                            result = await invocation.Proceed();
                            trans.Complete();
                        }
                    }
                    else
                    {
                        DynamicExecutionStrategyDbConfiguration.SuspendExecutionStrategy = true;
                        var executionStrategy = new SqlAzureExecutionStrategy(3, TimeSpan.FromMilliseconds(200));

                        await executionStrategy.ExecuteAsync(async() =>
                        {
                            using (var trans = _transactionScopeFactory.GetTransactionScope())
                            {
                                result = await invocation.Proceed();
                                trans.Complete();
                            }
                        }, new System.Threading.CancellationToken());

                        DynamicExecutionStrategyDbConfiguration.SuspendExecutionStrategy = false;
                    }

                    return result;
                }
            }));
        }
Ejemplo n.º 5
0
        public static async Task <int> ExecuteResilientlyAsync(string sql, dynamic param = null, CommandType?commandType = null)
        {
            int rowsAffected      = 0;
            var executionStrategy = new SqlAzureExecutionStrategy();
            await executionStrategy.ExecuteAsync(
                async() =>
            {
                using (var connection = await GetOpenConnectionAsync())
                {
                    //  Outside-initiated user transactions are not supported. See Limitations with Retrying Execution Strategies (EF6 onwards) +http://msdn.microsoft.com/en-us/data/dn307226
                    // +http://entityframework.codeplex.com/wikipage?title=Connection+Resiliency+Spec
                    // To find a workaround, search for SqlAzureExecutionStrategy in the project code.
                    // Example of using ExecuteAsync(). +https://code.google.com/p/dapper-dot-net/source/browse/DapperTests%20NET45/Tests.cs
                    rowsAffected = await SqlMapper.ExecuteAsync(connection, sql, param: param, transaction: null, commandTimeout: null, commandType: commandType);
                }
            },
                // Apparently, CancellationToken is not used. See +https://github.com/Icehunter/entityframework/blob/master/src/EntityFramework.SqlServer/DefaultSqlExecutionStrategy.cs
                CancellationToken.None
                );

            return(rowsAffected);
        }
Ejemplo n.º 6
0
 public async Task InsertUserIDToSQLStorage(string UserID)
 {
     await _sqlAzureExecutionStrategy.ExecuteAsync(async() =>
     {
         await _guardianContext.Database
         .SqlQuery <OpstoolEntity.User>("EXEC [dbo].[InsertUserIDToSQLStorage] @UserID",
                                        new SqlParameter("@UserID", UserID)).FirstOrDefaultAsync();
     }, CancellationToken.None);
 }