XAP.NET in Five Minutes

This tutorial explains how to deploy and use a XAP Data Grid from a .NET client application.

Prerequisites

  • .NET 6.0 SDK

  • Visual Studio 2022 or later

  • Java 8

Downloading and Installing XAP

GigaSpaces's XAP.NET is packaged as a standard Windows Installer package (.msi file).

Download the latest version of XAP from the Download Center, choosing Smart Cache/16.1 Enterprise Edition for .NET (x64).

Start the installation by double-clicking the .msi file. The installation wizard will guide you through the installation process.

Be sure to edit the license file, located at C:\GigaSpaces\smart-cache.net-16.1.0-x64\Runtime\gs-license.txt. To use the trial license, change the file to contain only the following line:

tryme

Starting a Service Grid

A Data Grid requires a Service Grid to host it. A service grid is composed of one or more machines (service grid nodes) running a Service Grid Agent (or GSA), and provides a framework to deploy and monitor applications on those machines, in our case the Data Grid.

In this tutorial you'll launch a single node service grid on your machine. To start the service grid, proceed as follows:

  • Open a command line prompt in the directory C:\GigaSpaces\smart-cache.net-16.1.0-x64\Runtime\bin

  • Type the following command:

  • gs host run-agent --auto

Deploying the Data Grid

The data grid can be deployed from command line, from the web management tool or via an Administration API. In this tutorial we'll use the command line.

Start a command line, navigate to the product's bin folder and run the following command:

  • Open a command line prompt in the directory C:\GigaSpaces\smart-cache.net-16.1.0-x64\Runtime\bin

  • Type the following commands:

  • gs container create --count=2 localhost

  • gs space deploy --partitions=2 myDataGrid

This command deploys a Data Grid (also called a Space) called myDataGrid with 2 partitions.

To see the resulting Space in the GigaSpaces Ops Manager, type the following URL in a browser:

http://localhost:8090/spaces

Interacting with the Data Grid

Setting up your IDE

Launch Visual Studio, create a new C# Console Application and add a reference to GigaSpaces.Core.dll from C:\GigaSpaces\smart-cache.net-16.1.0-x64\NET v6.0\Bin.

Implementing a PONO

Any PONO can be stored in the space, so long as it has a default constructor and an ID property.

The following code snippets are included here to explain the PONO functionality. The complete source code is provided below.

We define a Person class with the following properties:


				using GigaSpaces.Core.Metadata;
				public class Person
				{
				[SpaceID]
				public int? Ssn {get; set;}
				public String? FirstName {get; set;}
				public String? LastName  {get; set;}
				}
		

Note that we've annotated the Ssn property with a custom XAP.NET attribute ([SpaceID]) to mark it as the entries ID.

Interacting with the Data Grid

First, let's establish a connection to the data grid we've deployed:

ISpaceProxy spaceProxy = new SpaceProxyFactory("myDataGrid").Create();
		

Now that we have a proxy connected to the data grid, we can store entries in the grid using the Write() method and read them using various Read() methods:


				Console.WriteLine("Write (store) a couple of entries in the data grid:");
				spaceProxy.Write(new Person {Ssn=1, FirstName="Vincent", LastName="Chase"});
				spaceProxy.Write(new Person {Ssn=2, FirstName="Johnny", LastName="Drama"});

				Console.WriteLine("Read (retrieve) an entry from the grid by its id:");
				Person result1 = spaceProxy.ReadById<Person>(1);

				Console.WriteLine("Read an entry from the grid using LINQ:");
				var query = from p in spaceProxy.Query<Person>()
				where p.FirstName == "Johnny"
				select p;
				Person result2 = spaceProxy.Read<Person>(query.ToSpaceQuery());

				Console.WriteLine("Read all entries of type Person from the grid:");
				Person[] results = spaceProxy.ReadMultiple(new Person());
		

In the Ops Manager user interface, you will see two entries stored in the data grid, one in each partition.

Running the Application

Build and Run the sample .Net application,using the complete source code provided below.

The following is an example of the application output:


				Connecting to data grid...
				Write (store) a couple of entries in the data grid:
				Read (retrieve) an entry from the grid by its id:
				Result: Person #1: Vincent Chase
				Read an entry from the grid using LINQ:
				Result: Person #2: Johnny Drama
				Read all entries of type Person from the grid:
				Result: Person #1: Vincent Chase,Person #2: Johnny Drama
				Demo completed - press ENTER to exit

 

Complete Source Code


							using System;
							using System.Linq;
							using GigaSpaces.Core;
							using GigaSpaces.Core.Linq;

							namespace XapDemo
							{
							class Program
							{
							static void Main(string[] args)
							{
							Console.WriteLine("Connecting to data grid...");
							ISpaceProxy spaceProxy = new SpaceProxyFactory("myDataGrid").Create();

							Console.WriteLine("Write (store) a couple of entries in the data grid:");
							spaceProxy.Write(new Person { Ssn = 1, FirstName = "Vincent", LastName = "Chase" });
							spaceProxy.Write(new Person { Ssn = 2, FirstName = "Johnny", LastName = "Drama" });

							Console.WriteLine("Read (retrieve) an entry from the grid by its id:");
							Person result1 = spaceProxy.ReadById<Person>(1);
							Console.WriteLine("Result: " + result1);

							Console.WriteLine("Read an entry from the grid using LINQ:");
							var query = from p in spaceProxy.Query<Person>()
							where p.FirstName == "Johnny"
							select p;
							Person result2 = spaceProxy.Read<Person>(query.ToSpaceQuery());
							Console.WriteLine("Result: " + result2);

							Console.WriteLine("Read all entries of type Person from the grid:");
							Person[] results = spaceProxy.ReadMultiple(new Person());
							Console.WriteLine("Result: " + String.Join<Person>(",", results));

							Console.WriteLine("Demo completed - press ENTER to exit");
							Console.ReadLine();
							}
							}
							}
					

							using System;
							using GigaSpaces.Core.Metadata;

							namespace XapDemo
							{
							public class Person
							{
							[SpaceID(AutoGenerate=false)]
							public int? Ssn { get; set; }

							[SpaceIndex(Type=SpaceIndexType.Basic)]
							public String? FirstName { get; set; }

							public String? LastName { get; set; }

							public override string ToString()
							{
							return "Person #" + Ssn + ": " + FirstName + " " + LastName;
							}
							}
							}