Skip to main content

Windows Azure Service Bus - Adding properties to the BrokeredMessage automatic

Some days ago I started to look over a big system and trying to identify places where Windows Azure can be used. Trying to imagine a PoC I realized that I need a mechanism that is able to add properties to a BrokeredMessage automatically.
For example we can have a serializable item that is added to the BrokeredMessage. This message can have one, two or n properties that we want to add them to the BrokeredMessage. These properties will be used by the topics and subscription to filter and redirect each message.
This target can be accomplished in different ways:
  1. Our object can contain a method that returns the list of properties as KeyValuePair. This can be used to populate the Properties of the BrokeredMesssage.
  2. Another option is to create a separate class that store the list of properties for each class and to call an external method that will populate our BrokeredMessage with our custom properties.
  3. Create a custom attribute that will be used to mark properties that will be added to the BrokeredMessage. After adding our object to the BrokeredMessage, we will need to call another method that will processed our object and add the marked properties to the BrokeredMessage.
Each solution has good parts and bad parts. Also, based on the developer preference and the project characteristics, each team can prefer a different solution.
Using the first option has the downside that we create a coupling between our class and Windows Azure. Even if is not directly, the method will be relevant only for Windows Azure. If we want to reuse these classes, maybe we don’t want to load the class with a lot of methods.
The second option creates a clear decoupling between the item that is sent using BrokeredMessage and the system. The downside of is on the configuration part. We need to manage a separate configuration for items that will be added on the BrokeredMessage. Also we will need to create a mechanism that will read the configuration and extract from each item the properties that need to be exposed in the BrokeredMessage.
The last option creates a small coupling between our item and the mechanism that sends the message on the wire. In comparison with the first and the second option, this option in somehow in the middle. It will not add to much complexity to our project. The only problem that I see here is the reflection mechanism that needs to be used to retrieve the properties that need to be exposed in the BrokeredMessage.
I will take the last option that I will try to cover. To be able to develop a mechanism that automatically add the properties of BrokeredMessage the properties of an items that are marked with a custom properties we will need to create two things
  1. A custom attribute that will be used to mark the properties of our item
  2. Decorate our item properties with our custom attribute.
  3. A function that will extract the properties that are marked with our custom attribute and add them to the properties list of the BrokeredMessage
We need to create our attribute. This attribute will be used only on the properties that need to be exposed in the BrokeredMessage. Using the AttributeUsage attribute we will limit the places where our attribute can be used – only on properties.
[AttributeUsage(AttributeTargets.Property)]
public class ExposedProperty : System.Attribute
{
    private string name;

    public ExposedProperty(string name)
    {
        this.name = name;
    }
}
In this moment we have our attribute that can be used on the properties of our item that we want to expose.
public class Item
{
        [ExposedProperty( “name”, PropertyDirection.ReadAndWrite))]
        public string Name { get; set; }

        public int Age { get; set; }

        [ExposedProperty( “job” , PropertyDirection.ReadAndWrite))]
        public string Job { get; set; }

        public double Salary { get; set; }

}
As you can see we can have different properties names (one in our Item class and another one that is used in the BrokeredMessage). This can be very useful when we exposed different object that are coming from different sources. The PropertyDirection specify the direction of data. This is not mandatory, but can be useful and also tell us the direction of data.
The last step that we need to do is to write a method that will extract all the properties that are decorated with our custom attribute and add them to our BrokeredMessage.
public void AddCustomProperties(BrokeredMessage message, object item)
{
    Type exposedPropertyType = typeof(ExposedProperty);
    PropertyInfo[] itemProperties = serializableObject.GetType().GetProperties();
    foreach (PropertyInfo property in itemProperties)       
    {
        foreach (ExposedProperty exposedProperty in property.GetCustomAttributes(exposedPropertyType, true))
        {
            var propertyValue = property.GetValue(item, null);
            message.Properties.Add(exposedProperty.Name, propertyValue);
        }
    }
}
In this moment we have all that we need to add our item and all the custom properties to the BrokeredMessage. In the following example we are adding the item to the BrokeredMessage.
Item item = new Item();
...

SubscriptionClient subscriptionClient = SubscriptionClient.CreateFromConnectionString(
    CloudConfigurationManager.GetSetting("ServiceBusConnectionString"),
    "myFooTopic",
    "Property1Equal10");
BrokeredMessage message = subscriptionClient.Receive();
Sending the message to our subscribers:
BrokeredMessage message = new BrokeredMessage(item);
AddCustomProperties(message,item);
topicClient.Send(message);
In this way we will be able to define filters over the subscription using our custom properties. The downside of this solution is the way we het the list of properties that are decorated with our custom attribute – using reflection. Reflection is a very expensive mechanism. Because our machines and servers are very fast, based on the case, this may or not may be a problem.

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…