Azure Service Bus
- Nuget Windows Azure Service Bus
- To deploy to Azure, Copy Local = true (Azure doesn't have the SDK!)
- In Azure, it requires FULL TRUST. <WebRole name="WebRole" enableNativeCodeExecution="true">
- Relay: on-premise service and a client (on premise or in azure) do normal WCF via the relay service.
- Brokered: asynchronous messaging via
- Queues
- Pub/Sub: send to a "Topic" (like a queue) and read via "Subscriptions" which may be filtered)
Simple Relay service
static void Main(string[] args)
{
using (var serviceHost =
new ServiceHost(typeof(CallMeMaybeService)))
{
var sharedSecretTokenProvider =
TokenProvider.CreateSharedSecretTokenProvider("issuerName", "secretkey");
serviceHost.AddServiceEndpoint(
//contract
typeof(ICallMeMaybeService),
//binding - use the xRelayBinding versions of WCF bindings
new WS2007HttpRelayBinding(),
//address
ServiceBusEnvironment
.CreateServiceUri("sb", "serviceNamespace", "servicePath"))
//behavior for the credentials
.Behaviors.Add(new TransportClientEndpointBehavior
{
TokenProvider = sharedSecretTokenProvider
});
serviceHost.Open();
Console.WriteLine("Press [Enter] to exit");
Console.ReadLine();
serviceHost.Close();
}
}
Queuing
var credentials =
TokenProvider.CreateSharedSecretTokenProvider("issuerName", "secretkey");
var serviceUri =
ServiceBusEnvironment.CreateServiceUri("sb", "serviceNamespace", string.Empty);
//create a queue via the NamespaceManager
var mgr = new NamespaceManager(
serviceUri, credentials);
if (!mgr.QueueExists("TestQueue"))
mgr.CreateQueue("TestQueue");
//create a queue client via a messaging factory
var factory =
MessagingFactory.Create(serviceUri, credentials);
//simple -or ReceiveMode.PeekLock
var qClient = factory.CreateQueueClient("TestQueue", ReceiveMode.ReceiveAndDelete);
qClient.Send(new BrokeredMessage("Hello"));
var msg = qClient.Receive();
var payload = msg.GetBody<string>();
//in peekLock mode
msg.Complete(); //or .Abandon()
Topics/Subscriptions
//create a topic via the NamespaceManager
var myTopic = mgr.CreateTopic("TestTopic");
//create subscriptions (apply filters if required)
mgr.CreateSubscription(myTopic.Path, "Sub1");
mgr.CreateSubscription(myTopic.Path, "Sub2");
//create a topic client from messaging factory
var topicClient = factory.CreateTopicClient(myTopic.Path);
topicClient.Send(new BrokeredMessage("Hello"));
//you need a subscription client to pick it up
var subClient = factory.CreateSubscriptionClient(myTopic.Path, "Sub1",
ReceiveMode.ReceiveAndDelete);
var subMsg = subClient.Receive();