Skip to main content

Create a gSharp Project

Creating a gSharp project is done in Visual Studio (version 2022 or later) using the installed templates (see gSharp Installation). The template creates a Visual Studio solution containing two projects:

  • The calculation project – The project where the calculation is defined.
  • A testbed project – A console app that acts as a pseudo gSharp server and runs the calculation for debugging.

Project Creation Steps​

Create a project from a gSharp template​

Start Visual Studio and select Create a new project. This opens the Visual Studio template selection page, which contains a list of available templates. The gSharp templates will be in the list, but not necessarily at the top. To easily find them, select gSharp from the Product Types dropdown menu. This displays the available gSharp templates:

Visual Studio Create a new project dialog

Select a gSharp Solution Project​

Select one of the gSharp Solution (Xxx) templates to create a new calculation assembly from scratch. Be sure not to select a gSharp Project (Xxx) template, as those templates do not include a testbed. If you're migrating PI-ACE code, select the gSharp Assembly (PI-ACE) project—it is designed to be source-code compatible with the PI-ACE 2.0 libraries. For the purposes of this guide, we use the C# template.

Click the Next button to go to the Configure your new project page of the wizard.

Visual Studio Create a new project dialog

This page specifies what your overall .NET solution is going to be called and where it will be written to. This solution can contain multiple assembly projects.

Type the project name GSharpDemo for your new solution. Ensure that the Create in new folder check box is selected.

Click the Next button to go to the Additional information page.

Visual Studio Create a new project dialog

On this page, specify the assembly name. Type GSharpTankDemo in the Assembly Name field. In the Calculation Module Name field, type TankVolume.

Click the Create button and the new solution will be generated and loaded.

Project Overview​

With the project loaded into Visual Studio, it is ready for some initial setup. The result of the template is a Visual Studio solution with two projects:

  • The gSharp assembly to be built (in this case, GSharpTankDemo).
  • The gSharp testbed project GSharp.Testbed.

Visual Studio solution explorer showing the assembly projects

The GSharp.Testbed project is the default startup project. It's a console application, provided as a convenient means to debug and test gSharp calculation assemblies. When started, this application uses some of the core parts of the gSharp server to run the modules in the calculation project. The GSharp.Testbed project contains a project reference to the gSharp assembly project.

Project Files​

Of the files created by the template, those of primary interest are:

  • GSharpTankDemo/TankVolume.cs – The main calculation class, named as you entered it in the template wizard.
  • GSharp.Testbed/Program.cs – The configuration of the testbed.

TankVolume.cs​

This file contains the basic setup for a calculation module (comments and using directives removed for brevity):

namespace GSharpTankDemo;

public class TankVolume : CalculationModuleAFBase
{
[PointDataDirectionInput]
public AFAttrDoublePoint MyPoint1 { get; set; }

[PointDataDirectionOutput]
public AFAttrDoublePoint MyPoint2 { get; set; }


public override void Initialize()
{
}

public override void Execute(ISchedule schedule, DateTime timestamp, CancellationToken cancelToken)
{
Log.Info("MsgCode1", "Adding 1 to {PointName}.", MyPoint1.Name);
double point1Value = MyPoint1.Value;
MyPoint2.Value = point1Value + 1;
}

public override void Dispose()
{
}
}

Program.cs​

This file contains the configuration for the testbed, which we will use to debug and test your calculation. This class contains the Main() entry point method of the testbed app, but that method is located in a separate file. Program is responsible for setting up the assembly test host, and the entry point is separated to reduce noise.

namespace GSharp.Testbed;

internal partial class Program
{
private void ConfigureAssembly(AssemblyBuilder builder)
{
builder.AddModule<GSharpTankDemo.TankVolume>(moduleBuilder => moduleBuilder
.ConfigurePoint(nameof(GSharpTankDemo.TankVolume.MyPoint1), "Attribute1")
.ConfigurePoint(nameof(GSharpTankDemo.TankVolume.MyPoint2), "Attribute2")
.AddContext("TankVolume Test Context", @"\\AFServer\AFDatabase\Parent\Element 001")
);
}

private void ConfigureSettings(AssemblyTestHost.TestHostSettings settings)
{
LogLevelsSettings logLevelSettings = settings.Logging.Levels;
logLevelSettings.ApplyToAll(LogLevel.Info);
}
}

Build​

