Exemplo n.º 1
0
        static void Main(string[] args)
        {
            SqlParameter parameter = new SqlParameter();

            parameter.SqlDbType     = System.Data.SqlDbType.DateTime;
            parameter.ParameterName = "@travel_date";
            parameter.Value         = DateTime.UtcNow.AddHours(8);

            List <RTServiceAlerts> serviceAlerts = db.Database
                                                   .SqlQuery <RTServiceAlerts>("RTServiceAlertsGetActive @travel_date"
                                                                               , parameter)
                                                   .ToList();

            FeedMessage feed = new FeedMessage();

            if (serviceAlerts.Count > 0)
            {
                foreach (RTServiceAlerts serviceAlert in serviceAlerts)
                {
                    EntitySelector entitySel = new EntitySelector();
                    entitySel.RouteId = serviceAlert.route_id;
                    entitySel.StopId  = serviceAlert.stop_id;

                    Alert alert = new Alert();
                    alert.HeaderText      = Functions.GenerateTranslatedString(serviceAlert.description);
                    alert.DescriptionText = Functions.GenerateTranslatedString(serviceAlert.header);
                    alert.InformedEntities.Add(entitySel);
                    //alert.Url = GenerateTranslatedString(serviceAlert.id.ToString());

                    TimeRange tr = new TimeRange();
                    tr.Start = Functions.ToEpoch(serviceAlert.start_date.AddHours(-8));
                    tr.End   = Functions.ToEpoch(serviceAlert.end_date.AddHours(-8));

                    alert.ActivePeriods.Add(tr);

                    FeedEntity entity = new FeedEntity();
                    entity.Alert = alert;
                    entity.Id    = serviceAlert.id.ToString();

                    feed.Entities.Add(entity);
                }

                byte[] objSerialized = Functions.ProtoSerialize(feed);

                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
                string filename = "alert.pb";


                // Create a CloudFileClient object for credentialed access to Azure Files.
                CloudFileClient fileClient = storageAccount.CreateCloudFileClient();

                // Get a reference to the file share we created previously.
                CloudFileShare share = fileClient.GetShareReference("gtfsrt");
                share.CreateIfNotExists();

                var rootDir = share.GetRootDirectoryReference();
                using (var stream = new MemoryStream(objSerialized, writable: false))
                {
                    rootDir.GetFileReference(filename).UploadFromStream(stream);//.UploadFromByteArray(feed,);
                }
            }

            //byte[] objSerialized = Functions.ProtoSerialize(feed);

            //CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
            //string filename = "alert.pb";
            //Functions.CreatePBFile(storageAccount, filename, objSerialized);
        }
Exemplo n.º 2
0
        // delete a blob from storage
        public static bool DeleteBlob(string blobName, string containerName)
        {
            CloudStorageAccount storageAccount = CreateStorageAccountFromConnectionString(CloudConfigurationManager.GetSetting("StorageConnectionString"));
            CloudBlobClient     blobClient     = storageAccount.CreateCloudBlobClient();

            return(blobClient.GetContainerReference(containerName).GetBlockBlobReference(blobName).DeleteIfExists());
        }
 public MainWindow()
 {
     InitializeComponent();
     _connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
 }
Exemplo n.º 4
0
        public async Task <CloudTable> CreateTableAsync()
        {
            CloudStorageAccount storageAccount = CreateStorageAccountFromConnectionString(CloudConfigurationManager.GetSetting("StorageConnectionString"));

            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

            Console.WriteLine("1. Create a Table for the demo");

            CloudTable table = tableClient.GetTableReference(TableName);

            try
            {
                if (await table.CreateIfNotExistsAsync())
                {
                    Console.WriteLine("Created Table named: {0}", TableName);
                }
                else
                {
                    Console.WriteLine("Table {0} already exists", TableName);
                }
            }
            catch (StorageException ex)
            {
                Console.WriteLine(ex);
                Console.ReadLine();
                throw;
            }

            return(table);
        }
Exemplo n.º 5
0
        static KeyVaultAccessor()
        {
            keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(GetAccessToken));
            var clientAssertionCertPfx = CertificateHelper.FindCertificateByThumbprint(CloudConfigurationManager.GetSetting(Constants.KeyVaultAuthCertThumbprintSetting));
            var client_id = CloudConfigurationManager.GetSetting(Constants.KeyVaultAuthClientIdSetting);

            assertionCert = new ClientAssertionCertificate(client_id, clientAssertionCertPfx);
        }
        /// <summary>
        /// Create a queue for the sample application to process messages in.
        /// </summary>
        /// <returns>A CloudQueue object</returns>
        private static async Task <CloudQueue> CreateQueueAsync()
        {
            // Retrieve storage account information from connection string.
            CloudStorageAccount storageAccount = CreateStorageAccountFromConnectionString(CloudConfigurationManager.GetSetting("StorageConnectionString"));

            // Create a queue client for interacting with the queue service
            CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();

            Console.WriteLine("1. Create a queue for the demo");
            CloudQueue queue = queueClient.GetQueueReference("samplequeue");

            try
            {
                await queue.CreateIfNotExistsAsync();
            }
            catch (StorageException ex)
            {
                Console.WriteLine("If you are running with the default configuration please make sure you have started the storage emulator. Press the Windows key and type Azure Storage to select and run it from the list of applications - then restart the sample.");
                Console.ReadLine();
                throw;
            }

            return(queue);
        }
        private static async Task<OcrResults> DetectText(string imageUrl, string VisionServiceApiKey, TraceWriter log)
        {
            VisionServiceClient visionServiceClient = new VisionServiceClient(VisionServiceApiKey);

            int retriesLeft = int.Parse(CloudConfigurationManager.GetSetting("CognitiveServicesRetryCount"));
            int delay = int.Parse(CloudConfigurationManager.GetSetting("CognitiveServicesInitialRetryDelayms"));

            OcrResults response = null;
            while (true)
            {
                try
                {
                    response = await visionServiceClient.RecognizeTextAsync(imageUrl);
                    break;
                }
                catch (ClientException exception) when (exception.HttpStatus == (HttpStatusCode)429 && retriesLeft > 0)
                {
                    log.Info($"Computer Vision OCR call has been throttled. {retriesLeft} retries left.");
                    if (retriesLeft == 1)
                    {
                        log.Warning($"Computer Vision OCR call still throttled after {CloudConfigurationManager.GetSetting("CognitiveServicesRetryCount")} attempts, giving up.");
                    }

                    await Task.Delay(delay);
                    retriesLeft--;
                    delay *= 2;
                    continue;
                }
            }

            return response;
        }
 public ContainingClass()
 {
     _databaseId = CloudConfigurationManager.GetSetting("database");
 }
