Skip to content

Instantly share code, notes, and snippets.

@ReubenBond
Last active Jan 16, 2020
Embed
What would you like to do?
Orleans localhost demo
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Orleans;
using Orleans.Hosting;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace SimpleDemo
{
class Program
{
static async Task Main(string[] args)
{
await Host.CreateDefaultBuilder()
.UseOrleans(siloBuilder => siloBuilder.UseLocalhostClustering())
.ConfigureServices(s => s.AddSingleton<IHostedService, MyHostedService>())
.RunConsoleAsync();
}
}
public class MyHostedService : BackgroundService
{
private readonly IGrainFactory grainFactory;
public MyHostedService(IGrainFactory grainFactory)
{
this.grainFactory = grainFactory;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
await this.grainFactory.GetGrain<IEmptyGrain>(Guid.Empty).NoOp();
await Task.Delay(1000);
}
}
}
public class EmptyGrain : Grain, IEmptyGrain
{
private readonly ILogger log;
public EmptyGrain(ILogger<EmptyGrain> log) => this.log = log;
public Task NoOp()
{
this.log.LogInformation("beep");
return Task.CompletedTask;
}
}
public interface IEmptyGrain : IGrainWithGuidKey
{
Task NoOp();
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.1" />
<PackageReference Include="Microsoft.Orleans.CodeGenerator.MSBuild" Version="3.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Orleans.Server" Version="3.0.2" />
</ItemGroup>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment