public ActionResult Publish(TemplatePublisherModel templatePublisher, HttpPostedFileBase file, string tenantId)
        {
            CloudBlockBlob originalFileBlob = null;
            PublishModel publishJob = new PublishModel();
            try
            {
                if (file == null)
                {
                    ModelState.AddModelError(string.Empty, "Please provide file path");
                }

                //create an instance of templateModel from inputs
                IEnumerable<TemplateModel> templatesFromStorage = TemplateModel.GetFromStorage(tenantId);
                templatePublisher.Template = templatesFromStorage.Single<TemplateModel>(x => string.Compare(x.TemplateId, templatePublisher.Template.TemplateId, 
                    StringComparison.OrdinalIgnoreCase) == 0);

                publishJob.TenantId = tenantId;
                publishJob.OriginalFileName = file.FileName;
                publishJob.TemplateId = templatePublisher.Template.TemplateId;
                publishJob.OriginalFileSizeInBytes = file.ContentLength;
                publishJob.SaveToStorage();

                originalFileBlob = DataModel.StorageFactory.Instance.IpcAzureAppFileBlobContainer.GetBlockBlobReference(publishJob.OriginalFileBlobRef);

                // Create the blob by uploading the original file.
                using (var fileStream = file.InputStream)
                {
                    originalFileBlob.UploadFromStream(fileStream);
                }

                //Create a command message for the worker role and send it by queue
                RmsCommand rmsCommand = new RmsCommand(RmsCommand.Command.PublishTemplate, tenantId, publishJob.OriginalFileBlobRef);
                CloudQueueMessage cloudQueueMessage = new CloudQueueMessage(rmsCommand.ToString());
                DataModel.StorageFactory.Instance.IpcAzureAppWorkerJobQueue.AddMessage(cloudQueueMessage);

                //Poll for completion of job by worker role. Don't poll for more than a minute
                DateTime startTime = DateTime.Now;
                PublishModel pJob = publishJob;
                while (startTime.AddMinutes(1) > DateTime.Now &&
                    string.Compare(pJob.JState.ToString(), DataModel.Models.PublishModel.JobState.Completed.ToString(), true) != 0)
                {
                    System.Threading.Thread.Sleep(1 * 100);
                    pJob = DataModel.Models.PublishModel.GetFromStorage(publishJob.TenantId, publishJob.OriginalFileBlobRef);
                }

                //send the published file to the user
                CloudBlockBlob publishedFileblob = DataModel.StorageFactory.Instance.IpcAzureAppFileBlobContainer.GetBlockBlobReference(pJob.PublishedFileBlobRef);
                return File(publishedFileblob.OpenRead(), "application/force-download", pJob.PublishedFileName);
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.Message);
                return View("Error");
            }
        }
        public ActionResult Index(TemplatePublisherModel templatePublisherModel)
        {
            try
            {
                //check if the templates already exist in table cache
                ServicePrincipalModel servicePrincipal = ServicePrincipalModel.GetFromStorage(templatePublisherModel.ServicePrincipal.TenantId);
                IEnumerable<TemplateModel> templates = TemplateModel.GetFromStorage(templatePublisherModel.ServicePrincipal.TenantId);
                if (templates == null || templates.Count<TemplateModel>() == 0)
                {
                    //prepare a message and send via queue to worker role
                    RmsCommand rmsCommand = new RmsCommand(RmsCommand.Command.GetTemplate, servicePrincipal.TenantId);
                    CloudQueueMessage cloudQueueMessage = new CloudQueueMessage(rmsCommand.ToString());
                    DataModel.StorageFactory.Instance.IpcAzureAppWorkerJobQueue.AddMessage(cloudQueueMessage);

                    TemplateModel template = new TemplateModel();
                    template.TenantId = servicePrincipal.TenantId;

                    //Poll for completetion of job by worker role. Don't poll for more than a minute
                    DateTime startTime = DateTime.Now;
                    IEnumerable<TemplateModel> tList = null;
                    while (startTime.AddMinutes(1) > DateTime.Now)
                    {
                        System.Threading.Thread.Sleep(1 * 500);
                        tList = TemplateModel.GetFromStorage(template.TenantId);
                        if (tList != null && tList.Count<TemplateModel>() > 0)
                        {
                            templates = tList;
                            break;
                        }
                    }
                }
                templatePublisherModel.Templates = templates;
                templatePublisherModel.ServicePrincipal.TenantName = servicePrincipal.TenantName;
                templatePublisherModel.ServicePrincipal.TenantId = servicePrincipal.TenantId;
                return View(templatePublisherModel);
            }
            catch (StorageException se)
            {
                ViewBag.errorMessage = "Timeout error, try again. ";
                Trace.TraceError(se.Message);
                return View("Error");
            }
        }
 /// <summary>
 /// Default get request
 /// </summary>
 /// <returns>Action Result</returns>
 public ActionResult Index()
 {
     try
     {
         TemplatePublisherModel templatePublisherModel = new TemplatePublisherModel();
         IEnumerable<ServicePrincipalModel> servicePrincipals = ServicePrincipalModel.GetAllFromStorage();
         if (servicePrincipals == null || servicePrincipals.Count<ServicePrincipalModel>() == 0)
         {
             return RedirectToAction("Index", "ServicePrincipal");
         }
         else
         {
             templatePublisherModel.ServicePrincipals = servicePrincipals;
             return View(templatePublisherModel);
         }
     }
     catch (StorageException se)
     {
         ViewBag.errorMessage = "Timeout error, try again. ";
         Trace.TraceError(se.Message);
         return View("Error");
     }
 }