Skip to content

Instantly share code, notes, and snippets.

@AndyM84
Last active May 31, 2016 03:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndyM84/cb03fc8c1bf2b82adea690fb498a39c9 to your computer and use it in GitHub Desktop.
Save AndyM84/cb03fc8c1bf2b82adea690fb498a39c9 to your computer and use it in GitHub Desktop.
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