In this tutorial, follow step-by-step instructions to create the foundational moving parts common to most Orleans applications. It's designed to be self-contained and minimalistic.
This tutorial lacks appropriate error handling and other essential code useful for a production environment. However, it should help you gain a hands-on understanding of the common Orleans app structure and allow you to focus your continued learning on the parts most relevant to you.
Prerequisites Project setupFor this tutorial, create four projects as part of the same solution:
Replace the default code with the code provided for each project.
Silo
and name the solution OrleansHelloWorld
. For more information on creating a console app, see Tutorial: Create a .NET console application using Visual Studio.Client
.GrainInterfaces
. For information on creating a class library, see Tutorial: Create a .NET class library using Visual Studio.Grains
.Microsoft.Orleans.Server
Microsoft.Extensions.Logging.Console
Microsoft.Extensions.Hosting
Client Microsoft.Orleans.Client
Microsoft.Extensions.Logging.Console
Microsoft.Extensions.Hosting
Grain Interfaces Microsoft.Orleans.Sdk
Grains Microsoft.Orleans.Sdk
Microsoft.Extensions.Logging.Abstractions
Microsoft.Orleans.Server
, Microsoft.Orleans.Client
, and Microsoft.Orleans.Sdk
are metapackages that bring dependencies you'll most likely need on the Silo and client. For more information on adding package references, see dotnet package add or Install and manage packages in Visual Studio using the NuGet Package Manager.
In the GrainInterfaces project, add an IHello.cs code file and define the following IHello
interface:
namespace GrainInterfaces;
public interface IHello : IGrainWithIntegerKey
{
ValueTask<string> SayHello(string greeting);
}
Define a grain class
In the Grains project, add a HelloGrain.cs code file and define the following class:
using GrainInterfaces;
using Microsoft.Extensions.Logging;
namespace Grains;
public class HelloGrain : Grain, IHello
{
private readonly ILogger _logger;
public HelloGrain(ILogger<HelloGrain> logger) => _logger = logger;
ValueTask<string> IHello.SayHello(string greeting)
{
_logger.LogInformation("""
SayHello message received: greeting = "{Greeting}"
""",
greeting);
return ValueTask.FromResult($"""
Client said: "{greeting}", so HelloGrain says: Hello!
""");
}
}
Create the silo
To create the Silo project, add code to initialize a server that hosts and runs the grainsâa silo. Use the localhost clustering provider, which allows running everything locally without depending on external storage systems. For more information, see Local Development Configuration. In this example, run a cluster with a single silo.
Add the following code to Program.cs of the Silo project:
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
IHostBuilder builder = Host.CreateDefaultBuilder(args)
.UseOrleans(silo =>
{
silo.UseLocalhostClustering()
.ConfigureLogging(logging => logging.AddConsole());
})
.UseConsoleLifetime();
using IHost host = builder.Build();
await host.RunAsync();
The preceding code:
host
and waits for the process to terminate by listening for Ctrl+C or SIGTERM
.Finally, configure a client to communicate with the grains, connect it to the cluster (with a single silo), and invoke the grain. The clustering configuration must match the one used for the silo. For more information, see Clusters and Clients.
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using GrainInterfaces;
IHostBuilder builder = Host.CreateDefaultBuilder(args)
.UseOrleansClient(client =>
{
client.UseLocalhostClustering();
})
.ConfigureLogging(logging => logging.AddConsole())
.UseConsoleLifetime();
using IHost host = builder.Build();
await host.StartAsync();
IClusterClient client = host.Services.GetRequiredService<IClusterClient>();
IHello friend = client.GetGrain<IHello>(0);
string response = await friend.SayHello("Hi friend!");
Console.WriteLine($"""
{response}
Press any key to exit...
""");
Console.ReadKey();
await host.StopAsync();
Run the application
Build the solution and run the Silo. After receiving the confirmation message that the Silo is running, run the Client.
To start the Silo from the command line, run the following command from the directory containing the Silo's project file:
dotnet run
You'll see numerous outputs as part of the Silo startup. After seeing the following message, you're ready to run the client:
Application started. Press Ctrl+C to shut down.
From the client project directory, run the same .NET CLI command in a separate terminal window to start the client:
dotnet run
For more information on running .NET apps, see dotnet run. If you're using Visual Studio, you can configure multiple startup projects.
See alsoRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4