Member-only story
Microsoft Orleans — Reusing Grains and Grain State
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:
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}?");
}
}