예제 #1
0
        public void SaveThumbnail(int id, string fileName, System.IO.Stream strm)
        {
            var product = this.DB.Products.Where(a => a.ProductID == id).FirstOrDefault();

            if (product == null)
            {
                throw new Exception("Product is not found");
            }

            TransactionOptions topts = new System.Transactions.TransactionOptions();

            topts.Timeout        = TimeSpan.FromSeconds(60);
            topts.IsolationLevel = System.Transactions.IsolationLevel.Serializable;
            using (TransactionScope trxScope = new TransactionScope(TransactionScopeOption.Required, topts))
                using (DbConnection conn = DBConnectionFactory.GetRIAppDemoConnection())
                {
                    System.IO.BinaryReader br = new System.IO.BinaryReader(strm);
                    byte[]     bytes          = br.ReadBytes(64 * 1024);
                    string     fldname        = "ThumbNailPhoto";
                    BlobStream bstrm          = new BlobStream(conn as SqlConnection, "[SalesLT].[Product]", fldname, string.Format("WHERE [ProductID]={0}", id));
                    bstrm.InitColumn();
                    bstrm.Open();
                    while (bytes != null && bytes.Length > 0)
                    {
                        bstrm.Write(bytes, 0, bytes.Length);
                        bytes = br.ReadBytes(64 * 1024);;
                    }
                    bstrm.Close();
                    br.Close();
                    trxScope.Complete();
                }

            product.ThumbnailPhotoFileName = fileName;
            this.DB.SubmitChanges();
        }
예제 #2
0
        public string GetThumbnail(int id, System.IO.Stream strm)
        {
            string fileName = this.DB.Products.Where(a => a.ProductID == id).Select(a => a.ThumbnailPhotoFileName).FirstOrDefault();

            if (string.IsNullOrEmpty(fileName))
            {
                return("");
            }
            System.Transactions.TransactionOptions top = new System.Transactions.TransactionOptions();
            top.Timeout        = TimeSpan.FromSeconds(60);
            top.IsolationLevel = System.Transactions.IsolationLevel.Serializable;

            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, top))
                using (DbConnection conn = DBConnectionFactory.GetRIAppDemoConnection())
                {
                    byte[] bytes = new byte[64 * 1024];

                    string     fldname = "ThumbNailPhoto";
                    BlobStream bstrm   = new BlobStream(conn as SqlConnection, "[SalesLT].[Product]", fldname, string.Format("WHERE [ProductID]={0}", id));
                    bstrm.Open();
                    int cnt = bstrm.Read(bytes, 0, bytes.Length);
                    while (cnt > 0)
                    {
                        strm.Write(bytes, 0, cnt);
                        cnt = bstrm.Read(bytes, 0, bytes.Length);
                    }
                    bstrm.Close();
                    scope.Complete();
                }
            return(fileName);
        }
예제 #3
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;
            }
        }
예제 #4
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;
            }
        }
예제 #5
0
        public void SaveThumbnail2(int id, string fileName, Func <Stream, Task> copy)
        {
            var product = DB.Products.Where(a => a.ProductID == id).FirstOrDefault();

            if (product == null)
            {
                throw new Exception("Product is not found");
            }

            var topts = new TransactionOptions();

            topts.Timeout        = TimeSpan.FromSeconds(60);
            topts.IsolationLevel = IsolationLevel.Serializable;
            using (var trxScope = new TransactionScope(TransactionScopeOption.Required, topts))
                using (var conn = DBConnectionFactory.GetRIAppDemoConnection())
                {
                    var fldname = "ThumbNailPhoto";
                    var bstrm   = new BlobStream(conn as SqlConnection, "[SalesLT].[Product]", fldname,
                                                 string.Format("WHERE [ProductID]={0}", id));
                    bstrm.InitColumn();
                    bstrm.Open();
                    try
                    {
                        if (!copy(bstrm).Wait(10000))
                        {
                            throw new Exception("Write stream timeout");
                        }
                    }
                    finally
                    {
                        bstrm.Close();
                    }
                    trxScope.Complete();
                }

            product.ThumbnailPhotoFileName = fileName;
            DB.SaveChanges();
        }