Exemplo n.º 9
0
        private CloudBlobContainer GetCloudBlobContainer()
        {
            CloudStorageAccount storageAccount = CreateStorageAccountFromConnectionString(CloudConfigurationManager.GetSetting("StorageConnectionString"));

            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            CloudBlobContainer container = blobClient.GetContainerReference("democontainerblockblob");

            return(container);
        }
Exemplo n.º 10
0
        //*********************************************************************
        ///
        /// <summary>
        ///
        /// </summary>
        ///
        //*********************************************************************

        bool JoinDomain()
        {
            string currectDomainName = null;

            try
            {
                if (ActiveDirLib.DomainJoin.IsDomainJoined(out currectDomainName))
                {
                    if (null != _EventLog)
                    {
                        _EventLog.WriteEntry("Domain Joined : " + currectDomainName, EventLogEntryType.Information, 10, 1);
                    }

                    return(false);
                }
            }
            catch (Exception ex)
            {
                if (null != _EventLog)
                {
                    _EventLog.WriteEntry("Exception while detecting domain : " +
                                         CmpCommon.Utilities.UnwindExceptionMessages(ex), EventLogEntryType.Error, 100, 100);
                }
                return(true);
            }

            try
            {
                if (null != _EventLog)
                {
                    _EventLog.WriteEntry("Joining domain", EventLogEntryType.Information, 10, 1);
                }

                var domainName   = CloudConfigurationManager.GetSetting("CmpWorker.DomainJoin.DomainName");
                var userName     = CloudConfigurationManager.GetSetting("CmpWorker.DomainJoin.UserName");
                var userPassword = CloudConfigurationManager.GetSetting("CmpWorker.DomainJoin.UserPassword");
                //string destinationOU = CloudConfigurationManager.GetSetting("CmpWorker.DomainJoin.OU");
                string destinationOU = null;

                using (var XK = new KryptoLib.X509Krypto())
                    userPassword = XK.DecrpytKText(userPassword);

                ActiveDirLib.DomainJoin.Join(domainName, userPassword, userName, destinationOU);
            }
            catch (Exception ex)
            {
                if (null != _EventLog)
                {
                    _EventLog.WriteEntry("Exception while joining domain : " +
                                         CmpCommon.Utilities.UnwindExceptionMessages(ex), EventLogEntryType.Error, 100, 100);
                }
                return(true);
            }

            try
            {
                Thread.Sleep(20000);

                if (null != _EventLog)
                {
                    _EventLog.WriteEntry("Restarting after joining domain", EventLogEntryType.Information, 10, 1);
                }

                System.Diagnostics.Process.Start("shutdown", "/r");
            }
            catch (Exception ex)
            {
                if (null != _EventLog)
                {
                    _EventLog.WriteEntry("Exception while restarting after joining domain : " +
                                         CmpCommon.Utilities.UnwindExceptionMessages(ex), EventLogEntryType.Error, 100, 100);
                }
                return(true);
            }

            return(true);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Check if given connection string is for Azure Table storage or Azure CosmosDB Table.
        /// </summary>
        /// <returns>true if azure cosmosdb table</returns>
        public static bool IsAzureCosmosdbTable()
        {
            string connectionString = CloudConfigurationManager.GetSetting("StorageConnectionString");

            return(!String.IsNullOrEmpty(connectionString) && connectionString.Contains("table.cosmosdb"));
        }
Exemplo n.º 12
0
        public virtual async Task <HttpResponseMessage> Post([FromBody] Microsoft.Bot.Connector.Activity message)
        {
            ConnectorClient connector = new ConnectorClient(new Uri(message.ServiceUrl));

            IBotDataStore <BotData> dataStore = botStateStore;

            storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("piglatinbotsjameslew_AzureStorageConnectionString"));
            botStateStore  = new TableBotDataStore(storageAccount, "botdata");

            Microsoft.Bot.Connector.Activity replyMessage = message.CreateReply();
            replyMessage.Locale     = "en-Us";
            replyMessage.TextFormat = TextFormatTypes.Plain;

            if (message.GetActivityType() != ActivityTypes.Message)
            {
                replyMessage = await handleSystemMessagesAsync(message, connector, botStateStore);

                if (replyMessage != null)
                {
                    var reply = await connector.Conversations.ReplyToActivityAsync(replyMessage);
                }
            }
            else
            {
                if (message.Text.Contains("MessageTypesTest"))
                {
                    Microsoft.Bot.Connector.Activity mtResult = await messageTypesTest(message, connector);

                    await connector.Conversations.ReplyToActivityAsync(mtResult);
                }
                else if (message.Text.Contains("DataTypesTest"))
                {
                    Microsoft.Bot.Connector.Activity dtResult = await dataTypesTest(message, connector, botStateStore);

                    await connector.Conversations.ReplyToActivityAsync(dtResult);
                }
                else if (message.Text.Contains("CardTypesTest"))
                {
                    Microsoft.Bot.Connector.Activity ctResult = await cardTypesTest(message, connector);

                    await connector.Conversations.ReplyToActivityAsync(ctResult);
                }

                try
                {
                    if (await isNewUser(message.From.Id, message, botStateStore))
                    {
                        Microsoft.Bot.Connector.Activity introMessage = message.CreateReply();
                        introMessage.Locale     = "en-Us";
                        introMessage.TextFormat = TextFormatTypes.Plain;
                        introMessage.InputHint  = InputHints.IgnoringInput;

                        introMessage.Text = string.Format(translateToPigLatin("Hey there, I'm PigLatinBot. I make intelligible text unintelligible.  Ask me how by typing 'Help', and for terms and info, click ") + "[erehay](http://www.piglatinbot.com)", message.From.Name);
                        var reply = await connector.Conversations.ReplyToActivityAsync(introMessage);
                    }
                    replyMessage.InputHint = InputHints.AcceptingInput;
                    replyMessage.Speak     = message.Text;
                    replyMessage.Text      = translateToPigLatin(message.Text);
                    var httpResponse = await connector.Conversations.ReplyToActivityAsync(replyMessage);
                }
                catch (HttpResponseException e)
                {
                    Trace.WriteLine(e.Message);
                    var Response = Request.CreateResponse(HttpStatusCode.InternalServerError);
                    return(Response);
                }
            }
            var responseOtherwise = Request.CreateResponse(HttpStatusCode.OK);

            return(responseOtherwise);
        }
        protected IConfigurationRepository GetDataFromAzure()
        {
            IConfigurationRepository configurationRepository;

            if (bool.Parse(ConfigurationManager.AppSettings["LocalConfig"]))
            {
                configurationRepository = new FileStorageConfigurationRepository();
            }
            else
            {
                configurationRepository = new AzureTableStorageConfigurationRepository(CloudConfigurationManager.GetSetting("ConfigurationStorageConnectionString"));
            }
            return(configurationRepository);
        }
Exemplo n.º 14
0
        protected async Task ProcessAsync()
        {
            CloudStorageAccount storageAccount     = null;
            CloudBlobContainer  cloudBlobContainer = null;
            string sourceFile      = null;
            string destinationFile = null;


            // Retrieve the connection string for use with the application. The storage connection string is stored
            // in an environment variable on the machine running the application called storageconnectionstring.
            // If the environment variable is created after the application is launched in a console or with Visual
            // Studio, the shell needs to be closed and reloaded to take the environment variable into account.
            //string storageConnectionString = Environment.GetEnvironmentVariable("storageconnectionstring");
            string storageConnectionString = CloudConfigurationManager.GetSetting("AzureStorageLogTableConnectionString");

            // Check whether the connection string can be parsed.
            if (CloudStorageAccount.TryParse(storageConnectionString, out storageAccount))
            {
                try
                {
                    // Create the CloudBlobClient that represents the Blob storage endpoint for the storage account.
                    CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();

                    cloudBlobContainer = cloudBlobClient.GetContainerReference("helloworld" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"));
                    await cloudBlobContainer.CreateAsync();

                    azurelog.Info("Created container " + cloudBlobContainer.Name);

                    // Set the permissions so the blobs are public.
                    BlobContainerPermissions permissions = new BlobContainerPermissions
                    {
                        PublicAccess = BlobContainerPublicAccessType.Blob
                    };
                    await cloudBlobContainer.SetPermissionsAsync(permissions);

                    // Create a file in your local MyDocuments folder to upload to a blob.
                    string localPath     = "D:\\home\\site\\wwwroot\\temp";
                    string localFileName = System.Net.Dns.GetHostName() + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".txt";
                    sourceFile = Path.Combine(localPath, localFileName);
                    // Write text to the file.
                    File.WriteAllText(sourceFile, localFileName);

                    azurelog.Info("Temp file =" + sourceFile);
                    azurelog.Info("Uploading to Blob storage as blob " + localFileName);

                    // Get a reference to the blob address, then upload the file to the blob.
                    // Use the value of localFileName for the blob name.
                    CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(localFileName);
                    await cloudBlockBlob.UploadFromFileAsync(sourceFile);

                    // List the blobs in the container.
                    azurelog.Info("Listing blobs in container.");
                    BlobContinuationToken blobContinuationToken = null;
                    do
                    {
                        var results = await cloudBlobContainer.ListBlobsSegmentedAsync(null, blobContinuationToken);

                        // Get the value of the continuation token returned by the listing call.
                        blobContinuationToken = results.ContinuationToken;
                        foreach (IListBlobItem item in results.Results)
                        {
                            azurelog.Info(item.Uri);
                        }
                    } while (blobContinuationToken != null); // Loop while the continuation token is not null.

                    // Download the blob to a local file, using the reference created earlier.
                    // Append the string "_DOWNLOADED" before the .txt extension so that you can see both files in MyDocuments.
                    destinationFile = sourceFile.Replace(".txt", "_download.txt");
                    await cloudBlockBlob.DownloadToFileAsync(destinationFile, FileMode.Create);
                }
                catch (StorageException ex)
                {
                    azurelog.Info("Error returned from the service:" + ex.Message);
                }
                finally
                {
                    // Clean up resources. This includes the container and the two temp files.
                    //azurelog.Info("Deleting the container and any blobs it contains");
                    //if (cloudBlobContainer != null)
                    //{
                    //    await cloudBlobContainer.DeleteIfExistsAsync();
                    //}
                    //azurelog.Info("Deleting the local source file and local downloaded files");
                    //File.Delete(sourceFile);
                    //File.Delete(destinationFile);
                }
            }
            else
            {
                azurelog.Info(
                    "A connection string has not been defined in the system environment variables. " +
                    "Add a environment variable named 'storageconnectionstring' with your storage " +
                    "connection string as a value.");
            }
        }
Exemplo n.º 15
0
        public static string GenerateThumbnails(string iPropId, string OrigFile)
        {
            int    iSize          = 180;
            string sNewFileName   = "";
            string sFileExtension = OrigFile.Substring(OrigFile.LastIndexOf("."));

            string[] sFileName = sNewFileName.Split('.');
            sNewFileName = sFileName[0] + fnSizeName(iSize);

            ImageFormat format = null;

            if (sFileName[1].ToUpper() == ".GIF")
            {
                format = ImageFormat.Gif;
            }
            else if (sFileName[1].ToUpper() == ".JPG" || sFileName[1].ToUpper() == ".JPEG")
            {
                format = ImageFormat.Jpeg;
            }
            else
            {
                format = ImageFormat.Png;
            }

            Image originalImage = null;

            originalImage = Image.FromFile(OrigFile);
            // Compute thumbnail size.

            Size thumbnailSize = GetThumbnailSize(originalImage, iSize);

            Bitmap   originBmp  = new Bitmap(OrigFile);
            int      w          = thumbnailSize.Width;  //originBmp.Width / iSize;
            int      h          = thumbnailSize.Height; // originBmp.Height / iSize;
            Bitmap   resizedBmp = new Bitmap(w, h);
            Graphics g          = Graphics.FromImage(resizedBmp);

            // g.Clear(Color.Transparent);

            // Set high quality interpolation method
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            // Set the high-quality, low-speed rendering the degree of smoothing
            g.SmoothingMode      = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            // Anti-aliasing
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.DrawImage(originBmp, new Rectangle(0, 0, w, h), new Rectangle(0, 0, originBmp.Width, originBmp.Height), GraphicsUnit.Pixel);

            // resizedBmp.Save(reSizePicPath, format);
            CloudStorageAccount      accnt  = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
            CloudBlobClient          client = accnt.CreateCloudBlobClient();
            BlobContainerPermissions containerPermissions = new BlobContainerPermissions();

            containerPermissions.PublicAccess = BlobContainerPublicAccessType.Blob;
            CloudBlobContainer Container = client.GetContainerReference(iPropId);

            Container.CreateIfNotExist();
            Container.SetPermissions(containerPermissions);

            CloudBlob blob = null;

            if (sNewFileName.IndexOf(".") > 0)
            {
                blob = Container.GetBlobReference(sNewFileName);
            }
            else
            {
                blob = Container.GetBlobReference(sNewFileName);
            }

            Stream msicon = null;

            resizedBmp.Save(msicon, format);
            blob.UploadFromStream(msicon);


            g.Dispose();
            resizedBmp.Dispose();
            originBmp.Dispose();
            return(blob.Uri.AbsoluteUri.ToString());
        }
Exemplo n.º 16
0
 public AzureStorage()
 {
     StorageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
 }
Exemplo n.º 17
0
 protected virtual void Init( )
 {
     ConnectionString = CloudConfigurationManager.GetSetting("app:PacsDataArchieve");
     StorageConection = CloudConfigurationManager.GetSetting("app:PacsStorageConnection");
     DataAccess       = new SqlObjectArchieveDataAccess(ConnectionString);
 }
Exemplo n.º 18
0
        public async Task <HttpResponseMessage> ProcessIdcardPostAsync()
        {
            InitEnvironment();

            #region Step1-Step2. 첨부된 파일을 Web App의 Map Path에 복사하고, 이를 Blob Container에 업로드
            MultipartFormdataStreamBlobUploader multipartFormdataStreamBlobUploader = new MultipartFormdataStreamBlobUploader(provider, storageAccount, container);
            listUrlBlob = await multipartFormdataStreamBlobUploader.UploadAttachedFileToBlobContainer(this.Request, blobPrefixString);

            #endregion

            #region Step3. 저장한 blob 위치를 인지서비스에 전달하여 OCR 및 Face 정보 추출
            try
            {
                foreach (UrlBlob urlBlob in listUrlBlob)
                {
                    //OCR 호출
                    List <string> contentsOcr = await CognitiveServicesCallHelper.CognitiveServicePostAsync(
                        CloudConfigurationManager.GetSetting("CognitiveServicesKeyVision"),
                        "https://eastasia.api.cognitive.microsoft.com/vision/v1.0/ocr?language=ko&detectOrientation=true",
                        urlBlob.url);

                    //OCR 결과를 건별로 Queue에 넣음, trace 표시
                    foreach (string content in contentsOcr)
                    {
                        CloudQueueMessage message = new CloudQueueMessage(content);
                        queue.AddMessage(message);

                        Trace.WriteLine("OCR: " + content);
                    }

                    //Face Detection 호출
                    List <string> contentsFace = await CognitiveServicesCallHelper.CognitiveServicePostAsync(
                        CloudConfigurationManager.GetSetting("CognitiveServicesKeyVision"),
                        "https://eastasia.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceAttributes=age,gender,headPose,glasses,accessories",
                        urlBlob.url);

                    //Face 결과를 trace 표시
                    foreach (string content in contentsFace)
                    {
                        List <FaceDetectResult> faceDetectResults = JsonConvert.DeserializeObject <List <FaceDetectResult> >(content);

                        if (faceDetectResults.Count > 0)
                        {
                            Trace.WriteLine("Face: " + content);
                            Trace.WriteLine("FaceId: " + faceDetectResults[0].faceId);

                            HttpResponseMessage message = Request.CreateResponse(HttpStatusCode.OK, new JsonFaceId(faceDetectResults[0].faceId));

                            return(message);
                        }
                    }
                }
                // return empty FaceId if no faces were found.
                return(Request.CreateResponse(HttpStatusCode.OK, new JsonFaceId("")));
            }
            catch (Exception e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e));
            }
            #endregion
        }
Exemplo n.º 19
0
 public ModeloAnimalesStorage()
 {
     //CREAMOS EL OBJETO STORAGE A PARTIR DE LA CADENA DE CONEXION
     storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
 }
Exemplo n.º 20
0
        private static void LoadConfiguration()
        {
            var fileDropPath = CloudConfigurationManager.GetSetting("FileDropPath");

            if (String.IsNullOrEmpty(fileDropPath))
            {
                throw new Exception("FileDropPath is not set in configuration");
            }
            if (!Directory.Exists(fileDropPath))
            {
                throw new Exception(String.Format("FileDropPath - Directory {0} does not exist!", fileDropPath));
            }
            Global.FileDropPath = fileDropPath;

            var textAnalyticsSubscriptionKey = CloudConfigurationManager.GetSetting("TextAnalyticsSubscriptionKey");

            if (String.IsNullOrEmpty(textAnalyticsSubscriptionKey))
            {
                throw new Exception("TextAnalyticsSubscriptionKey is not set in configuration");
            }
            Global.TextAnalyticsSubscriptionKey = textAnalyticsSubscriptionKey;

            var bingSpeechApiSubscriptionKey = CloudConfigurationManager.GetSetting("BingSpeechApiSubscriptionKey");

            if (String.IsNullOrEmpty(bingSpeechApiSubscriptionKey))
            {
                throw new Exception("BingSpeechApiSubscriptionKey is not set in configuration");
            }
            Global.BingSpeechApiSubscriptionKey = bingSpeechApiSubscriptionKey;

            var fileDropArchivePath = CloudConfigurationManager.GetSetting("FileDropArchivePath");

            if (String.IsNullOrEmpty(fileDropArchivePath))
            {
                throw new Exception("FileDropArchivePath is not set in configuration");
            }
            if (!Directory.Exists(fileDropArchivePath))
            {
                throw new Exception(String.Format("FileDropArchivePath - Directory {0} does not exist!", fileDropPath));
            }
            Global.FileDropArchivePath = fileDropArchivePath;

            var batchSizeString = CloudConfigurationManager.GetSetting("BatchSize");

            Int32.TryParse(batchSizeString, out int batchSize);
            if (batchSize == 0)
            {
                batchSize = 10;
            }
            Global.BatchSize = batchSize;

            var maxDocumentSizeInCharactersString = CloudConfigurationManager.GetSetting("MaxDocumentSizeInCharacters");

            Int32.TryParse(maxDocumentSizeInCharactersString, out int maxDocumentSizeInCharacters);
            if (maxDocumentSizeInCharacters == 0)
            {
                maxDocumentSizeInCharacters = 5000;
            }
            Global.MaxDocumentSizeInCharacters = maxDocumentSizeInCharacters;

            var NegativeString = CloudConfigurationManager.GetSetting("Sentiment_Negative");

            Single.TryParse(NegativeString, out Single negative);
            if (negative == 0)
            {
                negative = (Single)0.4;
            }
            Global.Sentiment_Negative = negative;

            var IndifferentString = CloudConfigurationManager.GetSetting("Sentiment_Indifferent");

            Single.TryParse(IndifferentString, out Single indifferent);
            if (indifferent == 0)
            {
                indifferent = (Single)0.6;
            }
            Global.Sentiment_Indifferent = indifferent;

            var twitterConsumerKey = CloudConfigurationManager.GetSetting("TwitterConsumerKey");

            if (String.IsNullOrEmpty(twitterConsumerKey))
            {
                throw new Exception("TwitterConsumerKey is not set in configuration");
            }
            Global.TwitterConsumerKey = twitterConsumerKey;

            var twitterConsumerSecret = CloudConfigurationManager.GetSetting("TwitterConsumerSecret");

            if (String.IsNullOrEmpty(twitterConsumerSecret))
            {
                throw new Exception("TwitterConsumerSecret is not set in configuration");
            }
            Global.TwitterConsumerSecret = twitterConsumerSecret;

            var twitterOAuthUrl = CloudConfigurationManager.GetSetting("TwitterOAuthUrl");

            if (String.IsNullOrEmpty(twitterOAuthUrl))
            {
                throw new Exception("TwitterOAuthUrl is not set in configuration");
            }
            Global.TwitterOAuthUrl = twitterOAuthUrl;
        }
Exemplo n.º 21
0
        public void SchedulePolling(IHubCommunicator hubCommunicator, string externalAccountId)
        {
            string pollingInterval = CloudConfigurationManager.GetSetting("terminalDocuSign.PollingInterval");

            hubCommunicator.ScheduleEvent(externalAccountId, pollingInterval);
        }
Exemplo n.º 22
0
        static void Main(string[] args)
        {
            if (args == null || !args.Any() || args[0] == "help" || args[0] == "man")
            {
                Console.WriteLine("UploadToAzureStorage [TargetDirectory]");
                return;
            }

            var targetDirectory = args[0];

            if (!Directory.Exists(targetDirectory))
            {
                Console.WriteLine("Directory not found. TargetDirectory:{0}", targetDirectory);
                return;
            }

            var storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
            var blobClient     = storageAccount.CreateCloudBlobClient();
            var containerName  = CloudConfigurationManager.GetSetting("container");
            var container      = blobClient.GetContainerReference(containerName);

            var beforeExistsBlobUris = container.ListBlobs(null, true, BlobListingDetails.All)
                                       .Select(x => x.Uri.ToString());

            var files = Directory.GetFiles(targetDirectory, "*", SearchOption.AllDirectories);

            Parallel.ForEach(files, new ParallelOptions {
                MaxDegreeOfParallelism = 100
            }, file =>
            {
                try
                {
                    var assetBundlePath = file.Replace(targetDirectory, "").Replace(@"\", "/");
                    if (assetBundlePath.StartsWith("/"))
                    {
                        assetBundlePath = assetBundlePath.Substring(1);
                    }

                    var blockBlob = container.GetBlockBlobReference(assetBundlePath);
                    using (var fileStream = File.OpenRead(file))
                    {
                        var stopwatch = Stopwatch.StartNew();

                        stopwatch.Start();
                        blockBlob.UploadFromStream(fileStream);
                        stopwatch.Stop();

                        UploadedUriBag.Add(blockBlob.Uri.ToString());

                        var logMessage = GetStats(blockBlob, stopwatch.ElapsedMilliseconds, "Uploaded");
                        Console.WriteLine(logMessage);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Upload {0} Error {1}{2}{3}",
                                      file, ex.Message, Environment.NewLine, ex.StackTrace);
                }
            });

            Parallel.ForEach(
                beforeExistsBlobUris
                .Where(x => !UploadedUriBag.Contains(x))
                .Select(x => container.Uri.MakeRelativeUri(new Uri(x)).ToString().Replace(container.Name + "/", "")),
                new ParallelOptions {
                MaxDegreeOfParallelism = 100
            },
                blobName =>
            {
                var deleted = container.GetBlockBlobReference(blobName);

                try
                {
                    var stopwatch = Stopwatch.StartNew();

                    stopwatch.Start();
                    deleted.Delete();
                    stopwatch.Stop();

                    var logMessage = GetStats(deleted, stopwatch.ElapsedMilliseconds, "Deleted");
                    Console.WriteLine(logMessage);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Delete {0} Error {1}{2}{3}",
                                      blobName, ex.Message, Environment.NewLine, ex.StackTrace);
                }
            });
        }
