Over a million developers have joined DZone.

Using Apache Ignite.NET in LINQPad

DZone's Guide to

Using Apache Ignite.NET in LINQPad

Here is a quick how-to for using Apache Ignite.NET in LINQPad.

· Big Data Zone
Free Resource

Need to build an application around your data? Learn more about dataflow programming for rapid development and greater creativity. 

LINQPad is a must-have tool for every .NET developer, and it is a great way to explore and try Ignite.NET APIs.

LINQPad Logo

Getting Started

  1. Download LINQPad: linqpad.net/Download.aspx. Make sure to choose AnyCPU version on x64 OS.
  2. Install Ignite.NET NuGet packages:
    • Press F4 (or click Query -> References and Properties menu).
    • Click “Add NuGet…”. A warning may appear: As you don't have LINQPad Premium/Developer Edition, you can only search for NuGet packages that include LINQPad samples. This is fine since Ignite packages do include LINQPad samples.
    • Install packages by clicking “Add To Query” button.
    • Click “Add namespaces” link button and add (at least) the first one: Apache.Ignite.Core.
    • Close NuGet window, click OK on Query Properties window.
  3. Make sure Language drop-down is set to “C# Expression” (this is the default).
  4. Type Ignition.Start() (without semicolon) and hit F5.

Ignite node starts and you can see the usual console output in the output pane, as well as the resulting Ignite object dump.

Check out the bundled examples on “Samples” tab to the left.

Ignite in LINQPad

Recycling the Worker Process

LINQPad runs user code in a separate process. By default, this process is reused between runs (for performance reasons).

This has two consequences:

  1. Ignite.NET starts an in-process JVM. This JVM is reused when the process is reused, so you can’t change the JVM options.
  2. Ignition class keeps all started nodes in a static map. These nodes will keep running when the process is reused, which may cause Default Ignite instance has already been started exception if you run Ignition.Start() code twice.

This behavior may be useful in some scenarios and unwanted in other, but the best thing is that we can control it via built-in Util.NewProcess property. Switch Language dropdown on top to “C# Statement(s)” mode and run the following script:

Util.NewProcess = true;

This script can be run multiple times with no issues, since every time we start from scratch.

Reusing Started Node

Ignite node takes some time to start (8 seconds on my machine) due to JVM startup and network discovery process. To quickly iterate on our code in LINQPad, we can reuse started node between runs. For example, the following code reuses started Ignite instance and reuses existing cache, adding one item per run and showing existing items:

// Get existing instance or start a new one
var ignite = Ignition.TryGetIgnite() ?? Ignition.Start();

// Get existing cache or create a new one
var cache = ignite.GetOrCreateCache<Guid, DateTime>("cache");

// Add a new entry
cache[Guid.NewGuid()] = DateTime.Now;

// Show all entries

Contrary to several seconds for a fresh node start, this code runs in a couple of milliseconds.

When needed, use Shift+Control+F5 to unload the AppDomain and start from scratch.

Usage Tips and Tricks

Besides exploring the Ignite API in general, I’ve come up with the following Ignite+LINQPad use cases:

Examine Cache in a Running Cluster

Visor command line can show the cache contents, but doing this in LINQPad is much more flexible and user-friendly. Since we don’t have any actual classes in the LINQPad script, we have to use cache in binary mode in order to be able to read the contents.

The following code shows a list of all caches along with top 5 cache entries from each of them:

var ignite = Ignition.TryGetIgnite() ?? Ignition.Start();

foreach (var cacheName in ignite.GetCacheNames())
    ignite.GetCache<object, object>(cacheName)
        .WithKeepBinary<object, object>()
        .Select(x => x.ToString())

Convert Spring XML Configuration to C# IgniteConfiguration

Let’s say you have some Ignite Sring XML configuration file and need the same configuration for Ignite.NET; or you are migrating from Ignite.NET 1.5, where Spring XML was the only configuration mechanism.

You can simply start a node with said Spring XML file and call GetConfiguration() to see how it looks in .NET:


Convert IgniteConfiguration to app.config XML

Ignite.NET supports app.config and web.config configuration. However, writing XML is no fun. UsingIgniteConfiguration in C# is easier, IDE helps a lot, and invalid code just won’t compile.

To help us with XML, there is a hidden way to convert IgniteConfiguration instance to the XML representation. The following snippet shows how to use it via Reflection (make sure to set Language dropdown to C# Program):

void Main()
    new IgniteConfiguration
        CacheConfiguration = new[]
            new CacheConfiguration
                Name = "myCache",
                CacheMode = CacheMode.Replicated

public static class IgniteConfigurationExtensions
    public static string ToXml(this IgniteConfiguration cfg)
        var sb = new StringBuilder();

        var settings = new XmlWriterSettings
            Indent = true

        using (var xmlWriter = XmlWriter.Create(sb, settings))
                .Invoke(null, new object[] {cfg, xmlWriter, "igniteConfiguration"});

        return sb.ToString();

And the result is:

<?xml version="1.0" encoding="utf-16"?>
<igniteConfiguration xmlns="http://ignite.apache.org/schema/dotnet/IgniteConfigurationSection">
        <cacheConfiguration name="myCache" cacheMode="Replicated" />

Combined with the previous Spring XML use case, you can also convert Spring XML to app.config XML!

Next Ignite.NET versions will include this ToXml method as part of the public API, but for 1.6 and 1.7 Reflection is the way to go.

Check out the Exaptive data application Studio. Technology agnostic. No glue code. Use what you know and rely on the community for what you don't. Try the community version.

.net ,distributed caching ,linq ,linqpad

Published at DZone with permission of Pavel Tupitsyn. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}