From time to time we hear from you several excuses for not jumping into software development with PI data. Today we will discuss the top two that I get and how to fix them with a very simple solution. Here they are:

 

  1. I must be an administrator to install development applications;
  2. The tools required for developing applications in a commercial environment are expensive;

 

1. Free IDEs and Licensing

 

Let's start by talking about the cost of applications used for software development. Yes, usually they are expensive, but there are pretty good free alternatives. The problem is that you have to be really careful with the license that it uses and what are your responsibilities. For instance, the first version of Eclipse's EPL was so vague that auto-generated code could be interpreted as derivative work and could make it mandatory for the developer to open the source code. Since EPL2 they have fixed it and Eclipse is now business friendly.

 

Now, if you want to stick with Microsoft tools, there are two free alternatives. The first one is Visual Studio Community, a slim version of the full-blown Visual Studio. But it has a proprietary license that it's not suitable for enterprise as it states:

If you are an enterprise, your employees and contractors may not use the software to develop or test your applications, except for: (i ) open source; (ii) Visual Studio extensions; (iii) device drivers for the Windows operating system; and, (iv) education purposes as permitted above.

 

So if you are writing a tool to process your corporate data, it's a no-go. This pushes us to the other alternative, VS Code, a free, yet very powerful, community-driven IDE that's suitable for enterprise development. It uses a simple license that allows you to use the application for commercial purposes and in an enterprise environment. As clearly stated on item 1.a: "you may use any number of copies of the software to develop and test your applications, including deployment within your internal corporate network".

 

2. You don't need to be an Admin

 

This item is really easy to fix as Microsoft offers a standalone version of VS Code. You just have to download the .zip version from here and run the executable. If you are not able to execute the file, the company you work for may be blocking and, in this case, you should talk to your manager or IT departament.

 

Well, what now? Is it that simple? Unfortunately no. Due to the increasing participation of Microsoft in the open-source world, several of its products are now targeting the open-source audience. More specifically for the .NET platform, Microsoft has created the .NET Foundation to handle the usage of the .NET products under OSI (Open Source Initiative, not OSIsoft) rules. Because of this, VS Code is designed to work natively with .NET Core and not the standard .NET Framework. So, in order to use AF SDK, you need some tweaks. But fear not, they are really simple!

 

3. Adding the C# Extension to VS Code

 

In this step, we will install the C# Extension that allows the IDE to process .cs and .csproj files. It's very simple as you just have to go to the Extensions tab (highlighted below or just press control + shif + x), type "C# for Visual Studio Code" and hit install.

 

 

4. Creating a .NET Standard project in VS Code

 

First, we start VS Code and go to File -> Open Folder so we can select a folder that will be the base of our demo project. Once you see the welcome screen, go to View -> Terminal so you can use the integrated terminal to use the .NET CLI to create a new .NET project. Because we will create a console application, we must type

 

PS C:\Users\rafael\afdemo> dotnet new console

 

