/// <summary>
 /// Deprecated Method for adding a new object to the SPECIAL_Test EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToSPECIAL_Test(SPECIAL_Test sPECIAL_Test)
 {
     base.AddObject("SPECIAL_Test", sPECIAL_Test);
 }
 /// <summary>
 /// Create a new SPECIAL_Test object.
 /// </summary>
 /// <param name="id">Initial value of the Id property.</param>
 /// <param name="createdOn">Initial value of the CreatedOn property.</param>
 public static SPECIAL_Test CreateSPECIAL_Test(global::System.Int32 id, global::System.DateTime createdOn)
 {
     SPECIAL_Test sPECIAL_Test = new SPECIAL_Test();
     sPECIAL_Test.Id = id;
     sPECIAL_Test.CreatedOn = createdOn;
     return sPECIAL_Test;
 }
示例#3
0
        /// <summary>
        /// Runs the worker once to do the tests.
        /// </summary>
        public override void RunOnce()
        {
            // If this method is already running, then leave the already running alone, and return.
            // If it is not running, set the value os locker to 1 to indicate that now it is running.
            if (Interlocked.Exchange(ref locker, 1) != 0)
            {
                Trace.TraceInformation("TestWorker.RunOnce(): already running, exiting now.");
                return;
            }

            Trace.TraceInformation("TestWorker.RunOnce(): running service");

            var utcNow = this.GetUtcNow();
            using (var db = this.CreateNewCerebelloEntities())
            {
                // trying to save to the database
                Exception exSaveToDb = null;
                SPECIAL_Test dbObj = null;
                try
                {
                    var o = new SPECIAL_Test
                        {
                            CreatedOn = utcNow,
                        };

                    db.SPECIAL_Test.AddObject(o);

                    db.SaveChanges();

                    dbObj = o;
                }
                catch (Exception ex)
                {
                    exSaveToDb = ex;
                }

                if (exSaveToDb == null)
                    Trace.TraceInformation("TestWorker.RunOnce(): DB object saved");

                // trying to save a file in the storage
                Exception exSaveToStorage = null;
                try
                {
                    var storageManager = new WindowsAzureBlobStorageManager();
                    using (var stream = new MemoryStream(new byte[0]))
                        storageManager.UploadFileToStorage(
                            stream, "worker-test", string.Format("{0}", utcNow.ToString("yyyy'-'MM'-'dd hh'-'mm")));
                }
                catch (Exception ex)
                {
                    exSaveToStorage = ex;
                }

                if (exSaveToStorage == null)
                    Trace.TraceInformation("TestWorker.RunOnce(): blob saved to storage");

                // Sending e-mail about test status
                Exception exSendEmail = null;
                try
                {
                    var obj = new Dictionary<string, Exception>
                        {
                            { "exSaveToDb", exSaveToDb },
                            { "exSaveToStorage", exSaveToStorage },
                        };

                    var mailMessage = this.CreateEmailMessage("TestEmail", new MailAddress("*****@*****.**"), obj);
                    if (!this.TrySendEmail(mailMessage))
                        throw new Exception("Cannot send e-mail message.");
                }
                catch (Exception ex)
                {
                    exSendEmail = ex;
                }

                if (exSendEmail == null)
                    Trace.TraceInformation("TestWorker.RunOnce(): e-mail message sent");

                // Save result to storage
                var fileText = new StringBuilder(1000);
                fileText.AppendLine("File saved from TestWorker");

                if (exSaveToDb != null)
                {
                    fileText.AppendLine();
                    fileText.AppendLine("Save to DB failed: " + exSaveToDb.Message);
                }

                if (exSaveToStorage != null)
                {
                    fileText.AppendLine();
                    fileText.AppendLine("Save to Storage failed: " + exSaveToStorage.Message);
                }

                if (exSendEmail != null)
                {
                    fileText.AppendLine();
                    fileText.AppendLine("Send e-mail failed: " + exSendEmail.Message);
                }

                if (exSaveToStorage == null)
                    try
                    {
                        var storageManager = new WindowsAzureBlobStorageManager();
                        using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(fileText.ToString())))
                            storageManager.UploadFileToStorage(
                                stream, "worker-test", string.Format("{0}", utcNow.ToString("yyyy'-'MM'-'dd hh'-'mm")));
                    }
                    catch
                    {
                    }

                // Save result to db
                if (exSaveToDb == null)
                    try
                    {
                        if (dbObj != null)
                        {
                            dbObj.Value = fileText.ToString();
                            db.SaveChanges();
                        }
                    }
                    catch
                    {
                    }
            }

            // setting locker value to 0
            if (Interlocked.Exchange(ref locker, 0) != 1)
                throw new Exception("The value of locker should be 1 before setting it to 0.");
        }