[.net programming language only]

Getting started tutorial lesson 2.2: Creating a .NET client application

[Version 8.6 and later]To insert, delete, update, and retrieve data from your data grid, you must write a client application. The getting started sample includes a .NET client application that you can use to learn about creating your own client application.

Before you begin

  • You must have the WebSphere® eXtreme Scale Client for .NET installed. For more information, see Installing WebSphere eXtreme Scale Client for .NET.
  • The project file for the sample works with Microsoft Visual Studio 2010 or later. If you are using a previous version of Microsoft Visual Studio, you must create your own project file.

About this task

You can use the .NET getting started sample application for the following purposes:
  • To verify that you have installed the WebSphere eXtreme Scale Client for .NET correctly.
  • To learn how to write applications that for the .NET client that communicate with the data grid, so you can create custom applications. The sample demonstrates how to connect to a data grid on a remote catalog server. The interactive mode demonstrates how to run manual transactions using the GridMapPessimisticTx map. The command line mode demonstrates auto-commit transactions with the GridMapPessimisticAutoTx map.
  • To learn how to interoperate with the Java™ getting started sample. Both sample applications store items in the data grid with TestKey/TestValue pairs. The .NET sample has ClassAlias and FieldAlias attributes to create unique identifiers for serialization and de-serialization. If an insert key operation is run from the Java client application, the .NET client can get the value by running a get operation on the key that was inserted.
The .NET getting started sample application has the following limitations:
  • Only pessimistic locking is supported.
  • Two-phase commit operations are not supported. You can commit operations to one partition only. If you run a commit that involves multiple partitions, a MultiplePartitionWriteException exception results.
  • The sample does not support null values. The .NET API does allow null values, but you must use nullable types.

The SimpleClient.csproj project file is in the net_client_home/sample/SimpleClient directory. This project file is the client program that demonstrates how to connect to a catalog server, obtain the ObjectGrid instance, and use the ObjectMap API. The ObjectMap API stores data as key-value pairs and is ideal for caching objects that have no relationships involved. The following steps contain information about the key contents of the SimpleClient.csproj file. You can also look at the project file in more detail in Microsoft Visual Studio.

The tutorial demonstrates the use of IGridMapPessimisticTx, which is the manual transaction map that is used when the application is run in interactive mode. If you use the application in command-line mode, the IGridMapPessimisticAutoTx map is used.

Procedure

  1. Connect to the catalog service by obtaining a IClientConnectionContext instance.

    To connect to the catalog server, use the Connect method of the IGridManager API.

    IGridManager gm = GridManagerFactory.GetGridManager( );
    ICatalogDomainInfo cdi = gm.CatalogDomainManager.CreateCatalogDomainInfo( endpoint );
    ccc = gm.Connect( cdi, "SimpleClient.properties" );
    If the connection to the catalog server succeeds, the Connect method returns a IClientConnectionContext instance. The IClientConnectionContext instance is required to obtain the data grid from the IGridManager API.
  2. Obtain an ObjectGrid instance.

    To obtain an ObjectGrid instance, use the GetGrid method of the IGridManager API. The GetGrid method requires both the IClientConnectionContext instance and the name of the data grid instance. The IClientConnectionContext instance is obtained during the connection to the catalog server. The name of data grid instance is the grid that is specified in the objectgrid.xml file.

    grid = gm.GetGrid( ccc, gridName );
  3. Get a map instance.

    You can get a map instance by calling the GetGridMapPessimisticTx method of the IGrid API. Pass the name of the map as parameter to the GetGridMapPessimisticTx method to get the map instance.

    pessMap = grid.GetGridMapPessimisticTx<Object, Object>( mapName );
  4. Use the IGridMapPessimisticTx methods.

    After a map instance is obtained, you can use the IGridMapPessimisticTx API.

    The following code snippet demonstrates how to use the IGridMapPessimisticTx API.

    • To begin a transaction with the IGridMapPessimisticTx API, you must call the map.Transaction.Begin() method. This method starts a new transaction in which you can run operations.
      case "begin":
         map.Transaction.Begin( );
         return 0;
    • The add method inserts a new key/value pair . If the key currently exists, then an exception is thrown.
       case "a":
         if( key == null ) throw new MissingParameterException( "key" );
         if( value == null ) throw new MissingParameterException( "value" );
         map.Add( key, value );
         Console.WriteLine( "SUCCESS: Added key '{0}' with value '{1}', 
         partitionId={2}", key, value, partitionId );
         return 0;
    • The put method inserts or updates a key/value pair.
      case "p":
        if( key == null ) throw new MissingParameterException( "key" );
        if( value == null ) throw new MissingParameterException( "value" );
        map.Put( key, value );
        Console.WriteLine( "SUCCESS: Put key '{0}' with value '{1}', 
        partitionId={2}", key, value, partitionId );
        return 0;
    • The replace method replaces an existing key/value pair. If the item is not present, then an exception is thrown.
      case "r":
        if( key == null ) throw new MissingParameterException( "key" );
        if( value == null ) throw new MissingParameterException( "value" );
        map.Replace( key, value );
        Console.WriteLine( "SUCCESS: Replaced key '{0}' with value '{1}', 
        partitionId={2}", key, value, partitionId );
        return 0;
    • The remove method deletes a key/value pair.
      case "d":
        if( key == null ) throw new MissingParameterException( "key" );
        map.Remove( key );
        Console.WriteLine( "SUCCESS: Deleted value with key '{0}', 
        partitionId={1}", key, partitionId );
      	  return 0;
    • The get method retrieves the value for the given key.
       case "g":
        if( key == null ) throw new MissingParameterException( "key" );
        value = ( TestValue )map.Get( key );
        if( value != null )
        {
          Console.WriteLine( "SUCCESS: Value is '{0}', 
        partitionId={1}", value, partitionId );
        }
        else
        {
          Console.WriteLine( "FAILED: Key not found" );
        }
        return 0;
    • If you want to cancel the operations that you performed in the operation before you commit, use the rollback method.
      case "rollback":
        map.Transaction.Rollback( );
        return 0;
    • The commit method commits the operations that completed in the transaction.
      case "commit":
        map.Transaction.Commit( );
        return 0;
Related tasks[Version 8.6 and later][.net programming language only]Setting up the .NET development environment[Version 8.6 and later]To use the WebSphere eXtreme Scale Client for .NET in Microsoft Visual Studio, you must install the development environment and configure your project to use the WebSphere eXtreme Scale Client for .NET assembly.[Version 8.6 and later][.net programming language only]Accessing WebSphere eXtreme Scale Client for .NET API documentation[Version 8.6 and later]You can access the WebSphere eXtreme Scale Client for .NET API documentation within a .chm file or by viewing the API documentation in the information center.

Lesson checkpoint

In this lesson, you learned how to create a simple .NET client application to run data grid operations.