Exemplo n.º 23
0
 private static IConfigurationRepository GetConfigurationRepository()
 {
     return(new AzureTableStorageConfigurationRepository(CloudConfigurationManager.GetSetting("ConfigurationStorageConnectionString")));
 }
        /// <summary>
        /// Initializes the database connection.
        /// </summary>
        /// <returns>Tracking task</returns>
        private async Task InitializeAsync()
        {
            this.telemetryClient.TrackTrace("Initializing data store");

            var endpointUrl                = CloudConfigurationManager.GetSetting("CosmosDBEndpointUrl");
            var primaryKey                 = CloudConfigurationManager.GetSetting("CosmosDBKey");
            var databaseName               = CloudConfigurationManager.GetSetting("CosmosDBDatabaseName");
            var teamsCollectionName        = CloudConfigurationManager.GetSetting("CosmosCollectionTeams");
            var usersCollectionName        = CloudConfigurationManager.GetSetting("CosmosCollectionUsers");
            var pairupUsersCollectionName  = CloudConfigurationManager.GetSetting("PairupUsersCollections");
            var feedbackInfoCollectionName = CloudConfigurationManager.GetSetting("FeedbackInfoCollections");
            var imageInfoCollectionName    = CloudConfigurationManager.GetSetting("ImageInfoCollections");

            this.documentClient = new DocumentClient(new Uri(endpointUrl), primaryKey);

            this.documentClient = new DocumentClient(new Uri(endpointUrl), primaryKey);
            var requestOptions = new RequestOptions {
                OfferThroughput = DefaultRequestThroughput
            };
            bool useSharedOffer = true;

            // Create the database if needed
            try
            {
                this.database = await this.documentClient.CreateDatabaseIfNotExistsAsync(new Database { Id = databaseName }, requestOptions);
            }
            catch (DocumentClientException ex)
            {
                if (ex.Error?.Message?.Contains("SharedOffer is Disabled") ?? false)
                {
                    this.telemetryClient.TrackTrace("Database shared offer is disabled for the account, will provision throughput at container level", SeverityLevel.Information);
                    useSharedOffer = false;

                    this.database = await this.documentClient.CreateDatabaseIfNotExistsAsync(new Database { Id = databaseName });
                }
                else
                {
                    throw;
                }
            }

            // Get a reference to the Teams collection, creating it if needed
            var teamsCollectionDefinition = new DocumentCollection
            {
                Id = teamsCollectionName,
            };

            teamsCollectionDefinition.PartitionKey.Paths.Add("/id");
            this.teamsCollection = await this.documentClient.CreateDocumentCollectionIfNotExistsAsync(this.database.SelfLink, teamsCollectionDefinition, useSharedOffer?null : requestOptions);

            // Get a reference to the Users collection, creating it if needed
            var usersCollectionDefinition = new DocumentCollection
            {
                Id = usersCollectionName
            };

            usersCollectionDefinition.PartitionKey.Paths.Add("/id");


            var pairUpUsersCollectionDefinition = new DocumentCollection
            {
                Id = pairupUsersCollectionName,
            };

            pairUpUsersCollectionDefinition.PartitionKey.Paths.Add("/id");

            this.usersCollection = await this.documentClient.CreateDocumentCollectionIfNotExistsAsync(this.database.SelfLink, usersCollectionDefinition, useSharedOffer?null : requestOptions);

            this.pairUpusersCollections = await this.documentClient.CreateDocumentCollectionIfNotExistsAsync(this.database.SelfLink, pairUpUsersCollectionDefinition, useSharedOffer?null : requestOptions);

            var feedbackInfoCollectionDefinition = new DocumentCollection
            {
                Id = feedbackInfoCollectionName,
            };

            feedbackInfoCollectionDefinition.PartitionKey.Paths.Add("/id");
            this.feedbackCollections = await this.documentClient.CreateDocumentCollectionIfNotExistsAsync(this.database.SelfLink, feedbackInfoCollectionDefinition, useSharedOffer?null : requestOptions);

            var imageInfoCollectionDefinition = new DocumentCollection
            {
                Id = imageInfoCollectionName,
            };

            imageInfoCollectionDefinition.PartitionKey.Paths.Add("/id");
            this.imageCollections = await this.documentClient.CreateDocumentCollectionIfNotExistsAsync(this.database.SelfLink, imageInfoCollectionDefinition, useSharedOffer?null : requestOptions);

            this.usersCollection = await this.documentClient.CreateDocumentCollectionIfNotExistsAsync(this.database.SelfLink, usersCollectionDefinition, useSharedOffer?null : requestOptions);

            this.telemetryClient.TrackTrace("Data store initialized");
        }
