Core library of Microsoft Orleans used both on the client and server.
$ dotnet add package Microsoft.Orleans.CoreMicrosoft Orleans Core is the primary library used by both client and server applications. It provides the runtime components necessary for Orleans applications, including serialization, communication, and the core hosting infrastructure.
To use this package, install it via NuGet:
dotnet add package Microsoft.Orleans.Core
This package is automatically included when you reference the Orleans SDK or the Orleans client/server metapackages.
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Orleans;
using Orleans.Configuration;
using System;
using System.Threading.Tasks;
// Define a grain interface
namespace MyGrainNamespace;
public interface IHelloGrain : IGrainWithStringKey
{
Task<string> SayHello(string greeting);
}
// Implement the grain interface
public class HelloGrain : Grain, IHelloGrain
{
public Task<string> SayHello(string greeting)
{
return Task.FromResult($"Hello! I got: {greeting}");
}
}
// Create a client
var builder = Host.CreateApplicationBuilder(args)
.UseOrleansClient(client =>
{
client.UseLocalhostClustering();
});
var host = builder.Build();
await host.StartAsync();
// Get a reference to a grain and call it
var grain = host.Services.GetRequiredService<IClusterClient>().GetGrain<IHelloGrain>("grain-id");
var response = await grain.SayHello("Hello from client!");
// Print the result
Console.WriteLine($"Response: {response}");
// Keep the host running until the application is shut down
await host.WaitForShutdownAsync();
For more comprehensive documentation, please refer to: