Invoking Code Source File
Use this workflow to invoke a coded source file within a low-code workflow. In this example, we create a class called TimeSpanHelper in the TimeSpanHelper.cs coded source file. This class generates a random time span based on the specified bounds. To integrate this class in a low-code workflow, we use an Invoke Workflow File activity, followed by other low-code activities, depending on the use case.
- Create a coded source file. For this example, name it
TimeSpanHelper.cs. - In the coded source file, create a public class named TimeSpanHelper.
- Inside the TimeSpanHelper class, declare a private static Random object named
_randomto generate random values.private static Random _random = new Random(); - Declare a private static object named
_lockObjto secure the thread, while working with multiple threads at the same time.private static object _lockObj = new object(); - Implement a public static method called
GetRandomTimeSpanBetweenthat takes two integer parameters:lowerBoundMsandupperBoundMs.public static TimeSpan GetRandomTimeSpanBetween(int lowerBoundMs, int upperBoundMs)
{ - Use the lock statement with
_lockObjto secure this thread, while working with multiple threads at the same time.lock (_lockObj)
{ - Within the lock block, generate a random integer value using
_random.Nextand passlowerBoundMsandupperBoundMsas the arguments.var ms = _random.Next(lowerBoundMs, upperBoundMs); - Convert the generated random value to a
TimeSpanobject usingTimeSpan.FromMillisecondsand return this value directly from the method with the following command return:return TimeSpan.FromMilliseconds(ms);
- Inside the TimeSpanHelper class, declare a private static Random object named
Based on these steps, your final code should look like this:
public class TimeSpanHelper
{
private static Random _random = new Random();
private static object _lockObj = new object();
public static TimeSpan GetRandomTimeSpanBetween(int lowerBoundMs, int upperBoundMs)
{
lock (_lockObj)
{
var ms = _random.Next(lowerBoundMs, upperBoundMs);
return TimeSpan.FromMilliseconds(ms);
}
}
}
- Create a low-code workflow. For this example, name it
WorkflowUsingCodeSourceFile. - Add a Log Message activity to output a random timespan between two and four seconds.
- In the Message field, type the name of the project, call the coded source file, and then call the
GetRandomTimeSpanBetweenmethod.
In the example below, CodedWorkflowInteroperability is the name of the project.
CodedWorkflowInteroperability.TimeSpanHelper.GetRandomTimeSpanBetween(2000, 4000)