Debug the Assembly
With our TankVolume class defined and our asset store configured, we're now ready to debug our calculation.
The gSharp Visual Studio template includes a project named Testbed. This testbed is a console application that closely simulates the behavior of the gSharp server and provides a convenient means of debugging the calculation module class. Because the calculation is being run in Visual Studio you get all the benefits of that environment, such as setting breakpoints, stepping through code, live updates, etc.
The Testbed connects to the data provider (PI Asset Framework, Canary, etc.) to retrieve current values from each of its points and therefore requires the same level of access as the gSharp server does. The nature of the Testbed is that it doesn't require a gSharp server, allowing development to be done in isolation.
For safety reasons, the Testbed application only simulates sending data to the data provider; it does not transmit any data. Instead, it logs to the console what would have been sent if the calculation was running in gSharp.
Testbed
The TankVolume calculation class is ready to be built, but it can't be tested yet because the Testbed needs a context to map to, and it won't (can't) connect to gSharp to obtain one. More to the point, there is no way to create a context in gSharp until the assembly is registered, which has not happened yet.
Instead, we will configure the testbed application to simulate a gSharp context for us. Open the Program.cs file of the GSharp.Testbed project. The content should look similar to this:
- PI Asset Framework
- Canary Asset Hierarchy
namespace GSharp.Testbed;
internal partial class Program
{
private void ConfigureAssembly(AssemblyBuilder builder)
{
builder.AddModule<TankVolume>(moduleBuilder => moduleBuilder
.ConfigurePoint(nameof(TankVolume.Diameter), "Attribute1")
.ConfigurePoint(nameof(TankVolume.Volume), "Attribute2")
.AddContext("TankVolume Test Context", @"\\AFServer\AFDatabase\Parent\Element 001")
);
}
private void ConfigureSettings(TestHostSettings settings)
{
LogLevelsSettings logLevelSettings = settings.Logging.Levels;
logLevelSettings.ApplyToAll(LogLevel.Info);
}
}
namespace GSharp.Testbed;
internal partial class Program
{
private void ConfigureAssembly(AssemblyBuilder builder)
{
builder.AddModule<TankVolume>(moduleBuilder => moduleBuilder
.ConfigurePoint(nameof(TankVolume.Diameter), "Tag1")
.ConfigurePoint(nameof(TankVolume.Volume), "Tag2")
.AddContext("TankVolume Test Context", "ViewA.ViewB.ViewC.Asset001")
);
}
private void ConfigureSettings(TestHostSettings settings)
{
LogLevelsSettings logLevelSettings = settings.Logging.Levels;
logLevelSettings.ApplyToAll(LogLevel.Info);
}
}
The testbed uses the standard Program class similar to a typical console application, except that the Main() method instantiates an instance of Program rather than relying solely on static methods. The Main() method and most of its supporting code are located in a separate file (Program.init.cs) to keep this file as clean as possible. There should be no reason to edit other files.
The ConfigureAssembly() Function
Because the testbed doesn't connect to a gSharp server it needs to be manually configured with the details of a context and its points. Recall that our contexts set up previously (Configure the Data Store) are Tank_000 and Tank_001.
- PI Asset Framework
- Canary Asset Hierarchy


This ConfigureAssembly() method is passed an assembly builder which is used for adding a module, configuring its virtual points, and setting up a virtual context to test against. This is all done using the AddModule function.
builder.AddModule<TankVolume>(moduleBuilder => moduleBuilder
//...
);
Specifying <TankVolume> as the generic parameter tells the assembly builder which module is being tested. The function then creates a module builder for that type and passes it to the caller via the Action<ModuleBuilder> lambda function passed in.
The module builder is used to configure the module's points.
- PI Asset Framework
- Canary Asset Hierarchy
.ConfigurePoint(nameof(TankVolume.Diameter), "Attribute1")
- The first argument tells the builder the name of the point that is being configured. The name is important here because it tells the testbed which of your properties to bind your point configuration to.
- The second argument specifies the relative path of the AF attribute that the point is to be mapped to. Change this to the relative path of the attribute, "Diameter".
- Similarly, change the AF attribute path of the
TankVolume.Volumepoint to "Volume". - Finally, the gSharp Visual Studio template originally set up only two points and the
TankVolumemodule now has a newLevelpoint. Add a new call toConfigurePoint()to configure theTankVolume.Levelpoint and pass in "Level" as the AF attribute path.
Now that the module builder's points are configured we need to add a gSharp context that will be used for testing. This is done using the AddContext method:
.AddContext("ContextName1", @"\\AFServer\AFDatabase\Parent\Element 001")
- The first argument specifies the name of the context. This is arbitrary but required, because all contexts require a name. Change it to something that will be helpful when displayed in the log, such as "Test Tank 000".
- The second argument specifies the full path of the AF element that the context is being mapped to. The gSharp Visual Studio template originally set this to the full path of the unlikely \\AFServer\AFDatabase\Parent\Element 001 AF element to indicate what is required here. Change it to the full path of the AF element that is going to be used for testing, such as "\\SVRPI01\Tank Demo\Tanks\Tank 000".
.AddContext("Test Tank 000", @"\\SVRPI01\Tank Demo\Tanks\Tank 000")
.ConfigurePoint(nameof(TankVolume.Diameter), "Tag1")
- The first argument tells the builder the name of the point that is being configured. The name is important here because it tells the testbed which of your properties to bind your point configuration to.
- The second argument specifies the relative Canary asset tag that the point is to be mapped to. Change this to the relative asset tag of the attribute, "Diameter".
- Similarly, change the relative asset tag of the
TankVolume.Volumeto "Volume". - Finally, the gSharp Visual Studio template originally set up only two points and the
TankVolumemodule now has a newLevelpoint. Add a new call toConfigurePoint()to configure theTankVolume.Levelpoint and pass in "Level" as the relative asset tag.
Now that the module builder's points are configured we need to add a gSharp context that will be used for testing. This is done using the AddContext method:
.AddContext("ContextName1", "ViewA.ViewB.ViewC.Asset001")
- The first argument specifies the name of the context. This is arbitrary but required, because all contexts require a name. Change it to something that will be helpful when displayed in the log, such as "Test Tank 000".
- The second argument specifies the full path of the Canary asset that the context is being mapped to. The gSharp Visual Studio template originally set this to the full path of the unlikely ViewA.ViewB.ViewC.Asset001 asset to indicate what is required here. Change it to the full path of the asset that is going to be used for testing, such as "Tank Demo.GSharp.Tank Farm.Tank 000".
.AddContext("Test Tank 000", "VTank Demo.GSharp.Tank Farm.Tank 000")
The point configuration and context in this method are virtual and temporary, in that they resemble a real set of points and a context but are not connected to a gSharp server in any way, and their lifetimes are constrained to the execution of the testbed app.
The ConfigureSettings() Function
This method offers an opportunity to configure settings by passing in the debugging host's settings object. In the current version the only settings that can be configured are minimum logging levels.
The call to logLevelSettings.ApplyToAll(LogLevel.Info) tells the debugging host that the minimum log level to be logged for all logs is the LogLevel.Info level. The logLevelSettings object actually has a hierarchy of levels that can be set individually, but for now we'll just use the ApplyToAll() method.
If you were to change the Log.Info(...) call in the module's Execute method to Log.Debug(...), you would need to change the call to the ApplyToAll() method in order for the log entry to be generated:
logLevelSettings.ApplyToAll(LogLevel.Debug);
Program.cs
At this stage the Program class should look something like the code below and the whole solution should compile successfully.
- PI Asset Framework
- Canary Asset Hierarchy
internal partial class Program
{
private void ConfigureAssembly(AssemblyBuilder builder)
{
builder.AddModule<TankVolume>(moduleBuilder => moduleBuilder
.ConfigurePoint(nameof(GSharpTankDemo.TankVolume.Diameter), "Diameter_cnst")
.ConfigurePoint(nameof(GSharpTankDemo.TankVolume.Level), "Level_meas")
.ConfigurePoint(nameof(GSharpTankDemo.TankVolume.Volume), "Volume_calc")
.AddContext("Test Tank 000", @"\\SVRAF01\gSharp Demo\Tank Farm\Tank_000")
);
}
private void ConfigureSettings(TestHostSettings settings)
{
LogLevelsSettings logLevelSettings = settings.Logging.Levels;
logLevelSettings.ApplyToAll(LogLevel.Info);
}
}
internal partial class Program
{
private void ConfigureAssembly(AssemblyBuilder builder)
{
builder.AddModule<TankVolume>(moduleBuilder => moduleBuilder
.ConfigurePoint(nameof(GSharpTankDemo.TankVolume.Diameter), "Diameter_cnst")
.ConfigurePoint(nameof(GSharpTankDemo.TankVolume.Level), "Level_meas")
.ConfigurePoint(nameof(GSharpTankDemo.TankVolume.Volume), "Volume_calc")
.AddContext("Test Tank 000", "gSharp Demo.Tank Farm.Tank_000")
);
}
private void ConfigureSettings(TestHostSettings settings)
{
LogLevelsSettings logLevelSettings = settings.Logging.Levels;
logLevelSettings.ApplyToAll(LogLevel.Info);
}
}
Execution Process
The static Main method will start the debugging host. The debugging host will behave much like the gSharp service by doing the following:
- Create an instance of the
TankVolumemodule. - Assign new point instances to the module's
Diameter,Level, andVolumeproperties. - Call the module's
Initializemethod. - Call the module's
Executemethod with arguments:schedule- This would normally represent the schedule that caused the execution to occur, but in the testbed it is a special schedule with aScheduleTypeofScheduleType.AdHoc.timestamp- This is the time that the host calls theExecutemethod.cancelToken- This is provided for long-running calculations to periodically check if a cancellation has been requested. See the .NET documentation CancellationToken Struct for more information about cancellation tokens.
- Call the module's
Disposemethod. - Shut down.
In the gSharp server the Initialize method is called once when a context is first activated; however, in the Testbed app it is called each time the debugging host runs. Likewise, the Dispose method is called each time in the testbed, but only once in gSharp.
Debugging the Module
Now that the module is defined and the testbed host configured, it can be executed for testing and debugging.
Place a breakpoint at the beginning of the Execute method of the TankVolume module and start the testbed in debug mode in the normal way by clicking the Start button in Visual Studio. A console will open and the testbed will run the calculation and stop at the breakpoint. Normal debugging can be done from this point.
When the execution of the test has completed the console should look something similar to:
- PI Asset Framework
- Canary Asset Hierarchy


Some things to note about the output of the testbed:
Logging
As previously described, add a line of code in the TankVolume.Execute() method:
Log.Info("CalcMsg", $"Calculated the volume of tank '{Context.ContextName}' with a level of {Level.ValueToString()} to be {Volume.ValueToString()}");
This call to Log.Info() produces output in the console similar to:
- PI Asset Framework
- Canary Asset Hierarchy
14:47:12 [INFO] Calculated the volume of tank 'Test Tank 000' with a level of 86.557 cm to be 2202609.7 cm3
14:47:12 [INFO] Calculated the volume of tank 'Test Tank 000' with a level of 86.557099 to be 2202609.6817338
Logging can be done at any time and in any method in the module, except for the constructor. Because the constructor is the very first method to be called in a class, neither the gSharp server nor the debugging host has had an opportunity to assign the Log property. By the time the Initialize() method is called the Log property has been assigned a logger and logging can begin.
Calculation Output Data
The output in the console shows something similar to:
- PI Asset Framework
- Canary Asset Hierarchy
14:47:12 [INFO] Output 'Volume': [11/07/2025 14:47:10] 2202609.7 cm3
14:47:12 [INFO] Output 'Volume': [11/07/2025 14:47:10] 2202609.6817338
Although the input data is real data from AF, the output data is only an indicator of what would be sent back to AF when the execution is complete. However, no value is actually written.