Skip to main content

More about 'OnMessage' method of Service Bus

If you are using Service Bus, you should know that you have the ability to be notified when a message is available in the queue or in a subscription. Using this feature you don’t need to call the "Receive" method and catch the timeout exception when there are no new messages are available.
OnMessageOptions messageOptions = new OnMessageOptions()
  {
    AutoComplete = true,
    ExceptionReceived += LogErrorsOnMessageReceived
  };
queueClient.OnMessage((message) => 
  {
                ...
  }, messageOptions);
As you can see, you register to an event that will be called when a new message is available. If there are more than 1 messages available the event will be triggered more than one. Each notification will run on a different thread. To be able to control the number of concurrent messages that can be received by a client you should change the value of "MaxConcurrentCalls".
OnMessageOptions messageOptions = new OnMessageOptions()
  {
    AutoComplete = true,
    ExceptionReceived += LogErrorsOnMessageReceived
    MaxConcurrentCalls = 5
  };
queueClient.OnMessage((message) => 
  {
                ...
  }, messageOptions);
But what about the costs?
When using Service Bus, you pay for each transaction. Because of this each call of “Receive” method will consume a transaction. Even if there are no available messages and the call finish with a timeout exception, you will have to pay for one transaction.
Base on this, we should know that a similar thing is happening when we are using “OnMessage”. The behind implementation of this method use “Reveive” method and has a timeout value. Because of this, even if you register to “OnMessage” once you will notify that more than one transactions are consumed even if you don’t have messages in the Service Bus.
This is happening because “OnMessage” calls “Receive” method that will consume a transaction when a message is available or when a timeout exception occurs. The default timeout value is 60 seconds. This timeout value can be easily change using MessagingFactory.
From the cost perceptive you should not have any kind of problems. The cost of each client that use “OnMessage” method and has the timeout value set to 60 seconds is 4.3 cents per month.
In conclusion “OnMessage” is very useful because offer out of the box a mechanism that give us the possibility to be notified when a message is available – until now we had to implement this mechanism every time. You should not forget that this mechanism will handle each new message on a different thread and there are times when you want to control the concurrent level.  Also it generates some costs, but the cost is similar with the “Receive” mechanism – in the end “OnMessage” use “Receive”.

Comments

  1. Do you have any guidance for choosing a value for MaxConcurrentCalls?

    ReplyDelete
  2. A magic number don't exists. Depends on what kind of action you are executing when a new message is available. For example if the action will call the DB that you may be limited of the number of connection that you can have to DB. On the other hand if you have an action that is CPU intensive that you will not be able to run in parallel to many tasks.
    Based on my experience this value should be around 5 or 10.

    ReplyDelete
  3. But how to gracefull shutdown/stop a message pump!?

    ReplyDelete
    Replies
    1. There is now yet a gracefull mechanism. You can cal 'close' method, but odd behavior can appear.

      Delete
  4. whats the default value of MaxConcurrentCalls property?

    ReplyDelete

Post a Comment

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…