当前位置: 首页>>代码示例>>C#>>正文


C# CloudQueue.PeekMessage方法代码示例

本文整理汇总了C#中Microsoft.WindowsAzure.Storage.Queue.CloudQueue.PeekMessage方法的典型用法代码示例。如果您正苦于以下问题:C# CloudQueue.PeekMessage方法的具体用法?C# CloudQueue.PeekMessage怎么用?C# CloudQueue.PeekMessage使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Microsoft.WindowsAzure.Storage.Queue.CloudQueue的用法示例。


在下文中一共展示了CloudQueue.PeekMessage方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Run

        public override void Run()
        {
            storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
            cmdQueue = storageAccount.CreateCloudQueueClient().GetQueueReference("krawlercmd");
            urlQueue = storageAccount.CreateCloudQueueClient().GetQueueReference("krawlerurl");
            errorQueue = storageAccount.CreateCloudQueueClient().GetQueueReference("krawlererror");
            lastTenUrlQueue = storageAccount.CreateCloudQueueClient().GetQueueReference("lasttenurlcrawled");
            index = storageAccount.CreateCloudTableClient().GetTableReference("krawlerindex");
            visisted = new HashSet<string>();

            cmdQueue.CreateIfNotExists();
            urlQueue.CreateIfNotExists();
            errorQueue.CreateIfNotExists();
            lastTenUrlQueue.CreateIfNotExists();

            // This is a sample worker implementation. Replace with your logic.
            Trace.TraceInformation("WorkerRole entry point called", "Information");

            Crawler crawler = new Crawler();
            //cmdQueue.Clear();
            //cmdQueue.AddMessage(new CloudQueueMessage("stop"));

            while (true)
            {
                Thread.Sleep(500);
                Trace.TraceInformation("Working", "Information");

                CloudQueueMessage cmd = cmdQueue.PeekMessage();
                if (cmd == null || cmd.AsString == "stop")
                {
                    continue;
                }
                else if (cmd.AsString == "start")
                {
                    CloudQueueMessage url = urlQueue.GetMessage();
                    if (!visisted.Contains(url.AsString))
                    {
                        crawler.Crawl(url.AsString);
                        visisted.Add(url.AsString);
                    }

                    urlQueue.DeleteMessage(url);
                }
                else if (cmd.AsString == "load" && urlQueue.PeekMessage() == null)
                {
                    crawler.LoadQueue("http://www.cnn.com");
                }
                else
                {
                    continue;
                }
            }
        }
开发者ID:ngokchau,项目名称:pa4,代码行数:53,代码来源:WorkerRole.cs

示例2: Run

        public override void Run()
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]);
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
            datatable = tableClient.GetTableReference("datatable");
            datatable.CreateIfNotExists();

            CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
            urlQueue = queueClient.GetQueueReference("urlqueue");
            urlQueue.CreateIfNotExists();
            adminQueue = queueClient.GetQueueReference("adminqueue");
            adminQueue.CreateIfNotExists();

            ramCounter = new System.Diagnostics.PerformanceCounter("Memory", "Available MBytes");
            cpuCounter = new System.Diagnostics.PerformanceCounter();
            cpuCounter.CategoryName = "Processor";
            cpuCounter.CounterName = "% Processor Time";
            cpuCounter.InstanceName = "_Total";

            crawler = new Crawler();

            while (true)
            {
                Thread.Sleep(50);

                //If there is something in the admin queue
                if (adminQueue.PeekMessage() != null)
                {
                    CloudQueueMessage message = adminQueue.GetMessage();
                    crawler.handleAdminMessage(message);
                    adminQueue.DeleteMessage(message);
                }
                if (urlQueue.PeekMessage() != null)
                {
                    crawler.crawlingPhase(urlQueue.GetMessage());
                }
            }
        }
开发者ID:kinderst,项目名称:Web-Service-Like-Google,代码行数:38,代码来源:WorkerRole.cs

