-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
66 lines (58 loc) · 2.22 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Queue;
namespace QueueApp
{
class Program
{
private const string ConnectionString = "DefaultEndpointsProtocol=https;EndpointSuffix=core.windows.net;AccountName=storagetemporal2021;AccountKey=tnVg72evkDBhKlpa6JOFfA/763gjZcq7SOerEDQFEcsM4FMX2Q2qCloxCEQHQ0xyXf0RHecXENBd9hGoElw4HQ==";
static async Task Main(string[] args)
{
if (args.Length > 0)
{
string value = String.Join(" ", args);
await SendArticleAsync(value);
Console.WriteLine($"Sent: {value}");
}
else
{
string value = await ReceiveArticleAsync();
Console.WriteLine($"Received {value}");
}
}
static async Task SendArticleAsync(string newsMessage)
{
CloudQueue queue = GetQueue();
bool createdQueue = await queue.CreateIfNotExistsAsync();
if (createdQueue)
{
Console.WriteLine("The queue of news articles was created.");
}
CloudQueueMessage articleMessage = new CloudQueueMessage(newsMessage);
await queue.AddMessageAsync(articleMessage);
}
static async Task<string> ReceiveArticleAsync()
{
CloudQueue queue = GetQueue();
bool exists = await queue.ExistsAsync();
if (exists)
{
CloudQueueMessage retrievedArticle = await queue.GetMessageAsync();
if (retrievedArticle != null)
{
string newsMessage = retrievedArticle.AsString;
await queue.DeleteMessageAsync(retrievedArticle);
return newsMessage;
}
}
return "<queue empty or not created>";
}
static CloudQueue GetQueue()
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConnectionString);
CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
return queueClient.GetQueueReference("newsqueue");
}
}
}