Use Static Functions to Avoid Atomic Scope in Orchestration





4.00/5 (2 votes)
Use static functions to avoid atomic scope in orchestration
Introduction
Often, people ask me how to call a .NET class within an orchestration if it is not serializable. The probable answer for this question is to call the class within an atomic scope because within atomic scope, there is no persistence point. Although using atomic scope, there are some pros and cons. But the major issue with atomic scopes is that sometimes it can be very expensive in terms of performance and you should use them with caution.
While working with business scenario, there are some needs where a set of transactions can either be success as a whole or fail. In that case, using atomic scope is the best approach. But, for logging or getting some config values, we generally create class marking it as serializable to use it within an orchestration or non-serializable to use it within atomic shape. For each case, we are creating unnecessary objects and for large number of orchestration instances, this can cause serious performance issue. While calling static
members within a static
class, only one single instance gets created. Also, call to a static
method generates a call instruction in Microsoft Intermediate Language (MSIL), whereas a call to an instance method generates a callvirt
instruction, which also checks for a null
object references. However, most of the time, the performance difference between the two is not significant but for large orchestration instances, this matters.
Background
Check this link: Description about Static Classes and Static Members.
Problem This Solution Solves and How It Is Useful
The solution helps to identify unnecessary creation of atomic scope and class instances. Also, it is useful for increasing the overall performance of the solution.
Using the Code
To understand this concept, let us create a static
class as mentioned in code section and call it from BizTalk orchestration. The code creates a static
function Add
which accepts two int
parameters and returns the addition of two parameters.
using System;
namespace StaticFunctions
{
public static class StaticClass
{
public static int Add(int a, int b)
{
return a + b;
}
}
}
After creating the project, build it and GAC it using gacutil
command in Visual Studio command prompt.
Code Description and How It Works
In this orchestration, you don't have to create StaticClass
instance. You can directly call the Add
function by using:
msgOutput.Value = StaticFunctions.StaticClass.Add(msgInput.Param1,msgInput.Param2);
Using this approach, you can avoid unnecessary use of atomic scope within an orchestration.
Conclusion
Before defining classes in external assembly, first investigate the purpose of the class thoroughly and act accordingly.