public TableServiceContext(CloudTableClient client)
            : base(client.BaseUri, DataServiceProtocolVersion.V3)
        {
            CommonUtility.AssertNotNull("client", client);

            if (client.BaseUri == null)
            {
                throw new ArgumentNullException("client");
            }

            if (!client.BaseUri.IsAbsoluteUri)
            {
                string errorMessage = string.Format(CultureInfo.CurrentCulture, SR.RelativeAddressNotPermitted, client.BaseUri.ToString());

                throw new ArgumentException(errorMessage, "client");
            }

            this.SendingRequest += this.TableServiceContext_SendingRequest;

            this.IgnoreMissingProperties = true;
            this.MergeOption = MergeOption.PreserveChanges;
            this.ServiceClient = client;

            // Since the default is JSON light, this is valid. If users change it to Atom or NoMetadata, this gets updated.
            if (this.payloadFormat == TablePayloadFormat.Json)
            {
                this.Format.UseJson(new TableStorageModel(client.AccountName));
            }
        }
        internal static Task<WebResponse> GetResponseAsyncWithTimeout(this WebRequest req, CloudTableClient service, TimeSpan? timeout)
        {
            Task<WebResponse> serverTask = req.GetResponseAsync(service);

            Task<WebResponse> wrappedTask = TimeoutHelper.GetTimeoutWrappedTask(timeout, serverTask);
            return wrappedTask;
        }
示例#3
0
		public AddressBookContext(CloudTableClient client, string primaryTableName, string emailAddressTableName)
			: base(client) {
			Requires.NotNullOrEmpty(primaryTableName, "primaryTableName");
			Requires.NotNullOrEmpty(emailAddressTableName, "emailAddressTableName");

			this.PrimaryTableName = primaryTableName;
			this.EmailAddressTableName = emailAddressTableName;
		}
示例#4
0
 public Crawler(string url)
 {
     errorList = new List<string>();
     myurl = url;
     robot = new getRobot(url);
     CloudStorageAccount storageAccount;storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]);
     tableClient = storageAccount.CreateCloudTableClient();
     visitedUrl = new HashSet<string>();
     siteMap = new getSiteMap(robot.GetSiteMap());
     disAllow = robot.GetDisAllowed();
     myStorage = new Storage();
 }
示例#5
0
		public void SetUp() {
			AzureStorageConfig.RegisterConfiguration();

			var testContainerName = "unittests" + Guid.NewGuid().ToString();
			var testTableName = "unittests" + Guid.NewGuid().ToString().Replace("-", string.Empty);
			var account = CloudStorageAccount.DevelopmentStorageAccount;
			var client = account.CreateCloudBlobClient();
			this.tableClient = account.CreateCloudTableClient();
			this.tableClient.GetTableReference(testTableName).CreateIfNotExists();
			this.container = client.GetContainerReference(testContainerName);
			this.container.CreateContainerWithPublicBlobsIfNotExistAsync();
			this.controller = new InboxControllerForTest(this.container.Name, testTableName, CloudConfigurationName);
		}
        /// <summary>
        /// Initializes a new instance of the <see cref="TableServiceContext"/> class.
        /// </summary>
        public TableServiceContext(CloudTableClient client)
            : base(client.BaseUri)
        {
            if (client.BaseUri == null)
            {
                throw new ArgumentNullException("client.BaseUri");
            }

            if (!client.BaseUri.IsAbsoluteUri)
            {
                string errorMessage = string.Format(CultureInfo.CurrentCulture, SR.RelativeAddressNotPermitted, client.BaseUri.ToString());

                throw new ArgumentException(errorMessage, "client.BaseUri");
            }

            this.SendingRequest += this.TableServiceContext_SendingRequest;

            this.IgnoreMissingProperties = true;
            this.MergeOption = MergeOption.PreserveChanges;
            this.ServiceClient = client;
        }
        public TableStorageRepository(ICloudStorageAccountFacade accountParameter)
        {
            ICloudStorageAccountFacade account = accountParameter ?? throw new ArgumentNullException(nameof(account));

            client = account.CreateCloudTableClient();
        }
        /// <summary>
        /// Test upsert (insert or replace) on entities inside and outside the given range.
        /// </summary>
        /// <param name="testClient">The table client to test.</param>
        /// <param name="tableName">The name of the table to test.</param>
        /// <param name="accessPermissions">The access permissions of the table client.</param>
        /// <param name="startPk">The start partition key range.</param>
        /// <param name="startRk">The start row key range.</param>
        /// <param name="endPk">The end partition key range.</param>
        /// <param name="endRk">The end row key range.</param>
        private void TestUpsertReplace(
            CloudTableClient testClient,
            string tableName,
            SharedAccessTablePermissions accessPermissions,
            string startPk,
            string startRk,
            string endPk,
            string endRk)
        {
            Action<BaseEntity> upsertDelegate = (tableEntity) =>
            {
                TableServiceContext context = testClient.GetTableServiceContext();

                // Replace entity
                tableEntity.A = "10";
                context.AttachTo(tableName, tableEntity);
                context.UpdateObject(tableEntity);
                context.SaveChangesWithRetries(SaveChangesOptions.ReplaceOnUpdate);
            };

            SharedAccessTablePermissions upsertPermissions = (SharedAccessTablePermissions.Update | SharedAccessTablePermissions.Add);
            bool expectSuccess = (accessPermissions & upsertPermissions) == upsertPermissions;

            // Perform test
            TestOperationWithRange(
                tableName,
                startPk,
                startRk,
                endPk,
                endRk,
                upsertDelegate,
                "upsert replace",
                expectSuccess,
                expectSuccess ? HttpStatusCode.NoContent : HttpStatusCode.NotFound);
        }
示例#9
0
 private static CloudTable GetUsersTable(CloudTableClient tableClient)
 {
     return(tableClient.GetTableReference("Users"));
 }
示例#10
0
 public void CreateTable()
 {
     tableClient = storageAccount.CreateCloudTableClient();
     chatTable   = tableClient.GetTableReference(TABLENAME);
     chatTable.CreateIfNotExists();
 }
        public async Task TableFilterTest()
        {
            // Reinitialize the name resolver to avoid conflicts
            _resolver = new RandomNameResolver();

            IHost host = new HostBuilder()
                         .ConfigureDefaultTestHost <AzureStorageEndToEndTests>(b =>
            {
                b.AddAzureStorage();
            })
                         .ConfigureServices(services =>
            {
                services.AddSingleton <INameResolver>(_resolver);
            })
                         .Build();

            // write test entities
            string           testTableName = _resolver.ResolveInString(TableName);
            CloudTableClient tableClient   = _storageAccount.CreateCloudTableClient();
            CloudTable       table         = tableClient.GetTableReference(testTableName);
            await table.CreateIfNotExistsAsync();

            var operation = new TableBatchOperation();

            operation.Insert(new Person
            {
                PartitionKey = "1",
                RowKey       = "1",
                Name         = "Lary",
                Age          = 20,
                Location     = "Seattle"
            });
            operation.Insert(new Person
            {
                PartitionKey = "1",
                RowKey       = "2",
                Name         = "Moe",
                Age          = 35,
                Location     = "Seattle"
            });
            operation.Insert(new Person
            {
                PartitionKey = "1",
                RowKey       = "3",
                Name         = "Curly",
                Age          = 45,
                Location     = "Texas"
            });
            operation.Insert(new Person
            {
                PartitionKey = "1",
                RowKey       = "4",
                Name         = "Bill",
                Age          = 28,
                Location     = "Tam O'Shanter"
            });

            await table.ExecuteBatchAsync(operation);

            JobHost jobHost    = host.GetJobHost();
            var     methodInfo = GetType().GetMethod(nameof(TableWithFilter));
            var     input      = new Person {
                Age = 25, Location = "Seattle"
            };
            string json      = JsonConvert.SerializeObject(input);
            var    arguments = new { person = json };
            await jobHost.CallAsync(methodInfo, arguments);

            // wait for test results to appear
            await TestHelpers.Await(() => testResult != null);

            JArray results = (JArray)testResult;

            Assert.Single(results);

            input = new Person {
                Age = 25, Location = "Tam O'Shanter"
            };
            json      = JsonConvert.SerializeObject(input);
            arguments = new { person = json };
            await jobHost.CallAsync(methodInfo, arguments);

            await TestHelpers.Await(() => testResult != null);

            results = (JArray)testResult;
            Assert.Single(results);
            Assert.Equal("Bill", (string)results[0]["Name"]);
        }
        public FatPipeDAL(string storageConnectionString, FatpipeManager fpm)
        {
            try
            {
                LoggerFactory.Logger.Debug("FatPipeDAL C'tor", "Initializing Azure accounts");
                this.fpm = fpm;

                // The constructor below is hardcoded for now.. it will use the method params
                if (!string.IsNullOrEmpty(storageConnectionString))
                {
                    this.cloudAcct = CloudStorageAccount.Parse(storageConnectionString);
                }

                else //fallback path to Cloud storage
                {
                    cloudAcct = new CloudStorageAccount(new StorageCredentialsAccountAndKey("maargsoft", "njPcqdHZuYUNbp32GS1tpSAeoUSp1hZ1EJsqRdtnTJe5BZtEmVd61UHonvva6o3WZ1COeAPtTm4ofbMqFaqj7Q=="), false);
                }

                queueClient      = cloudAcct.CreateCloudQueueClient();
                blobClient       = cloudAcct.CreateCloudBlobClient();
                this.tableClient = cloudAcct.CreateCloudTableClient();

                bool runtimeEnv = !string.IsNullOrEmpty(MaargConfiguration.Instance[MaargConfiguration.FatpipeManagerIncomingQueueName]);
                if (runtimeEnv)
                {
                    incomingQ = queueClient.GetQueueReference(MaargConfiguration.Instance[MaargConfiguration.FatpipeManagerIncomingQueueName]);
                    outgoingQ = queueClient.GetQueueReference(MaargConfiguration.Instance[MaargConfiguration.FatpipeManagerOutgoingQueueName]);

                    // Create the queue if it doesn't already exist
                    incomingQ.CreateIfNotExist();
                    outgoingQ.CreateIfNotExist();

                    DateTime today  = DateTime.Today;
                    string   bucket = string.Format(BlobContainerPartitionNameFormat, today.Month, today.Year,
                                                    MaargConfiguration.Instance[MaargConfiguration.FatpipeManagerIncomingBlobContainerNameSuffix]);

                    string outbucket = string.Format(BlobContainerPartitionNameFormat, today.Month, today.Year,
                                                     MaargConfiguration.Instance[MaargConfiguration.FatpipeManagerOutgoingBlobContainerNameSuffix]);

                    // Retrieve a reference to a container
                    inContainer  = blobClient.GetContainerReference(bucket);
                    outContainer = blobClient.GetContainerReference(outbucket);

                    // Create the container if it doesn't already exist
                    inContainer.CreateIfNotExist();
                    outContainer.CreateIfNotExist();
                }

                schemaContainer = blobClient.GetContainerReference(SchemaContainerName);
                schemaContainer.CreateIfNotExist();

                notificationTemplateContainer = blobClient.GetContainerReference(NotificationTemplateContainerName);
                notificationTemplateContainer.CreateIfNotExist();

                //suspended store
                if (runtimeEnv)
                {
                    this.suspendedContainer = this.blobClient.GetContainerReference(MaargConfiguration.Instance[MaargConfiguration.SuspendedMessageBlobContainerName]);
                    this.suspendedContainer.CreateIfNotExist();
                    this.tableClient.CreateTableIfNotExist(MaargConfiguration.Instance[MaargConfiguration.SuspendedMessageTableName]);
                    this.suspendedTableServiceContext = this.tableClient.GetDataServiceContext();

                    int visibilityTimeoutInSeconds;
                    if (!int.TryParse(MaargConfiguration.Instance[MaargConfiguration.FatpipeManagerQueueMessageVisibilityTimeoutInSeconds], out visibilityTimeoutInSeconds))
                    {
                        visibilityTimeoutInSeconds = 30;
                        LoggerFactory.Logger.Warning("FatPipeDAL C'tor", EventId.BizpipeMissingConfigValue
                                                     , "Configuration for {0} is not defined. Using default value of {1} seconds."
                                                     , MaargConfiguration.FatpipeManagerQueueMessageVisibilityTimeoutInSeconds
                                                     , visibilityTimeoutInSeconds);
                    }
                    this.visibilityTimeout = new TimeSpan(0, 0, visibilityTimeoutInSeconds);

                    // does it need public access?
                    this.inContainer.SetPermissions(new BlobContainerPermissions {
                        PublicAccess = BlobContainerPublicAccessType.Blob
                    });
                    this.outContainer.SetPermissions(new BlobContainerPermissions {
                        PublicAccess = BlobContainerPublicAccessType.Blob
                    });
                    this.suspendedContainer.SetPermissions(new BlobContainerPermissions {
                        PublicAccess = BlobContainerPublicAccessType.Blob
                    });
                }
                LoggerFactory.Logger.Debug("FatPipeDAL C'tor", "Connections established to Azure accounts");
            }
            catch (Exception excp)
            {
                LoggerFactory.Logger.Error("FatPipeDAL C'tor", EventId.BizpipeCtor
                                           , "FatPipeDAL constructor encountered exception: {0} ", excp.ToString());
            }
        }
示例#13
0
        public void initStorageConnection(string storageConnectionString)
        {
            var storageAccount = CloudStorageAccount.Parse(storageConnectionString);

            _client = storageAccount.CreateCloudTableClient();
        }
示例#14
0
 public AzureTableStorageCacheHandler(string tableName, string partitionKey, CloudStorageAccount account) : this(tableName, partitionKey)
 {
     client = account.CreateCloudTableClient();
     Connect();
 }
示例#15
0
 public TableStorageProvider(CloudTableClient cloudTableClient)
 {
     table = cloudTableClient.GetTableReference("testCustomerStorage");
 }
示例#16
0
        public static void Main(string[] args)
        {
            Process currProc = Process.GetCurrentProcess();

            containerName = "testcontainer"; //Guid.NewGuid().ToString("N").ToLower();

            if (args.Length == 0)
            {
                args = new string[7];

                // the number of test iterations
                args[0] = "1";

                // the number of blobs
                args[1] = "10";

                // the number of concurrent test workers.
                args[2] = "2";

                // the blob size in KB
                args[3] = "1024";

                // the number of parallel requests per blob
                args[4] = "1";

                // use https or not
                args[5] = "false";

                // the result folder name
                args[6] = "folder1";
            }

            iterations              = Int32.Parse(args[0]);
            blobs                   = Int32.Parse(args[1]);
            concurrency             = Int32.Parse(args[2]);
            blobSizeInKB            = Int32.Parse(args[3]);
            parallelRequestsPerBlob = Int32.Parse(args[4]);
            useHttps                = bool.Parse(args[5]);
            resultFileFolderName    = args[6];

            if (!Directory.Exists(resultFileFolderName))
            {
                Directory.CreateDirectory(resultFileFolderName);
            }

            resultFile = string.Format(@"{6}\{0}_{1}_{2}_{3}_{4}_{5}.csv", blobSizeInKB, parallelRequestsPerBlob, concurrency, useHttps, iterations, blobs, resultFileFolderName);

            ThreadPool.SetMinThreads(concurrency * parallelRequestsPerBlob, concurrency * parallelRequestsPerBlob);

            accounts = Account.GetStorageAccounts(useHttps);
            ClientRegistry.Init(accounts, accounts[configAccountName]);

            configuration = new ReplicaConfiguration(containerName);
            ClientRegistry.AddConfiguration(configuration);

            if (firstClient)
            {
                // delete configuration blob and tables
                // ReplicaConfiguration.DeleteConfiguration(containerName);
                ConfigurationCloudStore backingStore = new ConfigurationCloudStore(accounts[configAccountName], configuration);
                backingStore.DeleteConfiguration();

                CloudTableClient ConfigurationCloudTableClient = accounts[configAccountName].CreateCloudTableClient();
                CloudTable       slaTable = ConfigurationCloudTableClient.GetTableReference(ConstPool.SLA_CONFIGURATION_TABLE_NAME);
                slaTable.DeleteIfExists();
                slaTable = ConfigurationCloudTableClient.GetTableReference(ConstPool.SESSION_STATE_CONFIGURATION_TABLE_NAME);
                slaTable.DeleteIfExists();

                Console.WriteLine("removed everything, wait 40 seconds ...");
                Thread.Sleep(40000);

                // recreate configuration
                configuration.PrimaryServers.Add("dbtsouthstorage");
                configuration.SyncWithCloud(accounts[configAccountName], false);
                Console.WriteLine("recreated configuration, wait 10 seconds ...");
                Thread.Sleep(10000);
            }
            else
            {
                // retrieve configuration from cloud
                configuration.SyncWithCloud(accounts[configAccountName]);
            }

            if (firstClient)
            {
                slaEngine = new ConsistencySLAEngine(CreateShoppingCartSla1(), configuration);
            }
            else
            {
                slaEngine = new ConsistencySLAEngine(CreateShoppingCartSla2(), configuration);
            }

            blobClient    = accounts["dbtsouthstorage"].CreateCloudBlobClient();
            blobContainer = blobClient.GetContainerReference(containerName);
            blobContainer.CreateIfNotExists();
            capContainer = new CapCloudBlobContainer(blobContainer);

            // Generate random data
            BlobDataBuffer = new byte[1024 * blobSizeInKB];
            Random random = new Random();

            random.NextBytes(BlobDataBuffer);

            //ServiceLevelAgreement sla = CreateShoppingCartSla();

            Stopwatch totalWatch = new Stopwatch();

            totalWatch.Start();
            for (int m = 0; m < concurrency; m++)
            {
                ThreadPool.QueueUserWorkItem((o) =>
                {
                    Interlocked.Increment(ref concurrentWorkers);
                    for (int i = 0; i < iterations; i++)
                    {
                        Console.WriteLine("Running thread " + m + "." + i);
                        Console.WriteLine("concurrent workers: " + concurrentWorkers);
                        try
                        {
                            // do upload blob test.
                            var blobsList = UploadBlob();
                            Console.WriteLine("Upload Finished ...\n");

                            // GET and DELETE.
                            DoGetAndDelete(blobsList);
                            Console.WriteLine("DoGetAndDelete Finished ...\n");

                            configure("client", containerName);
                            Console.WriteLine("Configure Finished ...\n");
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.ToString());
                        }
                    }


                    Interlocked.Decrement(ref concurrentWorkers);
                });
            }
            Console.WriteLine("Program: Started to sleep");
            Thread.Sleep(5000);
            while (Interlocked.CompareExchange(ref concurrentWorkers, -1, 0) != -1)
            {
                if (concurrentWorkers < 0)
                {
                    break;
                }
                Console.WriteLine("Waiting for a thread because there are " + concurrentWorkers + " threads.");
                Thread.Sleep(5000);
            }

            Console.WriteLine("Finished execution. ");
            ClientRegistry.GetConfigurationContainer(containerName).Delete();
            blobContainer.DeleteIfExists();

            totalWatch.Stop();
            long totalTimeTaken = totalWatch.ElapsedMilliseconds;

            using (StreamWriter sw = new StreamWriter(resultFile))
            {
                sw.Write(String.Format("Total time taken to run the test in ms : {0}\n\n", totalTimeTaken));
                sw.Write(String.Format("Args:Concurrency: {0} BlobSizeInKB:{1} ParallelRequestsPerBlob:{2} UsingHttps:{3} \n", concurrency, blobSizeInKB, parallelRequestsPerBlob, useHttps));

                // display result
                DisplayResults(sw, "Insert", insertTimes);
                DisplayResults(sw, "Get", getTimes);
                DisplayResults(sw, "Delete", deleteTimes);

                float tmp = 0;
                foreach (SubSLA s in capContainer.SLA)
                {
                    tmp += s.Utility * s.NumberOfHits;
                }

                sw.Write(String.Format("Current utility ", tmp));


                // Display perf results
                PerfMetrics metrics = new PerfMetrics();
                metrics.Update(currProc);
                metrics.Print(sw);
            }

            Console.Read();
        }
