Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Kritner
Created October 7, 2018 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kritner/dbba6369cf381ee5f3cae7f75131584b to your computer and use it in GitHub Desktop.
Save Kritner/dbba6369cf381ee5f3cae7f75131584b to your computer and use it in GitHub Desktop.
Orleans getting started part 0, client config
public class Program
{
const int initializeAttemptsBeforeFailing = 5;
private static int attempt = 0;
static int Main(string[] args)
{
return RunMainAsync().Result;
}
private static async Task<int> RunMainAsync()
{
try
{
using (var client = await StartClientWithRetries())
{
await DoClientWork(client);
Console.ReadKey();
}
return 0;
}
catch (Exception e)
{
Console.WriteLine(e);
Console.ReadKey();
return 1;
}
}
private static async Task<IClusterClient> StartClientWithRetries()
{
attempt = 0;
IClusterClient client;
client = new ClientBuilder()
.UseLocalhostClustering()
.Configure<ClusterOptions>(options =>
{
options.ClusterId = "dev";
options.ServiceId = "HelloWorldApp";
})
.ConfigureLogging(logging => logging.AddConsole())
.Build();
await client.Connect(RetryFilter);
Console.WriteLine("Client successfully connect to silo host");
return client;
}
private static async Task<bool> RetryFilter(Exception exception)
{
if (exception.GetType() != typeof(SiloUnavailableException))
{
Console.WriteLine($"Cluster client failed to connect to cluster with unexpected error. Exception: {exception}");
return false;
}
attempt++;
Console.WriteLine($"Cluster client attempt {attempt} of {initializeAttemptsBeforeFailing} failed to connect to cluster. Exception: {exception}");
if (attempt > initializeAttemptsBeforeFailing)
{
return false;
}
await Task.Delay(TimeSpan.FromSeconds(4));
return true;
}
private static async Task DoClientWork(IClusterClient client)
{
Console.WriteLine("Hello, what should I call you?");
var name = Console.ReadLine();
if (string.IsNullOrEmpty(name))
{
name = "anon";
}
// example of calling grains from the initialized client
var grain = client.GetGrain<IHelloWorld>(Guid.NewGuid());
var response = await grain.SayHello(name);
Console.WriteLine($"\n\n{response}\n\n");
}
}
@Kritner
Copy link
Author

Kritner commented Oct 7, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment