Esempio n. 1
0
        public async Task SaveThumbnail(int id, string fileName, IDataContent content)
        {
            try
            {
                TransactionOptions topts = new TransactionOptions()
                {
                    Timeout = TimeSpan.FromSeconds(60), IsolationLevel = IsolationLevel.Serializable
                };
                using (TransactionScope trxScope = new TransactionScope(TransactionScopeOption.Required, topts, TransactionScopeAsyncFlowOption.Enabled))
                    using (System.Data.Common.DbConnection conn = _connectionFactory.GetRIAppDemoConnection())
                    {
                        DbContextOptionsBuilder <AdventureWorksLT2012Context> dbOptionsBuilder = new DbContextOptionsBuilder <AdventureWorksLT2012Context>();
                        dbOptionsBuilder.UseSqlServer(conn);
                        // Create in the same transaction !!!
                        using (AdventureWorksLT2012Context db = new AdventureWorksLT2012Context(dbOptionsBuilder.Options))
                        {
                            Product product = await db.Product.Where(a => a.ProductId == id).FirstOrDefaultAsync();

                            if (product == null)
                            {
                                throw new Exception(string.Format("Product {0} is Not Found", id));
                            }

                            using (BlobStream blobStream = new BlobStream(conn as SqlConnection, "[SalesLT].[Product]", "ThumbNailPhoto",
                                                                          string.Format("WHERE [ProductID]={0}", id)))
                                using (BufferedStream bufferedStream = new BufferedStream(blobStream, 128 * 1024))
                                {
                                    await blobStream.InitColumnAsync();

                                    blobStream.Open();
                                    Task delayTask     = Task.Delay(TimeSpan.FromSeconds(15));
                                    Task completedTask = await Task.WhenAny(content.CopyToAsync(bufferedStream), delayTask);

                                    if (completedTask == delayTask)
                                    {
                                        throw new Exception("Saving Image took longer than expected");
                                    }

                                    await bufferedStream.FlushAsync();
                                }

                            product.ThumbnailPhotoFileName = fileName;
                            await db.SaveChangesAsync();

                            trxScope.Complete();
                        }
                    }
            }
            catch (Exception ex)
            {
                string msg = "";
                if (ex != null)
                {
                    msg = ex.GetFullMessage();
                }

                _logger.LogError(ex, msg);
                throw;
            }
        }
Esempio n. 2
0
        public async Task <string> GetThumbnail(int id, Stream strm)
        {
            string fileName = string.Empty;

            try
            {
                TransactionOptions topts = new TransactionOptions()
                {
                    Timeout = TimeSpan.FromSeconds(60), IsolationLevel = IsolationLevel.Serializable
                };
                using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, topts, TransactionScopeAsyncFlowOption.Enabled))
                    using (System.Data.Common.DbConnection conn = _connectionFactory.GetRIAppDemoConnection())
                    {
                        DbContextOptionsBuilder <AdventureWorksLT2012Context> dbOptionsBuilder = new DbContextOptionsBuilder <AdventureWorksLT2012Context>();
                        dbOptionsBuilder.UseSqlServer(conn);
                        // Create in the same transaction and connection!!!
                        using (AdventureWorksLT2012Context db = new AdventureWorksLT2012Context(dbOptionsBuilder.Options))
                        {
                            fileName = await db.Product.Where(a => a.ProductId == id).Select(a => a.ThumbnailPhotoFileName).FirstOrDefaultAsync();

                            if (string.IsNullOrEmpty(fileName))
                            {
                                throw new Exception($"Product: {id} is not found");
                            }

                            using (BlobStream bstrm = new BlobStream(conn as SqlConnection, "[SalesLT].[Product]", "ThumbNailPhoto",
                                                                     string.Format("WHERE [ProductID]={0}", id)))
                            {
                                bstrm.Open();
                                await bstrm.CopyToAsync(strm, 512 * 1024);
                            }

                            scope.Complete();
                        }
                    }

                return(fileName);
            }
            catch (Exception ex)
            {
                string msg = "";
                if (ex != null)
                {
                    msg = ex.GetFullMessage();
                }

                _logger.LogError(ex, msg);
                throw;
            }
        }
Esempio n. 3
0
 public SalesOrderDetailsController(AdventureWorksLT2012Context context)
 {
     _context = context;
 }
 public ProductsController(AdventureWorksLT2012Context context)
 {
     _context = context;
 }
 public CustomersController(AdventureWorksLT2012Context context)
 {
     _context = context;
 }