Skip to main content

Service Bus Topic – How we can migrate from Service Bus Queue

Yesterday we saw different methods that give us the ability to communicate with Service Bus Topics and all spin around the HTTP/S.
Today we will reach a more sensible topic. How we can write a client and a server that at begging use Service Bus Queues and after a while be begin use Service Bus Topics. We want to achieve this with a minimal effort time. Also when the transition is done from queues to topics we want minimal changes in our code.
I think that we already observed that this two services are very similar. The API are almost the same, not only this, but also the message that is send and received from the services has the same time, BrokeredMessages. Until now, on all examples we used QueueClient and TopicClient classes to communicate with queues and topics. This is very good; those are the basic classes that should be used for normal situation. But if we look in these two classes we will discover a powerful property named “InternalSender”.
This class returns a “MessageSender” object. We have a similar way to get “MessageReceiver”. These are the base classes used by the framework to send and receive messages to and from Service Bus. We can imagine the QuequeClient and TopicClient as wrappers classes over two base classes that can make our life easier.
Using this two base class we will not need to know if we use a queue or a topic. These classes are configured based on a URL that can target a queue or a topic.
Before looking over the examples please check my last post where I presented how does a URL of a topic (subscription) or queue looks like. To have easier life, Windows Azure has a helper class available to use that help us to generate the URLs for topics and queues. But if you want to write them from scratch it will work without any problem.
First step is to create the base service URL (it starts with sb:// and contain the namespace of our service). This namespace is a generic namespace; don’t specify that the URL represent a queue or a topic.
Uri serviceAddress = ServiceBusEnvironment.CreateServiceUri("sb", “myFooNamspace”, string.Empty);
The last parameter represents the service path. Out of the box we don’t need to specify any value to this parameter. After this step, MessagingFactory class will help us to create URL where we include our credentials also:
MessagingFactory messagingFactory  = MessagingFactory.Create(serviceAddress, credentials);
Based on this MessagingFactory we can create MessagingReceiver and MessagingSender on the fly based on the entity path.
MessageReceiver qmr = messagingFactory.CreateMessageReceiver(“myFooQueueName”);
MessageReceiver tmr = messagingFactory.CreateMessageReceiver(“myFooTopic/subscriptions/subscriptionName1”);
MessageSender qms = MemessagingFactory.CreateMessageSender(“myFooQueueName”);
MessageSender fms = messagingFactory.CreateMessageSender (“myFooTopic”);
As you can see for the subscriptions of a topic we need to specify also the subscription name. But for the sender we don’t need to do something like this. Or course we have some helper methods like CreateTopicClient, CreateQueueClient, CreateTopicSubscription and so on, but this will not help us to make the code to be more easily ported from Service Bus Queues to Service Bus Topics.
The MessageSender and MessageReceiver classes have method as Send, Receive and all the methods that we can find in the QueueClient and TopicClient. Using this we can do the same thing. When the time will come to migrate from queue to client the only thing that we will need to do is to change the paths that specify the queues and topics.
Here you can find all the code that we need to communicate with a queue (or topic):
Uri serviceAddress = ServiceBusEnvironment.CreateServiceUri("sb", “myFooNamspace”, string.Empty);
MessagingFactory messagingFactory = MessagingFactory.Create(serviceAddress, credentials);
MessageReceiver qmr = messagingFactory.CreateMessageReceiver(“myFooQueueName”);
BrokeredMesssage brokeredMesssage = new BrokeredMessage();

qmd.Send(brokeredMesssage);
Simple, clean and smart. This is what I like at this implementation. The code remains simple and easy to understand in this way also. In this way we can migrate from queues to topics and back to the queues only by changing the configuration file.

Comments

Popular posts from this blog

How to check in AngularJS if a service was register or not

There are cases when you need to check in a service or a controller was register in AngularJS.
For example a valid use case is when you have the same implementation running on multiple application. In this case, you may want to intercept the HTTP provider and add a custom step there. This step don’t needs to run on all the application, only in the one where the service exist and register.
A solution for this case would be to have a flag in the configuration that specify this. In the core you would have an IF that would check the value of this flag.
Another solution is to check if a specific service was register in AngularJS or not. If the service was register that you would execute your own logic.
To check if a service was register or not in AngularJS container you need to call the ‘has’ method of ‘inhector’. It will return TRUE if the service was register.
if ($injector.has('httpInterceptorService')) { $httpProvider.interceptors.push('httpInterceptorService&#…

ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded

Today blog post will be started with the following error when running DB tests on the CI machine:
threw exception: System.InvalidOperationException: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information. at System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(String providerTypeName, String providerInvariantName) This error happened only on the Continuous Integration machine. On the devs machines, everything has fine. The classic problem – on my machine it’s working. The CI has the following configuration:

TeamCity.NET 4.51EF 6.0.2VS2013
It seems that there …

Run native .NET application in Docker (.NET Framework 4.6.2)

Scope
The main scope of this post is to see how we can run a legacy application written in .NET Framework in Docker.

Context
First of all, let’s define what is a legacy application in our context. By a legacy application we understand an application that runs .NET Framework 3.5 or higher in a production environment where we don’t have any more the people or documentation that would help us to understand what is happening behind the scene.
In this scenarios, you might want to migrate the current solution from a standard environment to Docker. There are many advantages for such a migration, like:

Continuous DeploymentTestingIsolationSecurity at container levelVersioning ControlEnvironment Standardization
Until now, we didn’t had the possibility to run a .NET application in Docker. With .NET Core, there was support for .NET Core in Docker, but migration from a full .NET framework to .NET Core can be costly and even impossible. Not only because of lack of features, but also because once you…