Search code examples
azureazureservicebusazure-servicebus-queues

Convert QueueClient.Create to MessagingFactory.CreateQueueClient


Trying to convert an implementation using the .net library from using QueueClient.Create to the MessagingFactory.CreateQueueClient to be able to better control the BatchFlushInterval as well as to allowing the use of multiple factories over multiple connections to increase send throughput but running into roadblocks.

Right now we are creating QueueClients (they are maintained throughout the app) like this:

QueueClient.CreateFromConnectionString(address, queueName, ReceiveMode.PeekLock); // address is the connection string from the azure portal in the form of Endpoint=sb....

Trying to change it to creating a MessagingFactory in the class construtor that will be used to create the QueueClients:

messagingFactory = MessagingFactory.Create(address.Replace("Endpoint=",""),mfs);
// later on in another part of the class
messagingFactory.CreateQueueClient(queueName, ReceiveMode.PeekLock);
// error Endpoint not found.,

This throws the error Endpoint not found. If I don't replace the Endpoint= it won't even create the MessagingFactory. What is the proper way to handle this?

Notes:

  • address = Endpoint=sb://pmg-bus-mybus.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=somekey

As an aside, we have a process that is trying to push as many messages as possible to a queue and others reading it. The readers seem to easily keep up with the sender and I'm trying to maximize the send rate.


Solution

  • The address is the base address of namespace(sb://yournamespace.servicebus.windows.net/) you are connecting to. For more information, please refer to MessagingFactory. The following is the demo code :

     var Address = "sb://yournamespace.servicebus.windows.net/"; //base address of namespace you are connecting to.
     MessagingFactorySettings MsgFactorySettings = new MessagingFactorySettings
                {
                    NetMessagingTransportSettings = new NetMessagingTransportSettings
                    {
                        BatchFlushInterval = TimeSpan.FromSeconds(2)
                    },
                    TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider("RootManageSharedAccessKey", "balabala..."),
                    OperationTimeout = TimeSpan.FromSeconds(30)
                }; //specify operating timeout (optional)
     MessagingFactory messagingFactory = MessagingFactory.Create(Address, MsgFactorySettings);
     var queue =  messagingFactory.CreateQueueClient("queueName",ReceiveMode.PeekLock);
     var message = queue.Receive(TimeSpan.Zero);