Build the solution. This produces two assemblies, GSharp.Testbed.exe and GSharpTankDemo.dll, which you will find in the bin/Debug folder of the Testbed project. The Output window in Visual Studio should show the results of the build, much the same as this:

Visual Studio Output window showing the build log

note

Because we haven't done any configuration, the solution will build and run, but it will produce errors. Only when the configuration is complete will the testbed be able to perform its proper function.

First-build warning (Visual Studio 2026)

Immediately building a newly created solution may result in transient build errors such as:

NuGet package restore failed. Please see Error List window for detailed warnings and errors.
Error occurred while restoring NuGet packages: The operation failed as details for project GSharp.Testbed could not be loaded.

This behavior occurs in Visual Studio 2026 when building a solution immediately after creating it from a template. This is not a problem with the generated project or its configuration, it is a problem within Visual Studio itself. In this state, some Visual Studio features may also fail to load correctly (for example, IntelliSense, GitHub Copilot, etc.).

Workaround: Restart Visual Studio after creating the solution. The solution will then build cleanly and all features will function as expected.

Artifacts​

Notice that the build log in the Output panel of Visual Studio has the following line:

1> Artifacts copied to file:///D:\GSharpAssemblies\GSharpDemo\Artifacts\GSharpTankDemo

When your assembly has finished building, it copies the relevant files from the build folder (bin\Debug) to this folder. The file path displayed in the build log window is a hyperlink, so you can Ctrl-click it to open the folder in File Explorer where your assembly is. That folder will contain your GSharpTankDemo.dll assembly and its GSharpTankDemo.pdb file (debug symbols).

Windows Explorer showing the assembly&#39;s Artifacts folder

Dependency Files​

At some future point, your assemblies may have dependency assemblies (such as supporting assemblies you have written, or packages installed from NuGet), and those dependancies are automatically copied to the Artifacts folder as well. This is possible because .NET can discover an assembly's dependancies by interrogating it.

Some dependencies, however, are not discoverable by .NET and will not automatically be copied to the Artifacts folder. These include native dlls, some .NET assemblies (in rare cases) and other non assembly files such as text files and image files. To include these files your assembly needs to define information about them so that gSharp can discover them.

This is done using a special AssemblyExternalResourceFile attribute. Add an AssemblyInfo.cs file to your GSharpTankDemo project and add the attribute to it, as required:

using GHost.GSharp.Core.Attributes;

[assembly: AssemblyExternalResourceFile("TankData.json")]
[assembly: AssemblyExternalResourceFile("AnotherFile.txt", "AndAnother.jpg")]

Note the following points about this attribute:

  • As with all assembly-level attributes these must be placed in the AssemblyInfo.cs file above all other code except namespace declarations. Common practice would be to only put attributes in this file (hence the name).
  • Multiple AssemblyExternalResourceFile attributes can be used to specify multiple files.
  • A single AssemblyExternalResourceFile attribute can specify multiple files.
  • If a file is in a subfolder of your project the relative path must be specified with the file name. See below for more information.

Dependency File Subfolders​

If your project defines folders for your dependency files those folders must be included in the AssemblyExternalResourceFile attribute. For example, if your project structure was:

D:\GSharpAssemblies
└─ GSharpDemo
├─ GSharp.Testbed
│ ├─ Program.cs
│ └─ ...
└─ GSharpTankDemo
├─ AssemblyInfo.cs
├─ TankVolume.cs
└─ Resources
├─ Data.json
└─ Images
└─ Image1.png

your AssemblyInfo.cs file would look something like:

using GHost.GSharp.Core.Attributes;

[assembly: AssemblyExternalResourceFile("Resources\\Data.json")]
[assembly: AssemblyExternalResourceFile("Resources\\Images\\Image1.jpg")]

After a successful build the Artifacts folder would have the same structure as your project:

D:\GSharpAssemblies
└─ GSharpDemo
├─ GSharp.Testbed
│ └─ ...
├─ GSharpTankDemo
│ └─ ...
└─ Artifacts
└─ GSharpTankDemo
├─ GSharpTankDemo.dll
├─ GSharpTankDemo.pdb
└─ Resources
├─ Data.json
└─ Images
└─ Image1.png

When you get to the stage of registering your assembly the same folder structure will be preserved, meaning that relative file references to these files would still be valid. For example,

string dataJson = File.ReadAllText("Resources\\Data.json");
byte[] imageData = File.ReadAllBytes("Resources\\Images\\Image1.png");