Skip to content

Instantly share code, notes, and snippets.

@mkropat
Last active September 19, 2017 13:45
Show Gist options
  • Save mkropat/6b7c2837ae9e4cdaf0fb68bdc3a5d3a9 to your computer and use it in GitHub Desktop.
Save mkropat/6b7c2837ae9e4cdaf0fb68bdc3a5d3a9 to your computer and use it in GitHub Desktop.
<Query Kind="Program">
<NuGetReference>WindowsAzure.ServiceBus</NuGetReference>
<Namespace>Microsoft.ServiceBus</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
<Namespace>Microsoft.ServiceBus.Messaging</Namespace>
<Namespace>System.Runtime.Serialization</Namespace>
</Query>
async Task Main()
{
var address = ServiceBusEnvironment.CreateServiceUri("sb", Environment.GetEnvironmentVariable("AZURE_SB_NAMESPACE"), string.Empty);
var creds = TokenProvider.CreateSharedAccessSignatureTokenProvider(
Environment.GetEnvironmentVariable("AZURE_SB_KEY_NAME"),
Environment.GetEnvironmentVariable("AZURE_SB_ACCESS_KEY"));
var topic = Environment.GetEnvironmentVariable("AZURE_SB_TOPIC");
var subscription = $"{Environment.MachineName}-{Process.GetCurrentProcess().Id}";
var namespaceClient = new NamespaceManager(address, creds);
if (!await namespaceClient.SubscriptionExistsAsync(topic, subscription))
{
await namespaceClient.CreateSubscriptionAsync(new SubscriptionDescription(topic, subscription)
{
AutoDeleteOnIdle = TimeSpan.FromMinutes(10),
});
}
var factory = await MessagingFactory.CreateAsync(address, creds);
var client = factory.CreateSubscriptionClient(topic, subscription, ReceiveMode.ReceiveAndDelete);
while (true)
{
try
{
var message = await GetMessage(client);
LINQPad.Extensions.Dump(message);
}
catch (MessagingException ex)
{
ex.Dump();
}
await Task.Delay(TimeSpan.FromSeconds(10));
}
}
async Task<string> GetMessage(SubscriptionClient client)
{
using (var message = await client.ReceiveAsync())
{
var body = message?.GetBody<string>(new DataContractSerializer(typeof(string)));
return body;
}
}
<Query Kind="Program">
<NuGetReference>Newtonsoft.Json</NuGetReference>
<NuGetReference>WindowsAzure.ServiceBus</NuGetReference>
<Namespace>Microsoft.ServiceBus.Messaging</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
<Namespace>Newtonsoft.Json</Namespace>
<Namespace>System.Runtime.Serialization</Namespace>
</Query>
async Task Main()
{
var connectionString = Environment.GetEnvironmentVariable("AZURE_SB_CONNECTION"); // get from "Shared access policy" details in Azure portal
var topic = "derp"; // must create this through the Azure portal
var subscription = "herp"; // must create this through the Azure portal
var receiver = SubscriptionClient.CreateFromConnectionString(connectionString, topic, subscription);
try
{
while (true)
{
using (var message = await receiver.ReceiveAsync())
{
if (message == null)
{
Debug.WriteLine("got a null");
await Task.Delay(TimeSpan.FromMilliseconds(100));
}
else
{
// thanks to this post for pointers on deserializing the body: https://abhishekrlal.com/2012/03/30/formatting-the-content-for-service-bus-messages/
var body = message.GetBody<string>(new DataContractSerializer(typeof(string)));
JsonConvert.DeserializeObject<TestMessage>(body).Dump();
await message.CompleteAsync();
}
}
}
}
finally
{
await receiver.CloseAsync();
}
}
public class TestMessage
{
public string Id { get; set; }
public string Message { get; set; }
public string Timestamp { get; set; }
}
let azure = require('azure-sb'); // npm install azure-sb
let serviceBusService = azure.createServiceBusService(process.env.AZURE_SB_CONNECTION);
let body = {
Message: 'sent from node',
Timestamp: new Date().toISOString()
};
let message = {
body: '<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">' + JSON.stringify(body) + '</string>'
};
let topic = 'derp';
serviceBusService.sendTopicMessage(topic, message, (error) => {
if (error) {
console.error('sendTopicMessage error:', error);
}
});
<Query Kind="Program">
<NuGetReference>Newtonsoft.Json</NuGetReference>
<NuGetReference>WindowsAzure.ServiceBus</NuGetReference>
<Namespace>Microsoft.ServiceBus.Messaging</Namespace>
<Namespace>Newtonsoft.Json</Namespace>
<Namespace>System.Runtime.Serialization.Json</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
<Namespace>System.Runtime.Serialization</Namespace>
</Query>
async Task Main()
{
var connectionString = Environment.GetEnvironmentVariable("AZURE_SB_CONNECTION"); // get from "Shared access policy" details in Azure portal
var topic = "derp"; // must create this through the Azure portal
var client = TopicClient.CreateFromConnectionString(connectionString, topic);
var message = JsonConvert.SerializeObject(new TestMessage
{
Id = Guid.NewGuid().ToString(),
Message = "sent from .net",
Timestamp = DateTime.UtcNow.ToString()
});
await client.SendAsync(new BrokeredMessage(message, new DataContractSerializer(typeof(string))));
await client.CloseAsync();
}
public class TestMessage
{
public string Id { get; set; }
public string Message { get; set; }
public string Timestamp { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment