Writing code that uses pooled objects

The code that you write when using pooling will look somewhat different than code for use without pooling, which was described in Using your assembly: The basics. Because the pooled objects already have a connection to xfServerPlus, you do not need to call the connect() method. The disconnection from xfServerPlus is also handled by the pool, so you do not need to call disconnect().

Note

Because you do not call the connect() method when using pooling, you cannot specify the host name and port at runtime. You can either use the program defaults or specify the host name and port in an application configuration file (for .NET Framework) or with environment variables (for all .NET clients). See Specifying configuration settings for more information.

For .NET Framework, you must add a reference in your Visual Studio project to System.EnterpriseServices. The xfNetLink .NET classes use this namespace.

For .NET Framework, if your client application is written in C#, when using pooling you should instantiate the object with a using statement as shown in the example below. Instantiating the object in this manner means that it exists only within the scope of the using statement. When the using statement ends, the object is released, and the Deactivate() and CanBePooled() methods are called. Failure to instantiate the object with a using statement can result in problems with the object being properly released and returned to the pool for reuse.

For example,

string userID = "MFranklin";
string password = "abc123";
try 
{
    using (AppLogin userSess = new AppLogin())
    {
        userSess.login(userID, password);
    }
} 
catch (Exception e) 
{
    //Error handling code
} 
Note

If you are using ASP, instantiating the object within a using statement limits you to using the object on a single ASP page. If your application requires that the object have session scope (that is, you need to maintain state across several ASP pages), we do not recommend using pooling, as it is difficult to ensure that the object will be properly released, returned to the pool, and reused.

The following code shows the same example written in Visual Basic.

Dim userID As New String("MFranklin")
Dim password As New String("abc123")
Try
  Using userSess As New AppLogin()
    userSess.login(userID, password)
  Catch Ex As Exception
    ' Error handling code
  End Using
End Try

The following C# example shows how to use multiple copies of the same class in conjunction with pooling. (See Using multiple copies of the same class for more information about this feature.) In this example, our client application routes the user to a particular xfServerPlus port based upon a customer ID entered on the logon screen. We generated two instances of the AppLogin class, AppLogin and AppLogin1. We’ll use the generated C# interface (IAppLogin) to access methods in these classes so that all users can use the same client application, even though they are using different servers. As in the example above, we instantiate the object with the using statement.

//Class fields
private string userID;
private string password;
//Method to set up the classes to create instances at each port
private void Button_Click(object sender,
  System.EventArgs e)
{
  //Class name consists of "NameSpace.Class,Assembly"
  //Create instance for xfSP port 2356
  string className = "ABCComputers.AppLogin,ConsultIt";
  signOn(className);
  .
  .
  .
  //Create instance for xfSP port 4550
  className = "ABCComputers.AppLogin1,ConsultIt";
  signOn(className);
  .
  .
  .
}
//Method to create C# interface and use it with pooling
static void signOn(string className)
{
  using (IAppLogin userSess = getIAppLogin(className))
  {
      //Call the Synergy method using the C# interface
      userSess.login(userID, password);
  }
}
//Method to create an instance of the correct class and cast it
// to the C# interface
static IAppLogin getIAppLogin(string className)
{
  //Dynamically create an instance 
  Type tp = Type.GetType(className);
  Object obj = Activator.CreateInstance(tp);
  //Assign the object to the C# interface
  IAppLogin x = (IAppLogin)obj;
  return x;
}