示例#17
0
        private static void Main(string[] args)
        {
            string configPath = "config.json";

            if (!File.Exists(configPath))
            {
                Console.WriteLine("================================");
                Console.WriteLine("There is no config. Please select the transport you would like:");
                Console.WriteLine("[R]uffles");
                Console.WriteLine("[U]net");

                ConsoleKey key = ConsoleKey.Escape;

                do
                {
                    key = Console.ReadKey(true).Key;
                }while (key != ConsoleKey.R && key != ConsoleKey.U);

                if (key == ConsoleKey.U)
                {
                    Transport = new UnetTransport();

                    Console.WriteLine("================================");
                    Console.WriteLine("Please select a template for the UNET config file:");
                    Console.WriteLine("[M]LAPI - Copies the same UNET settings as the MLAPI uses by default");
                    Console.WriteLine("[H]LAPI - Copies the same UNET settings as the HLAPI uses by default");
                    Console.WriteLine("[E]mpty - Default UNET settings");

                    key = ConsoleKey.Escape;

                    do
                    {
                        key = Console.ReadKey(true).Key;
                    }while (key != ConsoleKey.M && key != ConsoleKey.H && key != ConsoleKey.E);

                    if (key == ConsoleKey.M)
                    {
                        UnetTransport.UnetConfig config = (UnetTransport.UnetConfig)Transport.GetConfig();

                        config.ConnectionConfig.AddChannel(QosType.ReliableFragmentedSequenced);
                        config.ConnectionConfig.AddChannel(QosType.ReliableSequenced);
                        config.ConnectionConfig.AddChannel(QosType.UnreliableSequenced);
                        config.ConnectionConfig.AddChannel(QosType.Unreliable);
                        config.ConnectionConfig.AddChannel(QosType.Unreliable);
                        config.ConnectionConfig.AddChannel(QosType.Reliable);
                        config.ConnectionConfig.AddChannel(QosType.UnreliableSequenced);
                        config.ConnectionConfig.AddChannel(QosType.ReliableSequenced);
                        config.ConnectionConfig.AddChannel(QosType.ReliableSequenced);
                        config.ConnectionConfig.AddChannel(QosType.UnreliableSequenced);

                        Config = new RelayConfig()
                        {
                            Transport                   = TransportType.UNET,
                            AllowTemporaryAlloc         = true,
                            MaxTemporaryAlloc           = 1024 * 16,
                            BandwidthGracePrediodLength = 60 * 2,
                            BandwidthLimit              = -1,
                            EnableRuntimeMetaLogging    = true,
                            GracePeriodBandwidthLimit   = -1,
                            TransportConfig             = config,
                            BufferSize                  = 1024 * 8,
                            ListenPort                  = 8888
                        };
                    }
                    else if (key == ConsoleKey.H)
                    {
                        UnetTransport.UnetConfig config = (UnetTransport.UnetConfig)Transport.GetConfig();

                        config.ConnectionConfig.AddChannel(QosType.ReliableSequenced);
                        config.ConnectionConfig.AddChannel(QosType.Unreliable);

                        Config = new RelayConfig()
                        {
                            Transport                   = TransportType.UNET,
                            BufferSize                  = 1024 * 8,
                            AllowTemporaryAlloc         = true,
                            BandwidthGracePrediodLength = 60 * 2,
                            BandwidthLimit              = -1,
                            EnableRuntimeMetaLogging    = true,
                            GracePeriodBandwidthLimit   = -1,
                            ListenPort                  = 8888,
                            MaxTemporaryAlloc           = 1024 * 16,
                            TransportConfig             = config
                        };
                    }
                    else if (key == ConsoleKey.E)
                    {
                        UnetTransport.UnetConfig config = (UnetTransport.UnetConfig)Transport.GetConfig();

                        Config = new RelayConfig()
                        {
                            Transport                   = TransportType.UNET,
                            BufferSize                  = 1024 * 8,
                            AllowTemporaryAlloc         = true,
                            BandwidthGracePrediodLength = 60 * 2,
                            BandwidthLimit              = -1,
                            EnableRuntimeMetaLogging    = true,
                            GracePeriodBandwidthLimit   = -1,
                            ListenPort                  = 8888,
                            MaxTemporaryAlloc           = 1024 * 16,
                            TransportConfig             = config
                        };
                    }
                }
                else if (key == ConsoleKey.R)
                {
                    Transport = new RufflesTransport();

                    RufflesTransport.RufflesConfig config = (RufflesTransport.RufflesConfig)Transport.GetConfig();

                    Config = new RelayConfig()
                    {
                        Transport                   = TransportType.Ruffles,
                        AllowTemporaryAlloc         = true,
                        BandwidthGracePrediodLength = 60 * 2,
                        BandwidthLimit              = -1,
                        BufferSize                  = 1024 * 8,
                        EnableRuntimeMetaLogging    = true,
                        GracePeriodBandwidthLimit   = -1,
                        ListenPort                  = 8888,
                        MaxTemporaryAlloc           = 1024 * 16,
                        TransportConfig             = config
                    };
                }

                if (Config != null)
                {
                    object config = Transport.BeforeSerializeConfig(Config);

                    string serializedJson = JsonConvert.SerializeObject(config, Formatting.Indented);

                    File.WriteAllText(configPath, Transport.ProcessSerializedJson(serializedJson));
                }
                else
                {
                    // TODO: Something went wrong. No config??
                }
            }
            else
            {
                try
                {
                    Config = JsonConvert.DeserializeObject <RelayConfig>(File.ReadAllText(configPath));

                    switch (Config.Transport)
                    {
                    case TransportType.Ruffles:
                        Transport = new RufflesTransport();
                        break;

                    case TransportType.UNET:
                        Transport = new UnetTransport();
                        break;
                    }

                    // Post deserialization job
                    Config = (RelayConfig)Transport.AfterDeserializedConfig(Config);
                }
                catch (Exception e)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("[ERROR] Error parsing config file: " + e);
                    Console.Read();
                    Environment.Exit(1);
                    return;
                }
            }

            Program.MESSAGE_BUFFER = new byte[Config.BufferSize];

            if (string.IsNullOrWhiteSpace(Config.IpAddress))
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("[ERROR] Set the IpAddress of the relay in the config file.");
                Environment.Exit(1);
            }
            if (string.IsNullOrWhiteSpace(Config.AzureTableConnectionString))
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("[ERROR] Set the connection string of the rooms Azure table in the config file.");
                Environment.Exit(1);
            }

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(Config.AzureTableConnectionString);
            CloudTableClient    tableClient    = storageAccount.CreateCloudTableClient();

            roomsTable = tableClient.GetTableReference("rooms");

            try
            {
                Console.WriteLine("[INFO] Starting server...");
                Transport.Start(Config.TransportConfig);
                Console.WriteLine("[INFO] Server started!");
            }
            catch (DllNotFoundException e)
            {
                Console.WriteLine("[FATAL] Could not locate one or more shared libraries! Message: \n" + e);
            }
            catch (Exception e)
            {
                Console.WriteLine("[FATAL] An unexpected error occurred! Message: \n" + e);
            }

            Stopwatch watch = new Stopwatch();

            while (true)
            {
                try
                {
                    watch.Restart();

                    RunLoop();

                    int timeBetweenTicks = (int)((1f / Config.TicksPerSecond) * 1000f);
                    int timeToSleep      = timeBetweenTicks - (int)watch.ElapsedMilliseconds;

                    if (timeToSleep > 0)
                    {
                        Thread.Sleep(timeToSleep);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("[ERROR] Exception during loop: " + e);
                }
            }
        }
示例#18
0
        public ActionResult GetPartition(string editPartitionKey, string editRowKey)
        {
            if (editPartitionKey != null)
            {
                ViewBag.EditPartitionKey = editPartitionKey;
                ViewBag.EditRowKey       = editRowKey;
                ViewBag.EditMode         = true;
            }
            else
            {
                ViewBag.EditMode = false;
            }

            // The code in this section goes here.
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                CloudConfigurationManager.GetSetting("irpistorageaccount_AzureStorageConnectionString"));
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

            // Collect user devices
            CloudTable devicesTable        = tableClient.GetTableReference("UserDevicesTable");
            TableQuery <TableEntity> query =
                new TableQuery <TableEntity>()
                .Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, User.Identity.GetUserId()));     // change

            List <string>          deviceNames = new List <string>();
            TableContinuationToken token       = null;

            do
            {
                TableQuerySegment <TableEntity> resultSegment = devicesTable.ExecuteQuerySegmented(query, token);
                token = resultSegment.ContinuationToken;

                foreach (TableEntity entity in resultSegment.Results)
                {
                    deviceNames.Add(entity.RowKey);
                }
            } while (token != null);

            List <IREntity> customers = new List <IREntity>();

            // Collect IR recordings for each device
            foreach (string deviceName in deviceNames)
            {
                CloudTable            table   = tableClient.GetTableReference("IRRecordingTable");
                TableQuery <IREntity> irQuery =
                    new TableQuery <IREntity>()
                    .Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, deviceName));     // change

                token = null;
                do
                {
                    TableQuerySegment <IREntity> resultSegment = table.ExecuteQuerySegmented(irQuery, token);
                    token = resultSegment.ContinuationToken;

                    foreach (IREntity customer in resultSegment.Results)
                    {
                        customers.Add(customer);
                    }
                } while (token != null);
            }

            return(View(customers));
        }
示例#19
0
        public WfResult Run(CancellationToken token)
        {
            WfResult result = WfResult.Unknown;

            //_logger.Write(String.Format("SqlServer: {0} query: {1}", _attributes[CONNECTION_STRING], _attributes[QUERY_STRING]));

            try
            {
                CloudStorageAccount account;

                if (Boolean.Parse(_attributes[IS_SAS_TOKEN]))
                {
                    StorageCredentials credentials = new StorageCredentials(_attributes[ACCOUNT_KEY]);
                    account = new CloudStorageAccount(credentials, _attributes[ACCOUNT_NAME], endpointSuffix: null, useHttps: true);
                }
                else
                {
                    StorageCredentials credentials = new StorageCredentials(_attributes[ACCOUNT_NAME], _attributes[ACCOUNT_KEY]);
                    account = new CloudStorageAccount(credentials, useHttps: true);
                }

                CloudTableClient tblClient = account.CreateCloudTableClient();

                CloudTable table = tblClient.GetTableReference(_attributes[AZURE_TABLE_NAME]);
                //PrintTableProperties(table);

                //get sql table schema to construct azure table query request
                DataTable     schema     = GetSchema(_attributes[SQL_TABLE_NAME]);
                List <string> columnList = schema.Columns.OfType <DataColumn>()
                                           .Select(c => c.ColumnName)
                                           //.Union(new string[]{ AZURE_TABLE_PARTITION_KEY_COL,AZURE_TABLE_ROW_KEY_COL,AZURE_TABLE_TIMESTAMP_COL})
                                           .ToList();

                //TableQuery<DynamicTableEntity> tableQuery = new TableQuery<DynamicTableEntity>().Select(columnList);
                TableQuery tableQuery = new TableQuery().Select(columnList);
                if (!String.IsNullOrEmpty(_attributes[CONTROL_COLUMN]))
                {
                    if (String.IsNullOrEmpty(_attributes[CONTROL_VALUE]))
                    {
                        throw new ArgumentException(String.Format("ControlValue is expected for {0}", _attributes[CONTROL_COLUMN]));
                    }

                    tableQuery = tableQuery.Where(this.GenerateFilterCondition(schema, _attributes[CONTROL_COLUMN], _attributes[CONTROL_VALUE]));
                }

                // Initialize the continuation token to null to start from the beginning of the table.
                TableContinuationToken continuationToken = null;
                TableRequestOptions    requestOptions    = GetRequestOptions(schema);
                OperationContext       ctx = new OperationContext();

                EntityResolver <DataRow> resolver = (pk, rk, ts, props, etag) =>
                {
                    DataRow dataRow = schema.NewRow();
                    dataRow[AZURE_TABLE_PARTITION_KEY_COL] = pk;
                    dataRow[AZURE_TABLE_ROW_KEY_COL]       = rk;
                    dataRow[AZURE_TABLE_TIMESTAMP_COL]     = ts;
                    foreach (var prop in props)
                    {
                        SetValue(schema, dataRow, prop);
                    }
                    schema.Rows.Add(dataRow);
                    return(dataRow);
                };

                do
                {
                    // Retrieve a segment (up to 1,000 entities).
                    Task <TableQuerySegment <DataRow> > task =
                        table.ExecuteQuerySegmentedAsync <DataRow>(tableQuery, resolver, continuationToken, requestOptions, ctx, token);

                    var tableQueryResult = task.Result;
                    token.ThrowIfCancellationRequested();

                    // Assign the new continuation token to tell the service where to
                    // continue on the next iteration (or null if it has reached the end).
                    continuationToken = tableQueryResult.ContinuationToken;
                    if (schema.Rows.Count > 9000 || continuationToken == null)
                    {
                        _logger.Information("Copy {Count} rows", schema.Rows.Count);
                        BulkLoad(schema);
                        schema.Clear();
                    }

                    // Loop until a null continuation token is received, indicating the end of the table.
                } while (continuationToken != null);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            result = WfResult.Succeeded;
            return(result);
        }
        /// <summary>
        /// Tests table access permissions with SAS, using a stored policy and using permissions on the URI.
        /// </summary>
        /// <param name="accessPermissions">The permissions to test.</param>
        /// <param name="startPk">The start partition key range.</param>
        /// <param name="startRk">The start row key range.</param>
        /// <param name="endPk">The end partition key range.</param>
        /// <param name="endRk">The end row key range.</param>
        internal void TestTableSasWithRange(
            SharedAccessTablePermissions accessPermissions,
            string startPk,
            string startRk,
            string endPk,
            string endRk)
        {
            TestContext.WriteLine("Testing SAS range: spk={0}; epk={1}; srk={2}; erk={3}", startPk, endPk, startRk, endRk);

            CloudTableClient tableClient = GenerateCloudTableClient();
            CloudTable table = tableClient.GetTableReference("T" + Guid.NewGuid().ToString("N"));

            try
            {
                table.Create();

                // Set up a policy
                string identifier = Guid.NewGuid().ToString();
                TablePermissions permissions = new TablePermissions();
                permissions.SharedAccessPolicies.Add(identifier, new SharedAccessTablePolicy
                {
                    Permissions = accessPermissions,
                    SharedAccessExpiryTime = DateTimeOffset.Now.AddDays(1)
                });
                table.SetPermissions(permissions);
                Thread.Sleep(30 * 1000);

                // Prepare SAS authentication using access identifier
                string sasString = table.GetSharedAccessSignature(new SharedAccessTablePolicy(), identifier, startPk, startRk, endPk, endRk);
                CloudTableClient identifierSasClient = new CloudTableClient(tableClient.BaseUri, new StorageCredentials(sasString));

                // Prepare SAS authentication using explicit policy
                sasString = table.GetSharedAccessSignature(
                                        new SharedAccessTablePolicy
                                        {
                                            Permissions = accessPermissions,
                                            SharedAccessExpiryTime = DateTimeOffset.Now.AddMinutes(30)
                                        },
                                        null,
                                        startPk,
                                        startRk,
                                        endPk,
                                        endRk);

                CloudTableClient explicitSasClient = new CloudTableClient(tableClient.BaseUri, new StorageCredentials(sasString));

                // Point query
                TestPointQuery(identifierSasClient, table.Name, accessPermissions, startPk, startRk, endPk, endRk);
                TestPointQuery(explicitSasClient, table.Name, accessPermissions, startPk, startRk, endPk, endRk);

                // Add row
                TestAdd(identifierSasClient, table.Name, accessPermissions, startPk, startRk, endPk, endRk);
                TestAdd(explicitSasClient, table.Name, accessPermissions, startPk, startRk, endPk, endRk);

                // Update row (merge)
                TestUpdateMerge(identifierSasClient, table.Name, accessPermissions, startPk, startRk, endPk, endRk);
                TestUpdateMerge(explicitSasClient, table.Name, accessPermissions, startPk, startRk, endPk, endRk);

                // Update row (replace)
                TestUpdateReplace(identifierSasClient, table.Name, accessPermissions, startPk, startRk, endPk, endRk);
                TestUpdateReplace(explicitSasClient, table.Name, accessPermissions, startPk, startRk, endPk, endRk);

                // Delete row
                TestDelete(identifierSasClient, table.Name, accessPermissions, startPk, startRk, endPk, endRk);
                TestDelete(explicitSasClient, table.Name, accessPermissions, startPk, startRk, endPk, endRk);

                // Upsert row (merge)
                TestUpsertMerge(identifierSasClient, table.Name, accessPermissions, startPk, startRk, endPk, endRk);
                TestUpsertMerge(explicitSasClient, table.Name, accessPermissions, startPk, startRk, endPk, endRk);

                // Upsert row (replace)
                TestUpsertReplace(identifierSasClient, table.Name, accessPermissions, startPk, startRk, endPk, endRk);
                TestUpsertReplace(explicitSasClient, table.Name, accessPermissions, startPk, startRk, endPk, endRk);
            }
            finally
            {
                table.DeleteIfExists();
            }
        }
示例#21
0
        async void Table_Clicked(object sender, EventArgs ea)
        {
            // Retrieve storage account information from connection string
            // How to create a storage connection string - http://msdn.microsoft.com/en-us/library/azure/ee758697.aspx
            CloudStorageAccount storageAccount = CreateStorageAccount();
            CloudTableClient    client         = storageAccount.CreateCloudTableClient();
            CloudTable          table          = client.GetTableReference("demotable1");

            Debug.WriteLine(table.Name + " " + table.StorageUri.PrimaryUri);
            Debug.WriteLine("isSAS:" + table.ServiceClient.Credentials.IsSAS);

            try
            {
                var exists = await table.ExistsAsync();

                Debug.WriteLine("exists; " + exists);

                var part = "xander";
                var row  = DateTime.Now.Ticks.ToString();

                DynamicTableEntity ent = new DynamicTableEntity()
                {
                    PartitionKey = part, RowKey = row
                };
                ent.Properties.Add("EncryptedProp1", new EntityProperty(string.Empty));
                ent.Properties.Add("EncryptedProp2", new EntityProperty(textEntry.Text));
                ent.Properties.Add("NotEncryptedProp", new EntityProperty(Convert.ToInt32(intEntry.Text)));

                // Insert Entity
                Debug.WriteLine("Inserting the entity.");
                await table.ExecuteAsync(TableOperation.Insert(ent));

                // Retrieve Single Entity
//				Debug.WriteLine("Retrieving the entity.");
//				TableOperation operation = TableOperation.Retrieve(ent.PartitionKey, ent.RowKey);
//				TableResult result = await table.ExecuteAsync(operation);
//				Debug.WriteLine("tag: " + result.Etag);

                // Retrieve Entities
                //
                // http://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-tables/#retrieve-all-entities-in-a-partition
                //
                data = new List <Tuple <string, int> >();
                // Initialize the continuation token to null to start from the beginning of the table
                TableContinuationToken continuationToken = null;

                TableQuery query = new TableQuery().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "xander"));
                foreach (var entity in await table.ExecuteQuerySegmentedAsync(query, continuationToken))
                {
                    try {
                        data.Add(new Tuple <string, int> (entity.Properties["EncryptedProp2"].StringValue, entity.Properties["NotEncryptedProp"].Int32Value.Value));

                        Debug.WriteLine("{0}, {1}\t{2}\t{3}",
                                        entity.PartitionKey,
                                        entity.RowKey,
                                        entity.Properties["EncryptedProp2"].StringValue,
                                        entity.Properties["NotEncryptedProp"].Int32Value);
                    } catch { Debug.WriteLine("invalid row"); }
                }
                list.ItemsSource = data;
            }
            catch (Exception ex)
            {
                Debug.WriteLine("error occurred " + ex);
            }
        }
        /// <summary>
        /// Test point queries entities inside and outside the given range.
        /// </summary>
        /// <param name="testClient">The table client to test.</param>
        /// <param name="tableName">The name of the table to test.</param>
        /// <param name="accessPermissions">The access permissions of the table client.</param>
        /// <param name="startPk">The start partition key range.</param>
        /// <param name="startRk">The start row key range.</param>
        /// <param name="endPk">The end partition key range.</param>
        /// <param name="endRk">The end row key range.</param>
        private void TestPointQuery(
            CloudTableClient testClient,
            string tableName,
            SharedAccessTablePermissions accessPermissions,
            string startPk,
            string startRk,
            string endPk,
            string endRk)
        {
            Action<BaseEntity> queryDelegate = (tableEntity) =>
            {
                TableServiceContext context = testClient.GetTableServiceContext();
                TableServiceQuery<BaseEntity> query = (from entity in context.CreateQuery<BaseEntity>(tableName)
                                                       where entity.PartitionKey == tableEntity.PartitionKey && entity.RowKey == tableEntity.RowKey
                                                       select entity).AsTableServiceQuery(context);
                IEnumerable<BaseEntity> list = query.Execute().ToList();
                Assert.AreEqual(1, list.Count());
                BaseEntity e = list.Single();
            };

            bool expectSuccess = (accessPermissions & SharedAccessTablePermissions.Query) != 0;

            // Perform test
            TestOperationWithRange(
                tableName,
                startPk,
                startRk,
                endPk,
                endRk,
                queryDelegate,
                "point query",
                expectSuccess,
                expectSuccess ? HttpStatusCode.OK : HttpStatusCode.NotFound);
        }
