Last active
January 16, 2020 03:19
-
-
Save ReubenBond/b20ec3fb1a5873c426d0c3424a8fe2fb to your computer and use it in GitHub Desktop.
Orleans localhost demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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