示例3: QueueSASTest

        public void QueueSASTest()
        {
            CloudQueue queue = DefaultQueueClient.GetQueueReference(GenerateNewQueueName());

            try
            {
                queue.Create();
                string messageContent = Guid.NewGuid().ToString();
                CloudQueueMessage message = new CloudQueueMessage(messageContent);
                queue.AddMessage(message);

                // Prepare SAS authentication with full permissions
                string id = Guid.NewGuid().ToString();
                DateTime start = DateTime.UtcNow;
                DateTime expiry = start.AddMinutes(30);
                QueuePermissions permissions = new QueuePermissions();
                SharedAccessQueuePermissions queuePerm = SharedAccessQueuePermissions.Add | SharedAccessQueuePermissions.ProcessMessages | SharedAccessQueuePermissions.Read | SharedAccessQueuePermissions.Update;
                permissions.SharedAccessPolicies.Add(id, new SharedAccessQueuePolicy()
                {
                    SharedAccessStartTime = start,
                    SharedAccessExpiryTime = expiry,
                    Permissions = queuePerm
                });

                queue.SetPermissions(permissions);
                Thread.Sleep(30 * 1000);

                string sasTokenFromId = queue.GetSharedAccessSignature(null, id);
                StorageCredentials sasCredsFromId = new StorageCredentials(sasTokenFromId);
                CloudQueue sasQueueFromId = new CloudQueue(queue.Uri, sasCredsFromId);
                CloudQueueMessage receivedMessage1 = sasQueueFromId.PeekMessage();
                Assert.AreEqual(messageContent, receivedMessage1.AsString);

                string sasTokenFromPolicy = queue.GetSharedAccessSignature(permissions.SharedAccessPolicies[id], null);
                StorageCredentials sasCredsFromPolicy = new StorageCredentials(sasTokenFromPolicy);
                CloudQueue sasQueueFromPolicy = new CloudQueue(queue.Uri, sasCredsFromPolicy);
                CloudQueueMessage receivedMessage2 = sasQueueFromPolicy.PeekMessage();
                Assert.AreEqual(messageContent, receivedMessage2.AsString);
            }
            finally
            {
                queue.DeleteIfExists();
            }
        }
开发者ID:thaiphuongs,项目名称:azure-sdk-for-net,代码行数:44,代码来源:CloudQueueTest.cs

示例4: QueueSASTest

        public void QueueSASTest()
        {
            CloudQueueClient client = GenerateCloudQueueClient();
            CloudQueue queue = client.GetQueueReference(GenerateNewQueueName());

            try
            {
                queue.Create();
                string messageContent = Guid.NewGuid().ToString();
                CloudQueueMessage message = new CloudQueueMessage(messageContent);
                queue.AddMessage(message);

                // Prepare SAS authentication with full permissions
                string id = Guid.NewGuid().ToString();
                DateTime start = DateTime.UtcNow;
                DateTime expiry = start.AddMinutes(30);
                QueuePermissions permissions = new QueuePermissions();
                SharedAccessQueuePermissions queuePerm = SharedAccessQueuePermissions.Add | SharedAccessQueuePermissions.ProcessMessages | SharedAccessQueuePermissions.Read | SharedAccessQueuePermissions.Update;
                permissions.SharedAccessPolicies.Add(id, new SharedAccessQueuePolicy()
                {
                    SharedAccessStartTime = start,
                    SharedAccessExpiryTime = expiry,
                    Permissions = queuePerm
                });

                queue.SetPermissions(permissions);
                Thread.Sleep(30 * 1000);

                string sasTokenFromId = queue.GetSharedAccessSignature(null, id);
                StorageCredentials sasCredsFromId = new StorageCredentials(sasTokenFromId);

                CloudStorageAccount sasAcc = new CloudStorageAccount(sasCredsFromId, null /* blobEndpoint */, new Uri(TestBase.TargetTenantConfig.QueueServiceEndpoint), null /* tableEndpoint */, null /* fileEndpoint */);
                CloudQueueClient sasClient = sasAcc.CreateCloudQueueClient();

                CloudQueue sasQueueFromSasUri = new CloudQueue(sasClient.Credentials.TransformUri(queue.Uri));
                CloudQueueMessage receivedMessage = sasQueueFromSasUri.PeekMessage();
                Assert.AreEqual(messageContent, receivedMessage.AsString);

                CloudQueue sasQueueFromSasUri1 = new CloudQueue(new Uri(queue.Uri.ToString() + sasTokenFromId));
                CloudQueueMessage receivedMessage1 = sasQueueFromSasUri1.PeekMessage();
                Assert.AreEqual(messageContent, receivedMessage1.AsString);

                CloudQueue sasQueueFromId = new CloudQueue(queue.Uri, sasCredsFromId);
                CloudQueueMessage receivedMessage2 = sasQueueFromId.PeekMessage();
                Assert.AreEqual(messageContent, receivedMessage2.AsString);

                string sasTokenFromPolicy = queue.GetSharedAccessSignature(permissions.SharedAccessPolicies[id], null);
                StorageCredentials sasCredsFromPolicy = new StorageCredentials(sasTokenFromPolicy);
                CloudQueue sasQueueFromPolicy = new CloudQueue(queue.Uri, sasCredsFromPolicy);
                CloudQueueMessage receivedMessage3 = sasQueueFromPolicy.PeekMessage();
                Assert.AreEqual(messageContent, receivedMessage3.AsString);
            }
            finally
            {
                queue.DeleteIfExists();
            }
        }
开发者ID:jianghaolu,项目名称:azure-storage-net,代码行数:57,代码来源:CloudQueueTest.cs

示例5: UpdateQueueSASTest

        public void UpdateQueueSASTest()
        {
            CloudQueueClient client = GenerateCloudQueueClient();
            CloudQueue queue = client.GetQueueReference(GenerateNewQueueName());

            try
            {
                queue.Create();
                string messageContent = Guid.NewGuid().ToString();
                CloudQueueMessage message = new CloudQueueMessage(messageContent);
                queue.AddMessage(message);
                SharedAccessQueuePolicy policy = new SharedAccessQueuePolicy()
                {
                    SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5),
                    SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30),
                    Permissions = SharedAccessQueuePermissions.Add | SharedAccessQueuePermissions.ProcessMessages,
                };
                string id = Guid.NewGuid().ToString();
                string sasToken = queue.GetSharedAccessSignature(policy, null);

                StorageCredentials sasCreds = new StorageCredentials(sasToken);
                CloudQueue sasQueue = new CloudQueue(queue.Uri, sasCreds);

                TestHelper.ExpectedException(
                    () => sasQueue.PeekMessage(),
                    "Peek when Sas does not allow Read access on the queue",
                    HttpStatusCode.NotFound);

                sasQueue.AddMessage(message);

                SharedAccessQueuePolicy policy2 = new SharedAccessQueuePolicy()
                {
                    SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5),
                    SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30),
                    Permissions = SharedAccessQueuePermissions.Add | SharedAccessQueuePermissions.ProcessMessages | SharedAccessQueuePermissions.Read,
                };

                string sasToken2 = queue.GetSharedAccessSignature(policy2, null);
                sasCreds.UpdateSASToken(sasToken2);
                sasQueue = new CloudQueue(queue.Uri, sasCreds);

                sasQueue.PeekMessage();
            }
            finally
            {
                queue.DeleteIfExists();
            }
        }
开发者ID:jianghaolu,项目名称:azure-storage-net,代码行数:48,代码来源:CloudQueueTest.cs

示例6: CloudQueueSASWithAbsoluteUri

        public void CloudQueueSASWithAbsoluteUri()
        {
            CloudQueueClient queueClient = GenerateCloudQueueClient();

            CloudQueue queue = queueClient.GetQueueReference(queueClient.BaseUri + GenerateNewQueueName());
            try
            {
                queue.CreateIfNotExists();

                SharedAccessQueuePolicy policy = new SharedAccessQueuePolicy()
                {
                    Permissions = SharedAccessQueuePermissions.Read,
                    SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5),
                    SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(10)
                };

                string sasToken = queue.GetSharedAccessSignature(policy);
                StorageCredentials creds = new StorageCredentials(sasToken);

                CloudQueue sasQueue = new CloudQueue(queue.Uri, creds);
                sasQueue.PeekMessage();
            }
            finally
            {
                queue.DeleteIfExists();
            }
        }
开发者ID:jasonnewyork,项目名称:azure-storage-net,代码行数:27,代码来源:CloudQueueTest.cs

示例7: CloudQueueSASSharedProtocolsQueryParam

        public void CloudQueueSASSharedProtocolsQueryParam()
        {
            CloudQueueClient client = GenerateCloudQueueClient();
            CloudQueue queue = client.GetQueueReference(GenerateNewQueueName());
            try
            {
                queue.Create();
                SharedAccessQueuePolicy policy = new SharedAccessQueuePolicy()
                {
                    Permissions = SharedAccessQueuePermissions.Read,
                    SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5),
                    SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30),
                };

                string sampleMessageContent = "sample content";
                CloudQueueMessage message = new CloudQueueMessage(sampleMessageContent);
                queue.AddMessage(message);

                foreach (SharedAccessProtocol? protocol in new SharedAccessProtocol?[] { null, SharedAccessProtocol.HttpsOrHttp, SharedAccessProtocol.HttpsOnly })
                {
                    string queueToken = queue.GetSharedAccessSignature(policy, null, protocol, null);
                    StorageCredentials queueSAS = new StorageCredentials(queueToken);
                    Uri queueSASUri = new Uri(queue.Uri + queueSAS.SASToken);
                    StorageUri queueSASStorageUri = new StorageUri(new Uri(queue.StorageUri.PrimaryUri + queueSAS.SASToken), new Uri(queue.StorageUri.SecondaryUri + queueSAS.SASToken));

                    int httpPort = queueSASUri.Port;
                    int securePort = 443;

                    if (!string.IsNullOrEmpty(TestBase.TargetTenantConfig.QueueSecurePortOverride))
                    {
                        securePort = Int32.Parse(TestBase.TargetTenantConfig.QueueSecurePortOverride);
                    }

                    var schemesAndPorts = new[] {
                        new { scheme = Uri.UriSchemeHttp, port = httpPort},
                        new { scheme = Uri.UriSchemeHttps, port = securePort}
                    };

                    CloudQueue queueWithSAS;
                    CloudQueueMessage resultMessage;

                    foreach (var item in schemesAndPorts)
                    {
                        queueSASUri = TransformSchemeAndPort(queueSASUri, item.scheme, item.port);
                        queueSASStorageUri = new StorageUri(TransformSchemeAndPort(queueSASStorageUri.PrimaryUri, item.scheme, item.port), TransformSchemeAndPort(queueSASStorageUri.SecondaryUri, item.scheme, item.port));

                        if (protocol.HasValue && protocol == SharedAccessProtocol.HttpsOnly && string.CompareOrdinal(item.scheme, Uri.UriSchemeHttp) == 0)
                        {
                            queueWithSAS = new CloudQueue(queueSASUri);
                            TestHelper.ExpectedException(() => queueWithSAS.PeekMessage(), "Access a queue using SAS with a shared protocols that does not match", HttpStatusCode.Unused);

                            queueWithSAS = new CloudQueue(queueSASStorageUri, null);
                            TestHelper.ExpectedException(() => queueWithSAS.PeekMessage(), "Access a queue using SAS with a shared protocols that does not match", HttpStatusCode.Unused);
                        }
                        else
                        {
                            queueWithSAS = new CloudQueue(queueSASUri);
                            resultMessage = queueWithSAS.PeekMessage();
                            Assert.AreEqual(sampleMessageContent, resultMessage.AsString);

                            queueWithSAS = new CloudQueue(queueSASStorageUri, null);
                            resultMessage = queueWithSAS.PeekMessage();
                            Assert.AreEqual(sampleMessageContent, resultMessage.AsString);
                        }
                    }
                }
            }
            finally
            {
                queue.DeleteIfExists();
            }
        }
开发者ID:Gajendra-Bahakar,项目名称:azure-storage-net,代码行数:72,代码来源:CloudQueueTest.cs

示例8: CloudQueueSASIPAddressHelper

        /// <summary>
        /// Helper function for testing the IPAddressOrRange funcitonality for queues
        /// </summary>
        /// <param name="generateInitialIPAddressOrRange">Function that generates an initial IPAddressOrRange object to use. This is expected to fail on the service.</param>
        /// <param name="generateFinalIPAddressOrRange">Function that takes in the correct IP address (according to the service) and returns the IPAddressOrRange object
        /// that should be accepted by the service</param>
        public void CloudQueueSASIPAddressHelper(Func<IPAddressOrRange> generateInitialIPAddressOrRange, Func<IPAddress, IPAddressOrRange> generateFinalIPAddressOrRange)
        {
            CloudQueueClient client = GenerateCloudQueueClient();
            CloudQueue queue = client.GetQueueReference(GenerateNewQueueName());

            try
            {
                queue.Create();
                SharedAccessQueuePolicy policy = new SharedAccessQueuePolicy()
                {
                    Permissions = SharedAccessQueuePermissions.Read,
                    SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5),
                    SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30),
                };

                string sampleMessageContent = "sample content";
                CloudQueueMessage message = new CloudQueueMessage(sampleMessageContent);
                queue.AddMessage(message);

                // The plan then is to use an incorrect IP address to make a call to the service
                // ensure that we get an error message
                // parse the error message to get my actual IP (as far as the service sees)
                // then finally test the success case to ensure we can actually make requests

                IPAddressOrRange ipAddressOrRange = generateInitialIPAddressOrRange();
                string queueToken = queue.GetSharedAccessSignature(policy, null, null, ipAddressOrRange);
                StorageCredentials queueSAS = new StorageCredentials(queueToken);
                Uri queueSASUri = queueSAS.TransformUri(queue.Uri);
                StorageUri queueSASStorageUri = queueSAS.TransformUri(queue.StorageUri);

                CloudQueue queueWithSAS = new CloudQueue(queueSASUri);
                OperationContext opContext = new OperationContext();
                IPAddress actualIP = null;
                opContext.ResponseReceived += (sender, e) =>
                {
                    Stream stream = e.Response.GetResponseStream();
                    stream.Seek(0, SeekOrigin.Begin);
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        string text = reader.ReadToEnd();
                        XDocument xdocument = XDocument.Parse(text);
                        actualIP = IPAddress.Parse(xdocument.Descendants("SourceIP").First().Value);
                    }
                };

                bool exceptionThrown = false;
                CloudQueueMessage resultMessage;
                try
                {
                    resultMessage = queueWithSAS.PeekMessage(null, opContext);
                }
                catch (StorageException)
                {
                    exceptionThrown = true;
                    Assert.IsNotNull(actualIP);
                }

                Assert.IsTrue(exceptionThrown);
                ipAddressOrRange = generateFinalIPAddressOrRange(actualIP);
                queueToken = queue.GetSharedAccessSignature(policy, null, null, ipAddressOrRange);
                queueSAS = new StorageCredentials(queueToken);
                queueSASUri = queueSAS.TransformUri(queue.Uri);
                queueSASStorageUri = queueSAS.TransformUri(queue.StorageUri);

                queueWithSAS = new CloudQueue(queueSASUri);
                resultMessage = queue.PeekMessage();
                Assert.AreEqual(sampleMessageContent, resultMessage.AsString);
                Assert.IsTrue(queueWithSAS.StorageUri.PrimaryUri.Equals(queue.Uri));
                Assert.IsNull(queueWithSAS.StorageUri.SecondaryUri);

                queueWithSAS = new CloudQueue(queueSASStorageUri, null);
                resultMessage = queue.PeekMessage();
                Assert.AreEqual(sampleMessageContent, resultMessage.AsString);
                Assert.IsTrue(queueWithSAS.StorageUri.Equals(queue.StorageUri));

            }
            finally
            {
                queue.DeleteIfExists();
            }
        }
开发者ID:Gajendra-Bahakar,项目名称:azure-storage-net,代码行数:87,代码来源:CloudQueueTest.cs


注:本文中的Microsoft.WindowsAzure.Storage.Queue.CloudQueue.PeekMessage方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。