The Darkside

Shedding light on things and stuff

 
  Home :: Contact :: Syndication  :: Login
  87 Posts :: 0 Stories :: 56 Comments :: 2 Trackbacks

Ads

 

Donate via PayPal...

...if you feel the site helped.

Archives

Post Categories

Open Source Projects

Other Blogs

I’m playing around with the new Service Bus for Windows Server 1.0 (running locally on my dev machine) and have managed to get a few basic examples doing what they need to do: Connect, send a message and receive a message. My starting point for these examples was on MSDN, and you’ll see the similarity in the sample I provide later.

What absolutely stumped me was that when I configured a VM and deployed the service bus software to it, my examples all failed – mostly with the same themes: Authorization failure or invalid token provider.

Clearly, the inner workings of the Microsoft.ServiceBus namespace had been doing authentication all along on my dev machine, but was now failing as the VM I had set up was only on a workgroup. To solve this, I first created a local user on the VM (called SBUser), and then used the Service Bus PowerShell to create a new namespace, and set the ManageUsers parameter to my newly created user.

New-SBNamespace -Name DarksideNamespace -ManageUsers SBUser

which should return something like this to you:

Name : DarksideNamespace
AddressingScheme : Path
CreatedTime : 11/18/2012 9:56:13 AM
IssuerName : DarksideNamespace
IssuerUri : DarksideNamespace
ManageUsers : {SBUser}
DnsEntry :
PrimarySymmetricKey : ==removed==
SecondarySymmetricKey :

In the code below, I set up a token provider that has the details of the local user on the VM, and I use that token provider when creating the namespace manager and messaging factory.

Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using System.Threading.Tasks;
  7. using Microsoft.ServiceBus;
  8. using Microsoft.ServiceBus.Messaging;
  9.  
  10. namespace Darkside
  11. {
  12.     class Program
  13.     {
  14.         static string ServerFQDN = "SBWS-VM";
  15.         static int HttpPort = 9355;
  16.         static int TcpPort = 9354;
  17.         static string ServiceNamespace = "DarksideNamespace";
  18.  
  19.  
  20.         static void Main(string[] args)
  21.         {
  22.             //Build up the connection string
  23.             ServiceBusConnectionStringBuilder connBuilder = new ServiceBusConnectionStringBuilder();
  24.             connBuilder.ManagementPort = HttpPort;
  25.             connBuilder.RuntimePort = TcpPort;
  26.             connBuilder.Endpoints.Add(new UriBuilder() { Scheme = "sb", Host = ServerFQDN, Path = ServiceNamespace }.Uri);
  27.             connBuilder.StsEndpoints.Add(new UriBuilder() { Scheme = "https", Host = ServerFQDN, Port = HttpPort, Path = ServiceNamespace }.Uri);
  28.  
  29.             //Create the token provider
  30.             var tokenProvider = TokenProvider.CreateOAuthTokenProvider(connBuilder.StsEndpoints, new NetworkCredential("SBUser", "P@ssword1", ServerFQDN));
  31.  
  32.             //Create the Namespace manager
  33.             NamespaceManager namespaceManager = NamespaceManager.CreateFromConnectionString(connBuilder.ToString());
  34.             namespaceManager.Settings.TokenProvider = tokenProvider;
  35.  
  36.             //Create the Messaging factory
  37.             MessagingFactory messagingFactory = MessagingFactory.Create(connBuilder.Endpoints, tokenProvider);
  38.  
  39.             //Set up the queue
  40.             var queueName = "TestQueue";
  41.             QueueDescription queue;
  42.             if (namespaceManager.QueueExists(queueName))
  43.             {
  44.                 Console.WriteLine("Queue found - retrieving...");
  45.                 queue = namespaceManager.GetQueue(queueName);
  46.             }
  47.             else
  48.             {
  49.                 Console.WriteLine("Queue not found - creating...");
  50.                 queue = namespaceManager.CreateQueue(queueName);
  51.             }
  52.             Console.WriteLine("Message count: " + queue.MessageCount);
  53.  
  54.             //Set up the message sender and send a test message
  55.             MessageSender myMessageSender = messagingFactory.CreateMessageSender(queueName);
  56.             var sendMessage = new BrokeredMessage("This is my message for the time " + DateTime.Now.ToShortTimeString());
  57.  
  58.             Console.WriteLine("Sending message...");
  59.             myMessageSender.Send(sendMessage);
  60.  
  61.             //Set up the message receiver and receive a message
  62.             MessageReceiver myMessageReceiver = messagingFactory.CreateMessageReceiver(queueName);
  63.             Console.WriteLine("Receiving message...");
  64.             var receiveMessage = myMessageReceiver.Receive();
  65.  
  66.             Console.WriteLine("Received Message contents: " + receiveMessage.GetBody<string>());
  67.  
  68.             //Remove the message from the queue and dispose of the object
  69.             receiveMessage.Complete();
  70.             receiveMessage.Dispose();
  71.  
  72.             //Wait.
  73.             Console.ReadLine();
  74.         }
  75.     }
  76. }

Just a side note: This is demonstration code – please don’t hardcode everything the way I have done in the example :)

posted on Sunday, November 18, 2012 1:10 PM
Comments have been closed on this topic.