Queue Azure queue storage (2)

Label Azurecloud computingQueue storageAzure
656 people read comments(0) Collection Report

We are connected to a discussion of queue storage, in which we have completed the configuration and connection in accordance with the steps in (1). Below we'll see how to use the specific storage.

Access to Queue queue storage in programming
Acquisition component

You can use NuGet to get the Microsoft.WindowsAzure.Storage.dll component. Right click on your Solution in Explorer Manage, and then select Nuget Packages Project, the online search "WindowsAzure.Storage" and then click Install Azure storage package and related packages.

SDK.NET Azure version also contains Microsoft.WindowsAzure.Storage.dll, you can get the K.NET developer center. The component is installed in the Files%\Microsoft SDKs\Azure.NET SDK\\ref\%Program directory.

Namespace declarations
Add the following code to the top of your C# program for your access to the Azure store:

Microsoft using.WindowsAzure.Storage;
Microsoft using.WindowsAzure.Storage.Auth;
Microsoft using.WindowsAzure.Storage.Queue;

Make sure you refer to the Microsoft.WindowsAzure.Storage.dll component

Retrieve connection string

You can useCloudStorageAccountType to display your storage account information. If you use the Azure project template or you have a reference to the Microsoft.WindowsAzure.CloudConfigurationManager namespace, you can also use theCloudConfigurationManagerType from the Azure service configuration table to retrieve your storage connection string and account information.

StorageAccount CloudStorageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));

If your application does not refer to Microsoft.WindowsAzure.CloudConfigurationManager, and your connection string is stored in web.config or app.config, you can use it.ConfigurationManagerTo get the connection string. But you also need to add a reference to the System.Configuration.dll, and then add a namespace declaration.
System.Configuration using;

System.Configuration using;
...
StorageAccount CloudStorageAccount = CloudStorageAccount.Parse (
ConfigurationManager.ConnectionStrings["StorageConnectionString"]);

Create a queue
CloudQueueClientCan let you access to reference's objects queues. The following code is a new oneCloudQueueClientObject. All of the code used in this example uses the same storage connection string in the Azure application service configuration.

Storage account from connection string / / Retrieve
StorageAccount CloudStorageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));

The queue client / / Create
QueueClient CloudQueueClient = storageAccount.CreateCloudQueueClient();

Use the queueClient object to get a reference to the queue you want to use. If queue does not exist, you can create a

A reference to a queue / / Retrieve
CloudQueueQueue =QueueClient.GetQueueReference ("Myqueue");

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

Insert a message queue into message
Insert a message into an existing queue, and then create a new CloudQueueMessage, and then call the AddMessage method. CloudQueueMessage can be created from a string (UTF-8) or from a byte array. Here is the sample code

Storage account from connection string. / / Retrieve
StorageAccount CloudStorageAccount=CloudStorageAccount.(Parse
CloudConfigurationManager.GetSetting ("StorageConnectionString"));

The queue client. / / Create
QueueClient CloudQueueClient=StorageAccount.CreateCloudQueueClient ();

A reference to a queue. / / Retrieve
CloudQueueQueue =QueueClient.GetQueueReference ("Myqueue");

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

/ /创建消息and add it to the尾巴。
cloudqueuemessage消息= 新的cloudqueuemessage(“世界,你好!”);
尾巴addmessage(消息);

peek下一条消息
你可以通过调用peekmessage方法来查看queue的头的一条消息而不需要删除消息本身。

存储/检索account from connection string
cloudstorageaccount storageaccount = cloudstorageaccount秒差距。(
cloudconfigurationmanager.getsetting“storageconnectionstring”))

客户/ create the尾巴
cloudqueueclient queueclient = storageaccount.createcloudqueueclient(的)

/ /检索参考到尾
cloudqueue = queueclient尾.getqueuereference“myqueue”

//PEEK at the next消息
cloudqueuemessage peekedmessage =尾.peekmessage(的)

/ /显示消息。
控制台.writeline(peekedmessage.asstring

修改加入queue队列的消息
您可以在原处修改一个queue中的内容。如果消息本身是work任务,那您可以使用这个功能来来更新work任务的状态。下面的示例代码展示了更新queue消息的新内容,把可视性timeout的时间延长了60秒。

存储/检索帐户从连接字符串。
cloudstorageaccount storageaccount = cloudstorageaccount秒差距。(
cloudconfigurationmanager.getsetting“storageconnectionstring”))

/ / create the尾巴的客户。
cloudqueueclient queueclient = storageaccount.createcloudqueueclient(的)

/ /检索参考到尾。
cloudqueue = queueclient尾.getqueuereference“myqueue”

/ / get the消息from the尾巴and更新信息内容。
cloudqueuemessage消息=尾.getmessage(的)
消息.setmessagecontent“更新内容。”
尾巴.updatemessage(消息,
timespan.fromseconds0)/使它立即可见。
messageupdatefields很高兴。| messageupdatefields能见度。

- queue下一条消息
可以通过两步来de尾queue中的消息。调用getmessage来获得queue中的下一条消息。getmessage返回的消息将会对已其它的代码变成不可见。默认的情况下,这个不可见性持续30秒.要想完全删除这条消息,你需要调用deletemessage方法。

存储/检索account from connection string
cloudstorageaccount storageaccount = cloudstorageaccount秒差距。(
cloudconfigurationmanager.getsetting“storageconnectionstring”))

客户/ create the尾巴
cloudqueueclient queueclient = storageaccount.createcloudqueueclient(的)

/ /检索参考到尾
cloudqueue = queueclient尾.getqueuereference“myqueue”

/ / get the next消息
cloudqueuemessage retrievedmessage =尾.getmessage(的)

/ /过程的信息inless than30第二,and然后删除的消息
尾巴.deletemessage(retrievedmessage)

针对common存储apis使用async - await模式尾

下面的示例代码展示了针对common存储apis使用的async - await模式尾

/ / create the尾巴如果不要already exist
如果(await尾巴createifnotexistsasync())
{
控制台WriteLine(“尾巴”{ 0 }“created”尾巴name);
}
其他的
{
控制台WriteLine(“尾巴”{ 0 }“存在”尾巴name);
}

/ /创建消息to put in the尾巴
cloudqueuemessage cloudqueuemessage= 新的cloudqueuemessage(“我的信息”);

/ /异步消息入队the
await尾巴addmessageasync(cloudqueuemessage);
控制台WriteLine(“消息added”);

/ /异步消息dequeue the
cloudqueuemessage retrievedmessage=await尾巴getmessageasync();
控制台WriteLine(“检索消息以及高兴’{ 0 }。”retrievedmessage,asstring);

/ /异步删除的消息
await尾巴deletemessageasync(retrievedmessage);
控制台WriteLine(“删除“消息”);

其它de - queuing消息的方法
通常可以有两种方式来自定义消息中的检索。首先,您可以拿到一批消息(最多32)。第二您可以设置更长的不可见时间来允许代码完成消息的操作。

存储/检索帐户从连接字符串。
cloudstorageaccount storageaccount = cloudstorageaccount秒差距。(
cloudconfigurationmanager.getsetting“storageconnectionstring”))

/ / create the尾巴的客户。
cloudqueueclient queueclient = storageaccountcreatecloudqueueclient。()

检索一个引用到队列。
云队列队列= queueclientgetqueuereference。“myqueue”

foreach(cloudqueuemessage消息队列getmessages。二十,时间跨度fromminutes。))
{
/ /处理所有消息少于分钟,删除处理后的每个消息。
队列deletemessage。(信息)
}

得到队列队列的长度
你可以拿到队列中估计的消息数。fetchattributes方法向队列服务抓取队列属性信息,其中包括消息数。

从连接字符串中检索存储帐户。
cloudstorageaccount storageaccount = cloudstorageaccount解析(。
cloudconfigurationmanager getsetting(。“storageconnectionstring”);

创建队列客户端。
cloudqueueclient queueclient = storageaccount。createcloudqueueclient();

检索一个引用到队列。
云队列队列queueclient getqueuereference(=。“myqueue”);

获取队列属性。
队列fetchattributes();

/ /检索缓存的近似信息计数。
int?cachedmessagecount =队列approximatemessagecount;

/ /显示消息数。
WriteLine(控制台。“队列中的消息数:”+ cachedmessagecount);

删除一个队列
调用删除方法来删除队列对象

从连接字符串中检索存储帐户。
cloudstorageaccount storageaccount=cloudstorageaccount解析(
cloudconfigurationmanagergetsetting(“storageconnectionstring”);

创建队列客户端。
cloudqueueclient queueclient=storageaccountcreatecloudqueueclient();

检索一个引用到队列。
云队列队列 =queueclientgetqueuereference(“myqueue”);

/删除队列。
队列delete();
猜你在找
查看评论
*以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:6815次
    • 积分:一百六十五
    • 等级:
    • 排名:千里之外
    • 原创:10篇
    • 转载:2篇
    • 译文:0篇
    • 评论:2条
    文章存档
    最新评论