示例#23
0
        /// <summary>
        /// Save Recent Activites emails to azure table
        /// </summary>
        public void GetRecentActivitiesEmailData()
        {
            Trace.TraceInformation("Worker Role RecentActivity start GetRecentActivitiesEmailData() {0}", DateTime.Now);

            try
            {
                List <int> objallcompany = objLeadNotifcationBusiness.GetAllActiveCompanies();
                foreach (int companyId in objallcompany)
                {
                    try
                    {
                        accountSetting = userBusiness.GetSendNotificationsActiveUsersId(companyId);
                    }
                    catch (Exception accountSettingExp)
                    {
                        hasError = true;
                        logdata.Append("\n");
                        logdata.Append(companyID + "," + " " + "," + ErucaCRM.Utility.Enums.ResponseResult.Failure.ToString() + "," + accountSettingExp.Message + ". Error Occured When Feching  User Account Setting Data");
                        continue;
                    }
                    try
                    {
                        foreach (var user in accountSetting)
                        {
                            WorkerRoleRecentActivity.CultureName = user.User.CultureInformation == null ? "en-US" : user.User.CultureInformation.CultureName;
                            WorkerRoleCommonFunctions.SetCurrentUserCulture();
                            WorkerRoleRecentActivity.UserId = user.UserId;
                            companyID = companyId;
                            WorkerRoleRecentActivity.TimeZoneOffSet = user.User.TimeZone == null ? "0" : user.User.TimeZone.offset.ToString();
                            int TotalRecords = 0;
                            List <SSP_GetRecentActivitesForEmailNotification_Result> RecentActivities = new List <SSP_GetRecentActivitesForEmailNotification_Result>();
                            try
                            {
                                RecentActivities = objLeadNotifcationBusiness.GetRecentActivitiesForNotification(ErucaCRM.Utility.ReadConfiguration.PageSize, companyId, user.UserId, ref TotalRecords);
                                AutoMapper.Mapper.Map(RecentActivities, RecentActivitiesModel);
                            }
                            catch (Exception exRecentActivities)
                            {
                                hasError = true;
                                logdata.Append("\n");
                                logdata.Append(companyID + "," + WorkerRoleRecentActivity.UserId + "," + ErucaCRM.Utility.Enums.ResponseResult.Failure.ToString() + "," + exRecentActivities.Message + ". Error Occured When Feching Recent Activites Data");
                                continue;
                            }

                            objmailhelper = new ErucaCRM.Utility.MailHelper();
                            if (RecentActivities.Count > 0)
                            {
                                maxLeadAuditId = RecentActivities.Max(x => x.LeadAuditId);
                            }
                            foreach (var homemodel in RecentActivitiesModel)
                            {
                                try
                                {
                                    MailBodyTemplat    = WorkerRoleCommonFunctions.GetGlobalizedLabel("EmailTemplates", "RecentActivityEmailBody", WorkerRoleRecentActivity.CultureName);
                                    mailSubjectTemplat = WorkerRoleCommonFunctions.GetGlobalizedLabel("EmailTemplates", "RecentActivityEmailSubject", WorkerRoleRecentActivity.CultureName);
                                    objmailhelper.Body = objmailhelper.Body + string.Format(MailBodyTemplat, ErucaCRM.Utility.ReadConfiguration.OwnerDetail + Convert.ToInt32(homemodel.CreatedBy).Encrypt(), ErucaCRM.Utility.ReadConfiguration.NoImage, homemodel.LeadAuditId, homemodel.ActivityText, homemodel.ActivityCreatedTime);
                                    if (objmailhelper.Subject == null)
                                    {
                                        objmailhelper.Subject = string.Format(mailSubjectTemplat, TotalRecords);
                                    }
                                    leadAuditIds.Add(homemodel.LeadAuditId);
                                }
                                catch (Exception expEmailTemplate)
                                {
                                    hasError = true;
                                    logdata.Append("\n");
                                    logdata.Append(companyID + "," + WorkerRoleRecentActivity.UserId + "," + ErucaCRM.Utility.Enums.ResponseResult.Failure.ToString() + "," + expEmailTemplate.Message + ". Error Occured When Reading Config File (OwnerDetail|NoImage|) || Reading Culture File.(EmailTemplates(RecentActivityEmailBody|RecentActivityEmailSubject|))");
                                }
                            }
                            if (RecentActivitiesModel.Count > 0)
                            {
                                try
                                {
                                    objmailhelper.Body = "<div style='width: 450px'> <div style='width:100%;text-align:center'><img src='" + ErucaCRM.Utility.ReadConfiguration.SiteUrl + ErucaCRM.Utility.ReadConfiguration.WebsiteLogoPath + "'></div>" + objmailhelper.Body;

                                    if (TotalRecords > ErucaCRM.Utility.ReadConfiguration.PageSize)
                                    {
                                        objmailhelper.Body = objmailhelper.Body + " <div style='text-align: right; margin: 15px 0 0 0;'><a href='" + ErucaCRM.Utility.ReadConfiguration.PopUrl + "' style='text-decoration:none;background: none repeat scroll 0 0 #0798bc; width:300px;color: #fff; font-size: 9px; border: 1px solid #8db6e4; border-radius: 3px; cursor: pointer; margin: 14px 5px 13px; padding: 1px;'>" + WorkerRoleCommonFunctions.GetGlobalizedLabel("DashBoard", "ViewAll", WorkerRoleRecentActivity.CultureName) + "</a></div></div>";
                                    }
                                    //send email
                                    objmailhelper.ToAddress     = user.User.EmailId;
                                    objmailhelper.RecipientName = user.User.FirstName + " " + user.User.LastName;
                                    //objmailhelper.SendMailMessage(objmailhelper.ToAddress, objmailhelper.Subject, objmailhelper.Body);

                                    // Retrieve the storage account from the connection string.
                                    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                                        CloudConfigurationManager.GetSetting("StorageConnectionString"));
                                    // Create the table client.
                                    CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
                                    // Create the table if it doesn't exist.
                                    CloudTable table = tableClient.GetTableReference("Message");
                                    table.CreateIfNotExists();
                                    // Create a new customer entity.

                                    Message message = new Message();
                                    message.ToAddress      = objmailhelper.ToAddress;
                                    message.Subject        = objmailhelper.Subject;
                                    message.Body           = objmailhelper.Body;
                                    message.ReciepientName = objmailhelper.RecipientName;
                                    var sendEmailRow = new SendEmail
                                    {
                                        PartitionKey     = message.ReciepientName,
                                        RowKey           = message.ToAddress,
                                        EmailAddress     = message.ToAddress,
                                        EmailSent        = false,
                                        MessageBody      = message.Body,
                                        ScheduledDate    = DateTime.Now,
                                        FromEmailAddress = ErucaCRM.Utility.ReadConfiguration.EmailForScheduler,
                                        SubjectLine      = message.Subject,
                                    };

                                    try
                                    {
                                        TableOperation insertOperation = TableOperation.InsertOrReplace(sendEmailRow);
                                        table.Execute(insertOperation);
                                        Trace.TraceInformation("Worker Role RecentActivity saved data in message table {0}", DateTime.Now);
                                    }
                                    catch (Exception ex)
                                    {
                                        string err = "Error creating SendEmail row:  " + ex.Message;
                                        if (ex.InnerException != null)
                                        {
                                            err += " Inner Exception: " + ex.InnerException;
                                        }
                                        Trace.TraceError(err);
                                    }

                                    string queueMessageString =
                                        sendEmailRow.PartitionKey + "," +
                                        sendEmailRow.RowKey + ",";
                                    // Create the queue client.
                                    CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
                                    // Retrieve a reference to a queue.
                                    CloudQueue queue = queueClient.GetQueueReference("azuremailqueue");
                                    // Create the queue if it doesn't already exist.
                                    queue.CreateIfNotExists();
                                    var queueMessage = new CloudQueueMessage(queueMessageString);
                                    // Create a message and add it to the queue.
                                    CloudQueueMessage cloudMessage = new CloudQueueMessage("azuremailqueue");
                                    queue.AddMessage(queueMessage);
                                    Trace.TraceInformation("Worker Role RecentActivity saved data in queue table {0}", DateTime.Now);
                                    userBusiness.UpdateUserNotification(WorkerRoleRecentActivity.UserId, maxLeadAuditId);

                                    logdata.Append("\n");
                                    logdata.Append(companyID + "," + WorkerRoleRecentActivity.UserId + "," + ErucaCRM.Utility.Enums.ResponseResult.Success.ToString() + "," + objmailhelper.Subject);
                                }
                                catch (System.Net.Mail.SmtpException expEmailSend)
                                {
                                    hasError = true;
                                    logdata.Append("\n");
                                    logdata.Append(companyID + "," + WorkerRoleRecentActivity.UserId + "," + ErucaCRM.Utility.Enums.ResponseResult.Failure.ToString() + "," + expEmailSend.Message + ". Error Occured When Sending Email || Reading Config File File(PopUrl|SiteUrl|WebsiteLogoPath|PageSize).");
                                }
                                catch (Exception expEmailSend)
                                {
                                    hasError = true;
                                    logdata.Append("\n");
                                    logdata.Append(companyID + "," + WorkerRoleRecentActivity.UserId + "," + ErucaCRM.Utility.Enums.ResponseResult.Failure.ToString() + "," + expEmailSend.Message + ". Error Occured When Sending Email || Reading Config File File(PopUrl|SiteUrl|WebsiteLogoPath|PageSize).");
                                }
                            }
                        }
                    }
                    catch (Exception exeption)
                    {
                        hasError = true;
                        logdata.Append("\n");
                        logdata.Append(companyID + "," + WorkerRoleRecentActivity.UserId + "," + ErucaCRM.Utility.Enums.ResponseResult.Failure.ToString() + "," + exeption.Message);
                    }
                }
                SaveLogData();
            }
            catch (Exception exep)
            {
                logdata.Append("\n");
                logdata.Append("" + "," + "" + "," + ErucaCRM.Utility.Enums.ResponseResult.Failure.ToString() + "," + exep.Message + ". Error Occur When Feching All Company Data.Please Check Network Releated Information.");
                hasError = true;
                SaveLogData();
            }
        }
示例#24
0
        static void Main(string[] args)
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
            CloudTableClient    tableClient    = storageAccount.CreateCloudTableClient();
            CloudTable          table          = tableClient.GetTableReference("customers");

            table.CreateIfNotExists();
            Console.WriteLine("Table customers is ready!");

            ////Insert
            //CustomerEntity customer1 = new CustomerEntity("AP", "A01");
            //customer1.FirstName = "James";
            //customer1.LastName= "Mark";
            //customer1.Email = "*****@*****.**";
            //customer1.PhoneNumber = "9820001010";

            //TableOperation insertOperation = TableOperation.Insert(customer1);
            //table.Execute(insertOperation);
            //Console.WriteLine("Row added!");

            ////Bulk insert
            //TableBatchOperation batchOperation = new TableBatchOperation();

            //CustomerEntity customer1 = new CustomerEntity("MH", "A02");
            //customer1.FirstName = "Sarah";
            //customer1.LastName = "Lee";
            //customer1.Email = "*****@*****.**";
            //customer1.PhoneNumber = "9820001011";

            //CustomerEntity customer2 = new CustomerEntity("MH", "A03");
            //customer2.FirstName = "Simon";
            //customer2.LastName = "Jack";
            //customer2.Email = "*****@*****.**";
            //customer2.PhoneNumber = "9820001012";

            //CustomerEntity customer3 = new CustomerEntity("MH", "A04");
            //customer3.FirstName = "Sim";
            //customer3.LastName = "Yu";
            //customer3.Email = "*****@*****.**";
            //customer3.PhoneNumber = "9820001013";

            //CustomerEntity customer4 = new CustomerEntity("MH", "A05");
            //customer4.FirstName = "Dan";
            //customer4.LastName = "Jones";
            //customer4.Email = "*****@*****.**";
            //customer4.PhoneNumber = "9820001014";

            //batchOperation.Insert(customer1);
            //batchOperation.Insert(customer2);
            //batchOperation.Insert(customer3);
            //batchOperation.Insert(customer4);

            //table.ExecuteBatch(batchOperation);
            //Console.WriteLine("Batch of rows added!");

            TableQuery <CustomerEntity> query = new TableQuery <CustomerEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "MH"));

            foreach (CustomerEntity entity in table.ExecuteQuery(query))
            {
                Console.WriteLine("{0}, {1}\t{2}\t{3}\t{4}\t{5}", entity.PartitionKey, entity.RowKey, entity.FirstName, entity.LastName, entity.Email, entity.PhoneNumber);
            }
        }
示例#25
0
        /// <summary>
        /// Run a bunch of core Table operations. Each operation is run ~100 times to measure latency.
        /// You can swap the endpoint and compare with regular Azure Table storage.
        /// </summary>
        /// <param name="tableClient">The Azure Table storage client</param>
        /// <param name="numIterations">Number of iterations</param>
        public void Run(CloudTableClient tableClient, int numIterations)
        {
            Console.WriteLine("\n");
            Console.WriteLine("Creating Table if it doesn't exist...");

            CloudTable table = tableClient.GetTableReference("People");

            table.DeleteIfExists();
            table.CreateIfNotExists();

            List <CustomerEntity> items = new List <CustomerEntity>();
            Stopwatch             watch = new Stopwatch();

            Console.WriteLine("\n");
            Console.WriteLine("Running inserts: ");
            for (int i = 0; i < numIterations; i++)
            {
                watch.Start();

                CustomerEntity item = new CustomerEntity()
                {
                    PartitionKey = Guid.NewGuid().ToString(),
                    RowKey       = Guid.NewGuid().ToString(),
                    Email        = $"{GetRandomString(6)}@contoso.com",
                    PhoneNumber  = "425-555-0102",
                    Bio          = GetRandomString(1000)
                };

                TableOperation insertOperation = TableOperation.Insert(item);
                table.Execute(insertOperation);
                double latencyInMs = watch.Elapsed.TotalMilliseconds;

                Console.Write($"\r\tInsert #{i + 1} completed in {latencyInMs} ms.");
                items.Add(item);

                watch.Reset();
            }

            Console.WriteLine("\n");
            Console.WriteLine("Running retrieves: ");

            for (int i = 0; i < numIterations; i++)
            {
                watch.Start();

                TableOperation retrieveOperation = TableOperation.Retrieve <CustomerEntity>(items[i].PartitionKey, items[i].RowKey);
                table.Execute(retrieveOperation);
                double latencyInMs = watch.Elapsed.TotalMilliseconds;

                Console.Write($"\r\tRetrieve #{i + 1} completed in {latencyInMs} ms");

                watch.Reset();
            }

            Console.WriteLine("\n");
            Console.WriteLine("Running replace: ");


            for (int i = 0; i < numIterations; i++)
            {
                watch.Start();

                // Same latency as inserts, p99 < 15ms, and p50 < 6ms
                items[i].PhoneNumber = "425-555-5555";
                TableOperation replaceOperation = TableOperation.Replace(items[i]);
                table.Execute(replaceOperation);

                double latencyInMs = watch.Elapsed.TotalMilliseconds;
                Console.Write($"\r\tReplace #{i + 1} completed in {latencyInMs} ms");

                watch.Reset();
            }
        }
        private async Task RunAsync(CancellationToken cancellationToken)
        {
            try
            {
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=[name];AccountKey=[key]");
                CloudQueueClient    queueClient    = storageAccount.CreateCloudQueueClient();
                CloudQueue          queue          = queueClient.GetQueueReference("myqueue");
                CloudTableClient    tableClient    = storageAccount.CreateCloudTableClient();
                CloudTable          table          = tableClient.GetTableReference("mytable");

                // Create the queue if it doesn't already exist
                queue.CreateIfNotExists();

                // Create the table if it doesn't exist.
                table.CreateIfNotExists();

                // TODO: Replace the following with your own logic.
                while (!cancellationToken.IsCancellationRequested)
                {
                    // Get the next message
                    CloudQueueMessage retrievedMessage = queue.GetMessage();

                    if (retrievedMessage != null)
                    {
                        var messages = new StringBuilder("Worker received: " + retrievedMessage.AsString);
                        messages.AppendLine();

                        // read the latest messages from the table
                        TableOperation retrieveOperation = TableOperation.Retrieve <MyMessages>("Partition0", "Row0");

                        // Execute the retrieve operation.
                        TableResult retrievedResult = table.Execute(retrieveOperation);

                        MyMessages myMessages = retrievedResult.Result == null
                                              ? new MyMessages {
                            PartitionKey = "Partition0", RowKey = "Row0", LastUpdated = DateTime.Now
                        }
                                              : (MyMessages)retrievedResult.Result;


                        messages.AppendLine(myMessages.Messages);

                        var filename = RoleEnvironment.IsEmulated
                                     ? @"c:\windows\system32\cmd.exe"
                                     : @"d:\windows\system32\cmd.exe";

                        var processStartInfo = new ProcessStartInfo()
                        {
                            Arguments = "/c echo \"test message from a process on the worker vm\"",
                            FileName  = filename,
                            RedirectStandardOutput = true,
                            UseShellExecute        = false
                        };

                        var process = Process.Start(processStartInfo);

                        using (var streamReader = new StreamReader(process.StandardOutput.BaseStream))
                        {
                            messages.AppendLine(streamReader.ReadToEnd() + " at " + DateTime.Now.ToString() + " on " + RoleEnvironment.CurrentRoleInstance);
                        }

                        // replace the messages
                        myMessages.Messages    = messages.ToString();
                        myMessages.LastUpdated = DateTime.Now;

                        // Create the Replace TableOperation.
                        TableOperation replaceOperation = TableOperation.InsertOrReplace(myMessages);

                        // Execute the operation.
                        var result = table.Execute(replaceOperation);

                        //Process the message in less than 30 seconds, and then delete the message
                        queue.DeleteMessage(retrievedMessage);


                        Trace.TraceInformation("Updated myMessage");
                    }

                    Trace.TraceInformation("Working");
                    await Task.Delay(1000);
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError("Worker failed with " + ex.Message);
            }
        }
        /// <summary>
        /// Test deleting entities inside and outside the given range.
        /// </summary>
        /// <param name="testClient">The table client to test.</param>
        /// <param name="tableName">The name of the table to test.</param>
        /// <param name="accessPermissions">The access permissions of the table client.</param>
        /// <param name="startPk">The start partition key range.</param>
        /// <param name="startRk">The start row key range.</param>
        /// <param name="endPk">The end partition key range.</param>
        /// <param name="endRk">The end row key range.</param>
        private void TestDelete(
            CloudTableClient testClient,
            string tableName,
            SharedAccessTablePermissions accessPermissions,
            string startPk,
            string startRk,
            string endPk,
            string endRk)
        {
            TableServiceContext referenceContext = testClient.GetTableServiceContext();

            Action<BaseEntity> deleteDelegate = (tableEntity) =>
            {
                TableServiceContext context = testClient.GetTableServiceContext();

                context.AttachTo(tableName, tableEntity, "*");
                context.DeleteObject(tableEntity);
                context.SaveChangesWithRetries();
                context.Detach(tableEntity);
            };

            bool expectSuccess = (accessPermissions & SharedAccessTablePermissions.Delete) != 0;

            // Perform test
            TestOperationWithRange(
                tableName,
                startPk,
                startRk,
                endPk,
                endRk,
                deleteDelegate,
                "delete",
                expectSuccess,
                expectSuccess ? HttpStatusCode.NoContent : HttpStatusCode.Forbidden);
        }
示例#28
0
 public AzureUtils(string connectionString)
 {
     cloudTableClient = CloudStorageAccount.Parse(connectionString).CreateCloudTableClient();
 }
        public void TableServiceContextConcurrencyAllowsOnlySingleOperationAtOnce()
        {
            CloudTableClient    tableClient  = GenerateCloudTableClient();
            TableServiceContext tableContext = tableClient.GetTableServiceContext();

            // insert entities to query against
            for (int i = 0; i < 5; i++)
            {
                for (int m = 0; m < 100; m++)
                {
                    BaseEntity ent = new BaseEntity("testpartition" + i, m.ToString());
                    ent.Randomize();
                    ent.A = ent.RowKey;
                    tableContext.AddObject(currentTable.Name, ent);
                }

                tableContext.SaveChangesWithRetries(SaveChangesOptions.Batch);
            }

            List <OperationContext> opContexts = new List <OperationContext>();
            object   lockerObj = new object();
            DateTime start     = DateTime.Now;

            int threadsRunning = 0;

            Exception lastEx = null;

            // Start 10 simultaneous threads to query entities associated with same context.
            for (int j = 0; j < 10; j++)
            {
                opContexts.Add(new OperationContext());
                Thread newThread = new Thread((arg) =>
                {
                    Interlocked.Increment(ref threadsRunning);
                    try
                    {
                        lock (lockerObj)
                        {
                            Monitor.Wait(lockerObj);
                        }

                        TableServiceQuery <BaseEntity> query = (from ent in tableContext.CreateQuery <BaseEntity>(currentTable.Name)
                                                                select ent).AsTableServiceQuery(tableContext);

                        Debug.WriteLine(String.Format("Thread {0} start operation @ {1}", Thread.CurrentThread.ManagedThreadId, (DateTime.Now - start).TotalMilliseconds));

                        try
                        {
                            query.Execute(null, arg as OperationContext).ToList();
                        }
                        catch (Exception)
                        {
                            // no op, expected to have some exceptions
                        }

                        Debug.WriteLine(String.Format("Thread {0} end operation @ {1}", Thread.CurrentThread.ManagedThreadId, (DateTime.Now - start).TotalMilliseconds));
                    }
                    catch (Exception ex)
                    {
                        lastEx = ex;
                    }
                    finally
                    {
                        Interlocked.Decrement(ref threadsRunning);
                    }
                });

                newThread.Start(opContexts[j]);
            }

            // Wait for all threads to start
            while (Interlocked.CompareExchange(ref threadsRunning, 10, 10) < 10)
            {
                Thread.Sleep(200);
            }

            // pulse all threads
            lock (lockerObj)
            {
                Monitor.PulseAll(lockerObj);
            }

            // Wait for all threads to complete
            while (Interlocked.CompareExchange(ref threadsRunning, -1, 0) > -1)
            {
                Thread.Sleep(200);
            }

            if (lastEx != null)
            {
                throw lastEx;
            }

            foreach (OperationContext opContext in opContexts)
            {
                if (opContext.LastResult == null || opContext.LastResult.StartTime == null || opContext.LastResult.EndTime == null)
                {
                    continue;
                }

                TestHelper.AssertNAttempts(opContext, 1);

                RequestResult currRes = opContext.LastResult;

                // Make sure this results start time does not occur in between any other results start & end time
                var overlappingResults = (from ctx in opContexts
                                          where ctx.LastResult != null && ctx.LastResult != currRes &&
                                          ctx.LastResult.StartTime != null && ctx.LastResult.EndTime != null &&
                                          ctx.LastResult.StartTime.Ticks <currRes.StartTime.Ticks &&
                                                                          ctx.LastResult.EndTime.Ticks> currRes.StartTime.Ticks
                                          select ctx.LastResult);

                Assert.AreEqual(overlappingResults.Count(), 0, "Detected overlapping query");
            }
        }
示例#30
0
 public FakeTableStorageClient(CloudTableClient tableClient, StorageUri baseUri, StorageCredentials credentials)
     : base(baseUri, credentials)
 {
     _tableClient = tableClient;
 }
示例#31
0
 public AzureTableStorageService(CloudStorageAccount cloudStorageAccount)
 {
     this.cloudStorageAccount = cloudStorageAccount ?? throw new ArgumentNullException($"{nameof(cloudStorageAccount)} cannot be null");
     this.tableClient         = this.cloudStorageAccount.CreateCloudTableClient();
 }
示例#32
0
        public void ProfileStorageAccess()
        {
            CloudStorageAccount cloudStorageAccount = CloudStorageAccount.DevelopmentStorageAccount;

            #region Blob

            //Blobs
            CloudBlobClient blobClient = cloudStorageAccount.CreateCloudBlobClient();

            CloudBlobContainer cloudBlobContainer = blobClient.GetContainerReference("blobcontainer");
            cloudBlobContainer.CreateIfNotExists();

            ICloudBlob cloudBlob = cloudBlobContainer.GetBlockBlobReference("testblob");

            byte[] data = { 1, 2, 3 };

            using (TimeMeasure.Measure("blob write"))
            {
                cloudBlob.UploadFromByteArray(data, 0, 3);
            }

            #endregion

            #region Queue

            CloudQueueClient queueClient = cloudStorageAccount.CreateCloudQueueClient();

            //Queues
            CloudQueue cloudQueue = queueClient.GetQueueReference("test-queue");
            cloudQueue.CreateIfNotExists();

            using (TimeMeasure.Measure("queue write"))
            {
                cloudQueue.AddMessage(new CloudQueueMessage("testing123"));
            }

            CloudTableClient tableClient = cloudStorageAccount.CreateCloudTableClient();

            #endregion

            #region Table

            //Tables
            CloudTable cloudTable = tableClient.GetTableReference("testtable");
            cloudTable.CreateIfNotExists();

            ITableEntity tableEntity = new DynamicTableEntity
            {
                PartitionKey = "pkey",
                Properties   = new Dictionary <string, EntityProperty> {
                    { "name", new EntityProperty("chris") }
                },
                RowKey = "row1",
            };

            TableOperation operation = TableOperation.InsertOrMerge(tableEntity);

            using (TimeMeasure.Measure("table entity write"))
            {
                cloudTable.Execute(operation);
            };

            #endregion
        }
 // used for testing only
 public ASQTarget(CloudTableClient client, IProvideDelayedDeliveryTableName delayedDeliveryTableNameProvider)
 {
     this.delayedDeliveryTableNameProvider = delayedDeliveryTableNameProvider;
     this.client = client;
 }
示例#34
0
 public InboxContext(CloudTableClient client, string tableName)
     : base(client)
 {
     Requires.NotNullOrEmpty(tableName, "tableName");
     this.TableName = tableName;
 }
示例#35
0
        protected TableStorage(string connectionString)
        {
            var strageAccount = CloudStorageAccount.Parse(connectionString);

            _tableClient = strageAccount.CreateCloudTableClient();
        }
        private CloudTableClient TableClient()
        {
            if (this._storageAccount == null)
            {
                this._storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting(this.Attributes[_azureStorageCnnAttributeName]));
                this._tableClient = _storageAccount.CreateCloudTableClient();
            }

            return this._tableClient;
        }
        private HttpWebRequest GetRequest(TableStorageFieldCollection parameters, out string entryId)
        {
            if (m_Connection.State != ConnectionState.Open)
            {
                throw new TableStorageException(Resources.ConnectionNotOpen);
            }

            if (string.IsNullOrWhiteSpace(m_Connection.ConnectionString))
            {
                throw new InvalidOperationException("The ConnectionString property has not been initialized.");
            }

            Regex  queryExpression      = new Regex(@"^(?<verb>GET|POST|PUT|DELETE)\s*(?<path>/(?<tablename>[a-z][a-z0-9]{2,63}).*)$", RegexOptions.IgnoreCase);
            Match  queryMatch           = queryExpression.Match(CommandText);
            string verb                 = queryMatch.Groups["verb"].Value;
            string tableName            = queryMatch.Groups["tablename"].Value;
            string path                 = queryMatch.Groups["path"].Value;
            Regex  parametersExpression = new Regex(@"@(?<param>[\w]{1}[\w\d]*)", RegexOptions.IgnoreCase);

            MatchEvaluator evaluator = delegate(Match match)
            {
                string parameterName = match.Groups["param"].Value;

                if (parameters.Contains(parameterName))
                {
                    TableStorageField parameter = parameters[parameterName];
                    object            value     = parameter.Value;

                    if ((value == null) || DBNull.Value.Equals(value))
                    {
                        return(string.Empty);
                    }

                    return(string.Format(parameter.FormatString, parameter.Value));
                }

                throw new TableStorageException(string.Format(Resources.ParameterNotFound, parameterName));
            };

            path = parametersExpression.Replace(path, evaluator);

            CloudTableClient tableClient = m_Connection.StorageAccount.CreateCloudTableClient();

            tableClient.Timeout = TimeSpan.FromSeconds(CommandTimeout);
            tableClient.CreateTableIfNotExist(tableName);

            // TODO (Matt Magurany 8/14/2012): If a transaction exists, add to existing batch

            Uri url = new Uri(tableClient.BaseUri.AbsoluteUri.Trim('/') + path);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            request.Accept = "application/atom+xml,application/xml";
            request.Headers["x-ms-version"]          = m_Connection.ServerVersion;
            request.Headers["Accept-Charset"]        = "UTF-8";
            request.Headers["DataServiceVersion"]    = "2.0;NetFx";
            request.Headers["MaxDataServiceVersion"] = "2.0;NetFx";
            request.Method  = verb;
            request.Timeout = CommandTimeout * 1000;

            if (StringComparer.OrdinalIgnoreCase.Equals("PUT", verb) || StringComparer.OrdinalIgnoreCase.Equals("DELETE", verb))
            {
                request.Headers["If-Match"] = "*";

                entryId = url.AbsoluteUri;
            }
            else
            {
                entryId = null;
            }

            m_Connection.StorageAccount.Credentials.SignRequestLite(request);

            return(request);
        }
 private static Task<WebResponse> GetResponseAsync(this WebRequest req, CloudTableClient service)
 {
     return new APMTask<WebResponse>(
         req.BeginGetResponse,
         (asyncresult) => service.EndGetResponse(asyncresult, req),
         req.Abort);
 }