Exemplo n.º 25
0
        // upload a blob to storage, requires full path
        public static async Task UploadBlobToStorageAsync(string blobToUpload, string containerName)
        {
            // Retrieve storage account information from connection string
            CloudStorageAccount storageAccount = CreateStorageAccountFromConnectionString(CloudConfigurationManager.GetSetting("StorageConnectionString"));

            // Create a blob client for interacting with the blob service.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Create a container for organizing blobs within the storage account.
            CloudBlobContainer container = blobClient.GetContainerReference(containerName);

            try
            {
                await container.CreateIfNotExistsAsync();
            }
            catch (StorageException)
            {
                throw;
            }

            // allow public access to blobs in this container
            await container.SetPermissionsAsync(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });

            // Upload a BlockBlob to the newly created container, default mode: overwrite existing
            CloudBlockBlob blockBlob = container.GetBlockBlobReference(Path.GetFileName(blobToUpload));
            await blockBlob.UploadFromFileAsync(blobToUpload, FileMode.Open);
        }
Exemplo n.º 26
0
        private void GetAppSettings()
        {
            try
            {
                globalSettings.ForceSocketCloseOnUserActionsTimeout =
                    CloudConfigurationManager.GetSetting("ForceSocketCloseOnUserActionsTimeout") == "true";
            }
            catch (Exception)
            {
            }

            // Read settings for Devices Event Hub
            eventHubDevicesSettings.connectionString        = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionStringDevices");
            eventHubDevicesSettings.name                    = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.EventHubDevices").ToLowerInvariant();
            eventHubDevicesSettings.storageConnectionString = CloudConfigurationManager.GetSetting("Microsoft.Storage.ConnectionString");
            eventHubDevicesSettings.namespaceManager        = NamespaceManager.CreateFromConnectionString(CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString"));

            // Read settings for Alerts Event Hub
            eventHubAlertsSettings.connectionString        = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionStringAlerts");
            eventHubAlertsSettings.name                    = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.EventHubAlerts").ToLowerInvariant();
            eventHubAlertsSettings.storageConnectionString = CloudConfigurationManager.GetSetting("Microsoft.Storage.ConnectionString");
            eventHubAlertsSettings.namespaceManager        = NamespaceManager.CreateFromConnectionString(CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString"));

            if (String.IsNullOrEmpty(Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME")))
            {
                // Assume we are running local: use different consumer groups to avoid colliding with a cloud instance
                eventHubDevicesSettings.consumerGroup = "local";
                eventHubAlertsSettings.consumerGroup  = "local";
            }
            else
            {
                eventHubDevicesSettings.consumerGroup = "website";
                eventHubAlertsSettings.consumerGroup  = "website";
            }
        }
Exemplo n.º 27
0
 public string GetConnectionString()
 {
     return(CloudConfigurationManager.GetSetting("SQL.Database.ConnectionString"));
 }
Exemplo n.º 28
0
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Model init
            CloudStorageAccount storageAccount = CreateStorageAccountFromConnectionString(CloudConfigurationManager.GetSetting("StorageConnectionString"));

            BlobModel.Init(storageAccount);
            QueueModel.Init(storageAccount);
            ImageProcessJobModel.Init(storageAccount);
            InputImageModel.Init(storageAccount);

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
                );
        }
Exemplo n.º 29
0
        /// <summary>
        /// Processes a received notification. This typically is triggered via an Azure Web Job that reads the Azure storage queue
        /// </summary>
        /// <param name="notification">Notification to process</param>
        public void ProcessNotification(NotificationModel notification)
        {
            ClientContext cc = null;

            try
            {
                #region Setup an app-only client context
                var am = new AuthenticationManager();

                var url          = $"https://{CloudConfigurationManager.GetSetting("TenantName")}{notification.SiteUrl}";
                var realm        = TokenHelper.GetRealmFromTargetUrl(new Uri(url));
                var clientId     = CloudConfigurationManager.GetSetting("ClientId");
                var clientSecret = CloudConfigurationManager.GetSetting("ClientSecret");

                cc = new Uri(url).DnsSafeHost.Contains("spoppe.com")
                    ? am.GetAppOnlyAuthenticatedContext(url, realm, clientId, clientSecret,
                                                        acsHostUrl: "windows-ppe.net", globalEndPointPrefix: "login")
                    : am.GetAppOnlyAuthenticatedContext(url, clientId, clientSecret);

                cc.ExecutingWebRequest += Cc_ExecutingWebRequest;
                #endregion

                #region Grab the list for which the web hook was triggered
                var lists   = cc.Web.Lists;
                var listId  = new Guid(notification.Resource);
                var results = cc.LoadQuery(lists.Where(lst => lst.Id == listId));
                cc.ExecuteQueryRetry();
                var changeList = results.FirstOrDefault();
                if (changeList == null)
                {
                    // list has been deleted in between the event being fired and the event being processed
                    return;
                }
                #endregion

                #region Grab the list changes and do something with them
                // grab the changes to the provided list using the GetChanges method
                // on the list. Only request Item changes as that's what's supported via
                // the list web hooks
                var changeQuery = new ChangeQuery(false, true)
                {
                    Item         = true,
                    FetchLimit   = 1000, // Max value is 2000, default = 1000
                    DeleteObject = false,
                    Add          = true,
                    Update       = true,
                    SystemUpdate = false
                };

                // grab last change token from database if possible
                using (var dbContext = new SharePointWebHooks())
                {
                    ChangeToken lastChangeToken = null;
                    var         id = new Guid(notification.SubscriptionId);

                    var listWebHookRow = dbContext.ListWebHooks.Find(id);
                    if (listWebHookRow != null)
                    {
                        lastChangeToken = new ChangeToken
                        {
                            StringValue = listWebHookRow.LastChangeToken
                        };
                    }

                    // Start pulling down the changes
                    var allChangesRead = false;
                    do
                    {
                        // should not occur anymore now that we record the starting change token at
                        // subscription creation time, but it's a safety net
                        if (lastChangeToken == null)
                        {
                            lastChangeToken = new ChangeToken
                            {
                                StringValue =
                                    $"1;3;{notification.Resource};{DateTime.Now.AddMinutes(-5).ToUniversalTime().Ticks.ToString()};-1"
                            };
                            // See https://blogs.technet.microsoft.com/stefan_gossner/2009/12/04/content-deployment-the-complete-guide-part-7-change-token-basics/
                        }

                        // Assign the change token to the query...this determines from what point in
                        // time we'll receive changes
                        changeQuery.ChangeTokenStart = lastChangeToken;

                        // Execute the change query
                        var changes = changeList.GetChanges(changeQuery);
                        cc.Load(changes);
                        cc.ExecuteQueryRetry();

                        // If item is changed more than once
                        var uniqueChanges = changes.Cast <ChangeItem>().AsEnumerable().DistinctBy(change => change.ItemId).ToList();

                        if (uniqueChanges.Any())
                        {
                            foreach (var change in uniqueChanges)
                            {
                                lastChangeToken = change.ChangeToken;

                                try
                                {
                                    // do "work" with the found change
                                    DoWork(cc, changeList, change);
                                }
                                catch (Exception)
                                {
                                    // ignored
                                }
                            }

                            // We potentially can have a lot of changes so be prepared to repeat the
                            // change query in batches of 'FetchLimit' until we've received all changes
                            if (changes.Count < changeQuery.FetchLimit)
                            {
                                allChangesRead = true;
                            }
                        }
                        else
                        {
                            allChangesRead = true;
                        }
                        // Are we done?
                    } while (allChangesRead == false);

                    // Persist the last used change token as we'll start from that one
                    // when the next event hits our service
                    if (listWebHookRow != null)
                    {
                        // Only persist when there's a change in the change token
                        if (!listWebHookRow.LastChangeToken.Equals(lastChangeToken.StringValue, StringComparison.InvariantCultureIgnoreCase))
                        {
                            listWebHookRow.LastChangeToken = lastChangeToken.StringValue;
                            dbContext.SaveChanges();
                        }
                    }
                    else
                    {
                        // should not occur anymore now that we record the starting change token at
                        // subscription creation time, but it's a safety net
                        dbContext.ListWebHooks.Add(new ListWebHooks()
                        {
                            Id              = id,
                            ListId          = listId,
                            LastChangeToken = lastChangeToken.StringValue,
                        });
                        dbContext.SaveChanges();
                    }
                }
                #endregion

                #region "Update" the web hook expiration date when needed
                // Optionally add logic to "update" the expiration date time of the web hook
                // If the web hook is about to expire within the coming 5 days then prolong it
                if (notification.ExpirationDateTime.AddDays(-5) >= DateTime.Now)
                {
                    return;
                }
                var webHookManager = new WebHookManager();
                var updateResult   = Task.WhenAny(
                    webHookManager.UpdateListWebHookAsync(
                        url,
                        listId.ToString(),
                        notification.SubscriptionId,
                        CloudConfigurationManager.GetSetting("WebHookEndPoint"),
                        DateTime.Now.AddMonths(3),
                        _accessToken)
                    ).Result;

                if (updateResult.Result == false)
                {
                    throw new Exception(
                              $"The expiration date of web hook {notification.SubscriptionId} with endpoint {CloudConfigurationManager.GetSetting("WebHookEndPoint")} could not be updated");
                }
                #endregion
            }
            catch (Exception ex)
            {
                // Log error
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                // ReSharper disable once ConstantConditionalAccessQualifier
                cc?.Dispose();
            }
        }
        public async Task <ActionResult> Create(string selectedSharePointList)
        {
            using (var cc = ContextProvider.GetWebApplicationClientContext(Settings.SiteCollection))
            {
                if (cc != null)
                {
                    // Hookup event to capture access token
                    cc.ExecutingWebRequest += Cc_ExecutingWebRequest;

                    ListCollection     lists           = cc.Web.Lists;
                    Guid               listId          = new Guid(selectedSharePointList);
                    IEnumerable <List> sharePointLists = cc.LoadQuery <List>(lists.Where(lst => lst.Id == listId));
                    cc.Load(cc.Web, w => w.Url);
                    cc.ExecuteQueryRetry();

                    WebHookManager webHookManager = new WebHookManager();
                    var            res            = await webHookManager.AddListWebHookAsync(cc.Web.Url, listId.ToString(), CloudConfigurationManager.GetSetting("WebHookEndPoint"), this.accessToken);

                    // persist the latest changetoken of the list when we create a new webhook. This allows use to only grab the changes as of web hook creation when the first notification comes in
                    using (SharePointWebHooks dbContext = new SharePointWebHooks())
                    {
                        dbContext.ListWebHooks.Add(new ListWebHooks()
                        {
                            Id              = new Guid(res.Id),
                            ListId          = listId,
                            LastChangeToken = sharePointLists.FirstOrDefault().CurrentChangeToken.StringValue,
                        });
                        var saveResult = await dbContext.SaveChangesAsync();
                    }
                }
            }

            return(RedirectToAction("Index", "Home"));
        }