Please ensure Javascript is enabled for purposes of website accessibility
Powered by Zoomin Software. For more details please contactZoomin

AF SDK Reference

Connecting to a PI AF Server

  • Last UpdatedJan 12, 2026
  • 5 minute read
Connecting to a PI AF Server

This topic contains the following sections:

There are several methods for connecting to a PI AF Server. The AF SDK will handle caching objects and connections to the server, therefore you should only close a connection when you are through using the server for a long period of time. This is different from typical programming against a database where you open a connect, get the data, and then close the connection.

When programming in a multi-user environment where the user identity could change between calls, you must not re-use any AF SDK objects between calls and get new objects starting with the PISystems collection each time. The AF SDK maintains a cache of objects and connections per user which is determined by the obtained PISystems collection for the current user.

Implicit Connection

This example demonstrates the method for connecting to an AF server implicitly.

1// Get the PISystem once and share within the application.
2if (staticPISystem == null)
3    staticPISystem = new PISystems().DefaultPISystem;
4
5// Do operation, a connection will automatically be created if needed for this user.
6AFValue myValue = null;
7AFAttribute myAttribute = AFAttribute.FindAttribute(@"MyDatabase\Element#1|Attribute#1", staticPISystem);
8if (myAttribute != null)
9    myValue = myAttribute.GetValue();

Implicit Connection Using OpenId

This example demonstrates the method for connecting to an AF server implicitly using openId connections.

 1ClaimsIdentity testAccountIdentity = new ClaimsIdentity();
 2testAccountIdentity = new ClaimsIdentity();
 3testAccountIdentity.AddClaim(new Claim("id", TestAccountName));
 4testAccountIdentity.AddClaim(new Claim("access_token", TestAccountAccessToken));
 5
 6PISystems myPISystems = new PISystems(testAccountIdentity);
 7
 8PISystem piSystem = myPISystems.DefaultPISystem;
 9string piSystemName = piSystem.ConnectionInfo.Host;
10
11// implicit connection using Claims Identity specified by PISystems constructor
12AFDatabase afdb = piSystem.Databases[myAFDatabase];
13if (afdb is null)
14    afdb = piSystem.Databases.Add(myAFDatabase);
15AFElement element = afdb.Elements[myAFElement];
16if (element is null)
17    element = afdb.Elements.Add(myAFElement);
18
19Console.WriteLine("CurrentUserName = {0}", piSystem.CurrentUserName);
20Console.WriteLine("AuthenticationType = {0}", piSystem.ConnectionInfo.AuthenticationType);
21Console.WriteLine();
22
23piSystem.Disconnect();
24
25// implicit connection using Claims Identity specified by PISystems constructor
26string ElementPath = $@"\\{piSystemName}\{myAFDatabase}\{myAFElement}";
27element = AFElement.FindElementsByPath(new[] { ElementPath }, null)[ElementPath];
28Console.WriteLine("CurrentUserName = {0}", piSystem.CurrentUserName);
29Console.WriteLine("AuthenticationType = {0}", piSystem.ConnectionInfo.AuthenticationType);
30Console.WriteLine();

Explicit Connection

This example demonstrates the normal method for connecting to an AF server explicitly.

 1// Get the PISystems collection for the current user and default PISystem.
 2PISystem myPISystem = new PISystems().DefaultPISystem;
 3
 4// Simple connect.
 5myPISystem.Connect();
 6myPISystem.Disconnect();
 7
 8// Connect and display a credential prompt dialog if current user login fails.
 9// Only available in .Net Framework AFSDK
10// myPISystem.Connect(true, null);
11// myPISystem.Disconnect();
12
13try
14{
15    // Connect using a specified credential.
16    NetworkCredential credential = new NetworkCredential("guest", String.Empty);
17    myPISystem.Connect(credential);
18}
19catch (Exception ex)
20{
21    // Expected exception since credential needs a valid user name and password.
22    Console.WriteLine(ex.Message);
23}

Explicit Connection Using OpenId

This example demonstrates the method for connecting to an AF server explicitly using openId connections.

 1PISystems myPISystems = new PISystems();
 2PISystem piSystem = myPISystems.DefaultPISystem;
 3
 4// explicit connection using access token 
 5piSystem.Connect(TestAccountAccessToken);
 6Console.WriteLine("CurrentUserName = {0}", piSystem.CurrentUserName);
 7Console.WriteLine("AuthenticationType = {0}", piSystem.ConnectionInfo.AuthenticationType);
 8Console.WriteLine();
 9
10// explicit connection using ClientId and ClientSecret
11piSystem.Connect(TestClientId, TestClientSecret);
12Console.WriteLine("CurrentUserName = {0}", piSystem.CurrentUserName);
13Console.WriteLine("AuthenticationType = {0}", piSystem.ConnectionInfo.AuthenticationType);
14Console.WriteLine();
15
16// for explicit connections using Authorization Code Flow refer to the ConnectWithPrompt documentation.

Collective Connection

This example demonstrates the method for connecting to an AF server configured as a collective.

 1// Get the PISystems collection for the current user and default PISystem.
 2PISystem myPISystem = new PISystems().DefaultPISystem;
 3
 4// Set default for all connections to be based upon collective member's priority.
 5AFConnectionInfo.DefaultPreference = AFConnectionPreference.Any;
 6AFCollectiveMember myMember;
 7
 8// Simple connect will use Default Preference.
 9myPISystem.Connect();
10myPISystem.Disconnect();
11
12// Check if default PISystem is a Collective.
13if (myPISystem.Collective != null)
14{
15    // Connect specifying that Primary is required and display a credential
16    // Connect and display a credential prompt dialog if current user login fails.
17    // Only available in .Net Framework AFSDK
18    // myPISystem.Connect(true, null, AFConnectionPreference.RequirePrimary);
19    // myPISystem.Disconnect();
20
21    // Connect specifying that Primary is required and display a credential
22    // Connect and display a credential prompt dialog if current user login fails.
23    // Only available in .Net Framework AFSDK.
24    // Connect to a specific collective member and display a credential
25    // Prompt dialog if current user login fails.
26    // myMember = myPISystem.Collective.Members[0];
27    // myMember.Connect(true, null);
28    // myPISystem.Disconnect();
29
30    try
31    {
32        // Connect to a specific collective member using a specified credential.
33        myMember = myPISystem.Collective.Members[0];
34        NetworkCredential credential = new NetworkCredential("guest", String.Empty);
35        myMember.Connect(credential);
36    }
37    catch (Exception ex)
38    {
39        // Expected exception since credential needs a valid user name and password.
40        Console.WriteLine(ex.Message);
41    }
42}

Multi-User Connection

This example demonstrates the method for connecting to an AF server using an implicit connection in a multi-user environment. In this environment, it is important not to re-use any AF objects if the user identity could have changed. Instead, you need to get a new PISystems collection before accessing any AF objects.

1// Get the PISystems collection for the current user and default PISystem.
2PISystem myPISystem = new PISystems().DefaultPISystem;
3
4// Do operation, a connection will automatically be created if needed for this user.
5AFValue myValue = null;
6AFAttribute myAttribute = AFAttribute.FindAttribute(@"MyDatabase\Element#1|Attribute#1", myPISystem);
7if (myAttribute != null)
8    myValue = myAttribute.GetValue();

See Also

In This Topic
Related Links
TitleResults for “How to create a CRG?”Also Available in