示例#39
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ShardSyncRequestManager" /> class.
 /// </summary>
 /// <param name="queue">The action queue holding the Data Elasticity actions.</param>
 /// <param name="tableClient">The table client.</param>
 public ShardSyncRequestManager(CloudQueue queue, CloudTableClient tableClient)
     : base(queue, tableClient)
 {
 }
        /// <summary>
        /// Test adding entities inside and outside the given range.
        /// </summary>
        /// <param name="testClient">The table client to test.</param>
        /// <param name="tableName">The name of the table to test.</param>
        /// <param name="accessPermissions">The access permissions of the table client.</param>
        /// <param name="startPk">The start partition key range.</param>
        /// <param name="startRk">The start row key range.</param>
        /// <param name="endPk">The end partition key range.</param>
        /// <param name="endRk">The end row key range.</param>
        private void TestAdd(
            CloudTableClient testClient,
            string tableName,
            SharedAccessTablePermissions accessPermissions,
            string startPk,
            string startRk,
            string endPk,
            string endRk)
        {
            TableServiceContext referenceContext = testClient.GetTableServiceContext();

            Action<BaseEntity> addDelegate = (tableEntity) =>
            {
                TableServiceContext context = testClient.GetTableServiceContext();

                context.AddObject(tableName, tableEntity);
                context.SaveChangesWithRetries();
            };

            bool expectSuccess = (accessPermissions & SharedAccessTablePermissions.Add) != 0;

            // Perform test
            TestOperationWithRange(
                tableName,
                startPk,
                startRk,
                endPk,
                endRk,
                addDelegate,
                "add",
                expectSuccess,
                expectSuccess ? HttpStatusCode.Created : HttpStatusCode.NotFound);
        }
示例#41
0
        private async Task <IList <TableResult> > ExecuteAsync(
            TableBatchOperation batch,
            CloudTableClient client,
            CloudTable table,
            TableRequestOptions requestOptions,
            OperationContext operationContext)
        {
            if (batch.Count == 0)
            {
                throw new InvalidOperationException(SR.EmptyBatchOperation);
            }

            if (batch.Count > 100)
            {
                throw new InvalidOperationException(SR.BatchExceededMaximumNumberOfOperations);
            }

            try
            {
                string          script = StellarBatchExecutor.MakeCreateDocumentsScript();
                StoredProcedure sproc  = await this.GetOrCreateStoredProcedureAsync(table, StellarBatchExecutor.BulkInsertOrMergeOrUpdate, script);

                List <Document>           javaScriptParams1 = new List <Document>();
                List <TableOperationType> javaScriptParams2 = new List <TableOperationType>();
                List <string>             javaScriptParams3 = new List <string>();
                string partitionKey = batch.FirstOrDefault().PartitionKey;
                foreach (TableOperation operation in batch)
                {
                    Document document = null;
                    if (operation.OperationType == TableOperationType.Retrieve)
                    {
                        document = this.GetDocumentWithPartitionAndRowKey(operation.RetrievePartitionKey, operation.RetrieveRowKey);
                    }
                    else
                    {
                        document = EntityHelpers.GetDocumentFromEntity(operation.Entity, operationContext, requestOptions);
                    }

                    javaScriptParams1.Add(document);
                    javaScriptParams2.Add(operation.OperationType);
                    javaScriptParams3.Add(operation.Entity == null ? "" : operation.Entity.ETag);
                }

                RequestOptions docdbRequestOptions = new RequestOptions {
                    PartitionKey = new PartitionKey(partitionKey)
                };
                StoredProcedureResponse <string> response =
                    await table.ServiceClient.DocumentClient.ExecuteStoredProcedureAsync <string>(sproc.SelfLink, docdbRequestOptions,
                                                                                                  javaScriptParams1.ToArray(), javaScriptParams2.ToArray(), javaScriptParams3.ToArray());

                JArray jArray = JArray.Parse(response.Response);

                List <TableResult> tableResults = new List <TableResult>();

                for (int i = 0; i < jArray.Count; i++)
                {
                    tableResults.Add(GetTableResultFromDocument(batch[i], jArray[i].ToObject <Document>(), operationContext));
                }

                return(tableResults);
            }
            catch (Exception ex)
            {
                throw EntityHelpers.GetTableResultFromException(ex);
            }
        }
        public void TableSasInvalidOperations()
        {
            CloudTableClient tableClient = GenerateCloudTableClient();
            CloudTable table = tableClient.GetTableReference("T" + Guid.NewGuid().ToString("N"));

            try
            {
                table.Create();
                // Prepare SAS authentication with full permissions
                string sasString = table.GetSharedAccessSignature(
                                        new SharedAccessTablePolicy
                                        {
                                            Permissions = SharedAccessTablePermissions.Delete,
                                            SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30)
                                        },
                                        null,
                                        null,
                                        null,
                                        null,
                                        null);

                CloudTableClient sasClient = new CloudTableClient(tableClient.BaseUri, new StorageCredentials(sasString));

                // Construct a valid set of service properties to upload.
                ServiceProperties properties = new ServiceProperties();
                properties.Logging.Version = Constants.AnalyticsConstants.LoggingVersionV1;
                properties.HourMetrics.Version = Constants.AnalyticsConstants.MetricsVersionV1;
                properties.Logging.RetentionDays = 9;
                sasClient.GetServiceProperties();
                sasClient.SetServiceProperties(properties);

                // Test invalid client operations
                // BUGBUG: ListTables hides the exception. We should fix this
                // TestHelpers.ExpectedException(() => sasClient.ListTablesSegmented(), "List tables with SAS", HttpStatusCode.NotFound);
                TestHelper.ExpectedException(() => sasClient.GetServiceProperties(), "Get service properties with SAS", HttpStatusCode.NotFound);
                TestHelper.ExpectedException(() => sasClient.SetServiceProperties(properties), "Set service properties with SAS", HttpStatusCode.NotFound);

                CloudTable sasTable = sasClient.GetTableReference(table.Name);

                // Verify that creation fails with SAS
                TestHelper.ExpectedException(() => sasTable.Create(), "Create a table with SAS", HttpStatusCode.NotFound);

                // Create the table.
                table.Create();

                // Test invalid table operations
                TestHelper.ExpectedException(() => sasTable.Delete(), "Delete a table with SAS", HttpStatusCode.NotFound);
                TestHelper.ExpectedException(() => sasTable.GetPermissions(), "Get ACL with SAS", HttpStatusCode.NotFound);
                TestHelper.ExpectedException(() => sasTable.SetPermissions(new TablePermissions()), "Set ACL with SAS", HttpStatusCode.NotFound);
            }
            finally
            {
                table.DeleteIfExists();
            }
        }
        public IActionResult GetSavedDiscovery([FromQuery] string filter)
        {
            string entityAsJson = "";

            try
            {
                _logger.LogInformation("CPAPI: Get");

                // Deserialize the filter
                DiscoveryFilters oFilter = new DiscoveryFilters();
                if (filter != null && filter.Length > 0)
                {
                    _logger.LogDebug("Deserializing filter of length: " + filter.Length);
                    oFilter = JsonConvert.DeserializeObject <DiscoveryFilters>(filter);
                }

                string storageAccountConnectionString = Utils.GetSecretOrEnvVar(ConfigurationProperties.AzureStorageAccountConnectionString, Configuration, _logger).Trim();
                // validate tika base address
                if (storageAccountConnectionString == "")
                {
                    _logger.LogWarning("Azure storage account connection string not set");
                    return(StatusCode((int)System.Net.HttpStatusCode.InternalServerError));
                }
                else
                {
                    _logger.LogDebug("Azure storage account connection string loaded");
                }

                // Process the records
                CloudStorageAccount account = CloudStorageAccount.Parse(storageAccountConnectionString);

                // Create the table client.
                //log.Info("Creating cloud table client");
                CloudTableClient tableClient = account.CreateCloudTableClient();

                // Create the table if it doesn't exist.
                //log.Info("Getting table reference");
                CloudTable table   = tableClient.GetTableReference("stlpdiscovery");
                Task       tCreate = table.CreateIfNotExistsAsync();
                tCreate.Wait();

                // Create a default query
                TableQuery <DiscoveryEntity> query = new TableQuery <DiscoveryEntity>();
                if (oFilter.filters.Count > 0)
                {
                    string combinedFilter = "";
                    foreach (DiscoveryFilter entry in oFilter.filters)
                    {
                        string cleanFilterPKey = Utils.CleanTableKey(entry.Value);
                        string pkquery         = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, cleanFilterPKey);
                        combinedFilter = pkquery;
                    }
                    // Create final combined query
                    query = new TableQuery <DiscoveryEntity>().Where(combinedFilter);
                }
                List <DiscoveryEntity> spfileEntities = new List <DiscoveryEntity>();
                TableContinuationToken token          = null;

                var runningQuery = new TableQuery <DiscoveryEntity>()
                {
                    FilterString  = query.FilterString,
                    SelectColumns = query.SelectColumns
                };

                do
                {
                    runningQuery.TakeCount = query.TakeCount - spfileEntities.Count;

                    Task <TableQuerySegment <DiscoveryEntity> > tSeg = table.ExecuteQuerySegmentedAsync <DiscoveryEntity>(runningQuery, token);
                    tSeg.Wait();
                    token = tSeg.Result.ContinuationToken;
                    spfileEntities.AddRange(tSeg.Result);
                } while (token != null && (query.TakeCount == null || spfileEntities.Count < query.TakeCount.Value));    //!ct.IsCancellationRequested &&


                //no sorting
                //spfileEntities.Sort((x, y) => String.Compare(x.Label, y.Label));

                entityAsJson = JsonConvert.SerializeObject(spfileEntities, Formatting.Indented);
            }
            catch (Exception ex)
            {
                string exceptionMsg = "Discovery batch status GET exception: " + ex.Message;
                //log.Info("Exception occurred extracting text from uploaded file \r\nError: " + ex.Message);
                if (ex.InnerException != null)
                {
                    exceptionMsg = exceptionMsg + "[" + ex.InnerException.Message + "]";
                }

                _logger.LogError(exceptionMsg);
                return(StatusCode((int)System.Net.HttpStatusCode.InternalServerError));
            }

            ObjectResult result = new ObjectResult(entityAsJson);

            return(result);
        }
        public void TableSASConstructors()
        {
            CloudTableClient tableClient = GenerateCloudTableClient();
            CloudTable table = tableClient.GetTableReference("T" + Guid.NewGuid().ToString("N"));
            try
            {
                table.Create();

                TableServiceContext context = tableClient.GetTableServiceContext();
                context.AddObject(table.Name, new BaseEntity("PK", "RK"));
                context.SaveChangesWithRetries();

                // Prepare SAS authentication with full permissions
                string sasToken = table.GetSharedAccessSignature(
                    new SharedAccessTablePolicy
                    {
                        Permissions = SharedAccessTablePermissions.Add | SharedAccessTablePermissions.Delete | SharedAccessTablePermissions.Query,
                        SharedAccessExpiryTime = DateTimeOffset.Now.AddMinutes(30)
                    },
                    null /* accessPolicyIdentifier */,
                    null /* startPk */,
                    null /* startRk */,
                    null /* endPk */,
                    null /* endRk */);

                CloudStorageAccount sasAccount;
                StorageCredentials sasCreds;
                CloudTableClient sasClient;
                CloudTable sasTable;
                TableServiceContext sasContext;
                Uri baseUri = new Uri(TestBase.TargetTenantConfig.TableServiceEndpoint);
                int count;

                // SAS via connection string parse
                sasAccount = CloudStorageAccount.Parse(string.Format("TableEndpoint={0};SharedAccessSignature={1}", baseUri.AbsoluteUri, sasToken));
                sasClient = sasAccount.CreateCloudTableClient();
                sasTable = sasClient.GetTableReference(table.Name);
                sasContext = sasClient.GetTableServiceContext();
                count = sasContext.CreateQuery<BaseEntity>(sasTable.Name).AsTableServiceQuery(sasContext).Execute().Count();
                Assert.AreEqual(1, count);

                // SAS via account constructor
                sasCreds = new StorageCredentials(sasToken);
                sasAccount = new CloudStorageAccount(sasCreds, null, null, baseUri, null);
                sasClient = sasAccount.CreateCloudTableClient();
                sasTable = sasClient.GetTableReference(table.Name);
                sasContext = sasClient.GetTableServiceContext();
                count = sasContext.CreateQuery<BaseEntity>(sasTable.Name).AsTableServiceQuery(sasContext).Execute().Count();
                Assert.AreEqual(1, count);

                // SAS via client constructor URI + Creds
                sasCreds = new StorageCredentials(sasToken);
                sasClient = new CloudTableClient(baseUri, sasCreds);
                sasContext = sasClient.GetTableServiceContext();
                count = sasContext.CreateQuery<BaseEntity>(sasTable.Name).AsTableServiceQuery(sasContext).Execute().Count();
                Assert.AreEqual(1, count);

            }
            finally
            {
                table.DeleteIfExists();
            }
        }
        public AzureTableApiDbContext(RentalRepairsAzureTableApiDbSettings settings)
        {
            var storageAccount = CloudStorageAccount.Parse(settings.StorageConnectionString);

            _tableClient = storageAccount.CreateCloudTableClient();
        }
        /// <summary>
        /// Test update (merge) on entities inside and outside the given range.
        /// </summary>
        /// <param name="testClient">The table client to test.</param>
        /// <param name="tableName">The name of the table to test.</param>
        /// <param name="accessPermissions">The access permissions of the table client.</param>
        /// <param name="startPk">The start partition key range.</param>
        /// <param name="startRk">The start row key range.</param>
        /// <param name="endPk">The end partition key range.</param>
        /// <param name="endRk">The end row key range.</param>
        private void TestUpdateMerge(
            CloudTableClient testClient,
            string tableName,
            SharedAccessTablePermissions accessPermissions,
            string startPk,
            string startRk,
            string endPk,
            string endRk)
        {
            Action<BaseEntity> updateDelegate = (tableEntity) =>
            {
                TableServiceContext context = testClient.GetTableServiceContext();

                // Merge entity
                tableEntity.A = "10";
                context.AttachTo(tableName, tableEntity, "*");
                context.UpdateObject(tableEntity);
                context.SaveChangesWithRetries();
            };

            bool expectSuccess = (accessPermissions & SharedAccessTablePermissions.Update) != 0;

            // Perform test
            TestOperationWithRange(
                tableName,
                startPk,
                startRk,
                endPk,
                endRk,
                updateDelegate,
                "update merge",
                expectSuccess,
                expectSuccess ? HttpStatusCode.NoContent : HttpStatusCode.NotFound);
        }
        public static async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log, ExecutionContext context)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string connectionsJson = File.ReadAllText(Path.Combine(context.FunctionAppDirectory, "Connections.json"));

            JObject ConnectionsObject = JObject.Parse(connectionsJson);

            string connectionString = ConnectionsObject["AZURE_STORAGE_URL"].ToString();

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);

            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

            CloudTable table = tableClient.GetTableReference("InventoryData");

            await table.CreateIfNotExistsAsync();

            var room = req.Headers["room"];

            if (string.IsNullOrEmpty(room))
            {
                room = req.Query["room"];
            }

            if (string.IsNullOrEmpty(room))
            {
                return(new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    Content = new StringContent("Please pass a room name on the query string or in the header")
                });
            }

            var partitionKey = "Demo";
            var rowKey       = room;

            try
            {
                // get the room from the table
                var getRoom = TableOperation.Retrieve <InventoryData>(partitionKey, rowKey);

                var query = await table.ExecuteAsync(getRoom);

                var currInventoryData = (InventoryData)query.Result;

                // if room not exist, create a record using default data
                if (currInventoryData == null)
                {
                    var defaultRoom = new InventoryData(partitionKey, rowKey);
                    var createRoom  = TableOperation.Insert(defaultRoom);
                    await table.ExecuteAsync(createRoom);

                    currInventoryData = (InventoryData)(await table.ExecuteAsync(getRoom)).Result;
                }

                var operation = req.Query["operation"].ToString().ToLower();
                var product   = req.Query["product"].ToString().ToLower();
                int quantity  = (!string.IsNullOrEmpty(req.Query["quantity"].ToString())) ? int.Parse(req.Query["quantity"].ToString()) : 0;
                var updated   = false;

                log.LogInformation($"Executing {operation} on {(string.IsNullOrEmpty(product) ? "no product" : product)} for {quantity}");

                if (!string.IsNullOrEmpty(operation))
                {
                    if (operation.Equals("reset"))
                    {
                        currInventoryData.LoadDefaultData();
                        updated = true;
                    }
                    else if (operation.Equals("help"))
                    {
                        currInventoryData.Help = true;
                        updated = true;
                    }
                    else if (operation.Equals("query"))
                    {
                        //This is to ensure the json data object is returned to Custom Commands.
                        updated = true;
                    }
                    else if (operation.Equals("remove"))
                    {
                        currInventoryData.Help = false;
                        if (product.Equals("blue"))
                        {
                            if (currInventoryData.FirstItem < quantity)
                            {
                                currInventoryData.Message = $"Can not ship {quantity} blue boxes because there are only {currInventoryData.FirstItem} in stock.";
                            }
                            else
                            {
                                currInventoryData.FirstItem -= quantity;
                                currInventoryData.Message    = $"Shipped {quantity} blue boxes";
                            }
                            updated = true;
                        }
                        else if (product.Equals("yellow"))
                        {
                            if (currInventoryData.SecondItem < quantity)
                            {
                                currInventoryData.Message = $"Can not ship {quantity} yellow boxes because there are only {currInventoryData.SecondItem} in stock.";
                            }
                            else
                            {
                                currInventoryData.SecondItem -= quantity;
                                currInventoryData.Message     = $"Shipped {quantity} yellow boxes";
                            }
                            updated = true;
                        }
                        else if (product.Equals("green"))
                        {
                            if (currInventoryData.ThirdItem < quantity)
                            {
                                currInventoryData.Message = $"Can not ship {quantity} green boxes because there are only {currInventoryData.ThirdItem} in stock.";
                            }
                            else
                            {
                                currInventoryData.ThirdItem -= quantity;
                                currInventoryData.Message    = $"Shipped {quantity} green boxes";
                            }
                            updated = true;
                        }
                        else
                        {
                            currInventoryData.Help = true;
                            updated = true;
                        }
                    }
                    else if (operation.Equals("deplete"))
                    {
                        currInventoryData.Help = false;
                        if (product.Equals("blue"))
                        {
                            currInventoryData.Message   = $"Shipped all {currInventoryData.FirstItem} blue boxes";
                            currInventoryData.FirstItem = 0;

                            updated = true;
                        }
                        else if (product.Equals("yellow"))
                        {
                            currInventoryData.Message    = $"Shipped all {currInventoryData.SecondItem} yellow boxes";
                            currInventoryData.SecondItem = 0;

                            updated = true;
                        }
                        else if (product.Equals("green"))
                        {
                            currInventoryData.Message   = $"Shipped all {currInventoryData.ThirdItem} green boxes";
                            currInventoryData.ThirdItem = 0;

                            updated = true;
                        }
                        else
                        {
                            currInventoryData.FirstItem  = 0;
                            currInventoryData.SecondItem = 0;
                            currInventoryData.ThirdItem  = 0;
                            currInventoryData.Message    = "Shipped all items";
                            updated = true;
                        }
                    }
                    else if (operation.Equals("add"))
                    {
                        currInventoryData.Help = false;
                        if (product.Equals("blue"))
                        {
                            currInventoryData.FirstItem += quantity;
                            currInventoryData.Message    = $"Received {quantity} blue boxes";
                            updated = true;
                        }
                        else if (product.Equals("yellow"))
                        {
                            currInventoryData.SecondItem += quantity;
                            currInventoryData.Message     = $"Received {quantity} yellow boxes";
                            updated = true;
                        }
                        else if (product.Equals("green"))
                        {
                            currInventoryData.ThirdItem += quantity;
                            currInventoryData.Message    = $"Received {quantity} green boxes";
                            updated = true;
                        }
                        else
                        {
                            currInventoryData.Help = true;
                            updated = true;
                        }
                    }
                }

                if (updated)
                {
                    var updateRoom = TableOperation.Replace(currInventoryData as InventoryData);
                    await table.ExecuteAsync(updateRoom);

                    log.LogInformation("successfully updated the record");
                }

                return(new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent(JsonConvert.SerializeObject(currInventoryData, Formatting.Indented), Encoding.UTF8, "application/json")
                });
            }
            catch (Exception e)
            {
                log.LogError(e.Message);
                return(new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    Content = new StringContent("Failed to process request")
                });
            }
        }