Member-only story

Microsoft Orleans — Reusing Grains and Grain State

Russell Hammett Jr. (Kritner)
7 min readOct 17, 2018

--

We’ve explored Orleans for distributing application logic across a cluster. Next, we’ll be looking at grain reuse and grain state…

Recap

Note the starting point of this code can be found here. As described previously, grains are the “primitives” that are created for use with Orleans code. You invoke grains in a very similar manner to your “normal” code to make it as simple as possible. In the previous example we simply called the grain a single time; it takes in a value, and spits it back:

var grain = client.GetGrain<IHelloWorld>(Guid.NewGuid());
var response = await grain.SayHello(name); Console.WriteLine($"\n\n{response}\n\n");

Returns:

Entering “Kritner” returns “Kritner” along with a message that’s just the bee’s knees.

Grain Reuse

Grains don’t have to be used only once, in most situations I would wager they’re used in the hundreds and thousands of times. Though the current grain we’re working with doesn’t have much use to be invoked multiple times, it can still make for a (good?) example.

Let’s change our grain implementation a bit from:

public class HelloWorld : Grain, IHelloWorld
{
public Task<string> SayHello(string name)
{
return Task.FromResult($"Hello World! Orleans is neato torpedo, eh {name}?");
}
}

--

--

Russell Hammett Jr. (Kritner)
Russell Hammett Jr. (Kritner)

Written by Russell Hammett Jr. (Kritner)

Just a boring Application Developer/Dad. I enjoy gaming, learning new technologies, reading, and potentially other stuff. That’s about it.

No responses yet