If you really need to work with VB.NET you can append "-lang VB" at the end. (but please, don't )

 

You should end up with a structure similar to this:

 

 

If by any chance, your structure misses the .vscode folder, press control+shift+B to compile it and VS Code will ask if you want to add a default build action. Just say yes.

 

5. Converting from .NET Core to .NET Standard

 

Now that we have our project, we must change it to .NET Standard, so we can later reference the AF SDK assembly. We have to change two files. The first one is the .csproj file. You have to change from this:

 

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>

    <OutputType>Exe</OutputType>

    <TargetFramework>netcoreapp2.2</TargetFramework>

  </PropertyGroup>

</Project>

 

To this:

 

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>

    <OutputType>Exe</OutputType>

    <TargetFramework>net472</TargetFramework>

    <PlatformTarget>x64</PlatformTarget>

    <DebugType>net472</DebugType>

  </PropertyGroup>

</Project>

 

Here we see the first caveat of this approach: the application must be 64 bits. Also, note that I'm explicitly referencing the .NET Framework version that I want to work with. The list of available frameworks (as well as how to write it on the XML file) can be found here.

 

Now we move on the recently created launch.json inside the .vscode folder. If you pay close attention, you will see that it references a type "coreclr". You have to change it to type "clr". Also, the path must match the references framework version. So, if every goes as planned, you will change from this:

 

{

  "version":"0.2.0",

  "configurations":[

  {

    "name":".NET Core Launch (console)",

    "type":"coreclr",

    "request":"launch",

    "preLaunchTask":"build",

    "program":"${workspaceFolder}/bin/Debug/netcoreapp2.1/afdemo2.dll",

    "args":[],

    "cwd":"${workspaceFolder}",

    "console":"internalConsole",

    "stopAtEntry":false,

    "internalConsoleOptions":"openOnSessionStart"

  },

  {

    "name":".NET Core Attach",

    "type":"coreclr",

    "request":"attach",

    "processId":"${command:pickProcess}"

  }

 ]

}

 

To this:

{

  "version":"0.2.0",

  "configurations":[

  {

    "name":".NET Standard Launch (console)",

    "type":"clr",

    "request":"launch",

    "preLaunchTask":"build",

    "program":"${workspaceFolder}/bin/Debug/net472/afdemo.exe",

    "args":[],

    "cwd":"${workspaceFolder}",

    "console":"internalConsole",

    "stopAtEntry":false,

    "internalConsoleOptions":"openOnSessionStart"

  },

{

  "name":".NET Standard Attach",

  "type":"clr",

  "request":"attach",

  "processId":"${command:pickProcess}"

  }

 ]

}

 

And that's it! If everything went all right, you can now test your Hello World and see if it's working as it should be.

 

6. Referencing OSIsoft.AFSDK.dll

 

Now, in order to get PI Data into your project, you just have to add the reference to the AF SDK assembly. This is just a matter of changing your .csproj file and manually including the reference by adding the <ItemGroup> element to your <Project>. Also, it's important to mention that HintPath can also handle relative paths. I'm hardcoding the full path here so the unobservant copypaster don't break the code:

 

<Project Sdk="Microsoft.NET.Sdk">

  <ItemGroup>

    <Reference Include="OSIsoft.AFSDK, Version=4.0.0.0, Culture=neutral, PublicKeyToken=6238be57836698e6, processorArchitecture=MSIL">

      <SpecificVersion>False</SpecificVersion>

      <HintPath>C:\Program Files (x86)\PIPC\AF\PublicAssemblies\4.0\OSIsoft.AFSDK.dll</HintPath>

    </Reference>

  </ItemGroup>

</Project>

 

7. Code Time

 

Now let's write a simple code that lists all PI Systems and their respective AF Databases

 

using System;

using OSIsoft.AF;

using OSIsoft.AF.Asset;

using OSIsoft.AF.Data;

using OSIsoft.AF.PI;

using OSIsoft.AF.Time;

 

namespace afdemo

{

  class Program

  {

    static void Main(string[] args)

    {

      Console.WriteLine("This is a VS Code Project!");

      PISystems piSystems = new PISystems();

     foreach (var piSystem in piSystems)

     {

        Console.WriteLine(piSystem.Name);

        foreach (var database in piSystem.Databases)

        {

          Console.WriteLine(" ->" + database.Name);

        }

      }

    }

  }

}

 

You can build the code by pressing Control+Shift+B (Visual Studio users, rejoice!) and debug it by pressing F5. Now you can easily debug your code on the same way you do on the full-blown version of Visual Studio!

 

 

You can also execute the code by typing dotnet run on your integrated console. Here the output for this specific code on my machine:

 

 

8. Conclusion

 

By using VS Code you have no more excuses to not jump headfirst into developing custom tools that can make your workflow simpler and easier. Oh, and one more thing: VS Code is suitable for a myriad of languages, including web development. So you can also use it to create PI Vision Custom Symbols (leave a comment if you would like a guide on how to configure an environment for PI Vision Custom Symbols development).