Last active
May 31, 2016 03:10
-
-
Save AndyM84/cb03fc8c1bf2b82adea690fb498a39c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Text; | |
using ppatierno.AzureSBLite; | |
using ppatierno.AzureSBLite.Messaging; | |
namespace DnxSb | |
{ | |
class DnxSbExample | |
{ | |
static string SBConnStr = "[sb-connection-string]"; | |
static string SBTopicName = "[sb-topic-name]"; | |
static string SBTopicSubscriptionName = "[sb-topic-subscription-name]"; | |
static void Main(string[] args) | |
{ | |
PushMessage("Howdy! " + DateTime.UtcNow); | |
PullMessage(1); | |
} | |
static void PushMessage(string Message) | |
{ | |
var builder = new ServiceBusConnectionStringBuilder(SBConnStr); | |
builder.TransportType = TransportType.Amqp; | |
var factory = MessagingFactory.CreateFromConnectionString(SBConnStr); | |
var client = factory.CreateTopicClient(SBTopicName); | |
var stream = new MemoryStream(Encoding.UTF8.GetBytes(Message)); | |
var message = new BrokeredMessage(stream); | |
message.Properties["time"] = DateTime.UtcNow; | |
client.Send(message); | |
client.Close(); | |
factory.Close(); | |
} | |
static void PullMessage(int MaxMessages) | |
{ | |
var builder = new ServiceBusConnectionStringBuilder(SBConnStr); | |
builder.TransportType = TransportType.Amqp; | |
var factory = MessagingFactory.CreateFromConnectionString(SBConnStr); | |
var client = factory.CreateSubscriptionClient(SBTopicName, SBTopicSubscriptionName); | |
int messagesReceived = 0, maxAttemptCount = 0; | |
while (maxAttemptCount < 1000) | |
{ | |
var req = client.Receive(); | |
if (req == null) | |
{ | |
++maxAttemptCount; | |
continue; | |
} | |
Console.WriteLine(Encoding.UTF8.GetString(req.GetBytes())); | |
req.Complete(); | |
if (++messagesReceived == MaxMessages) | |
{ | |
break; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment