Created
October 7, 2018 17:27
-
-
Save Kritner/dbba6369cf381ee5f3cae7f75131584b to your computer and use it in GitHub Desktop.
Orleans getting started part 0, client config
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
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"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
More or less from https://github.com/dotnet/orleans/blob/master/Samples/2.0/HelloWorld/src/OrleansClient/Program.cs