internal static void SeedElasticsearch(this IApplicationBuilder app) { if (app == null) { throw new ArgumentNullException(nameof(app)); } IElasticClient client = app.ApplicationServices.GetService <IElasticClient>(); AppSettings settings = app.ApplicationServices.GetService <AppSettings>(); string index = settings != null && settings.ElasticConfig != null && string.IsNullOrWhiteSpace(settings.ElasticConfig.DefaultIndex) ? settings.ElasticConfig.DefaultIndex : "friends"; var observable = Observable.Create <bool> (async o => { var elasticHealth = await client.CatHealthAsync(); if (elasticHealth.IsValid) { Console.WriteLine("Elasticsearch is UP!"); o.OnNext(true); o.OnCompleted(); } else { Console.WriteLine("Elasticsearch is down!"); o.OnError(new Exception("Elasticsearch is down")); } }); var observer = Observer.Create <bool> (isElasticsearchUp => { // Remove then create the index client.DeleteIndex(index); client.CreateIndex(index, c => c.Mappings(ms => ms.Map <Friend> (m => m.AutoMap()))); // Bulk insert random friends; bulk for performance client.Bulk((s) => { return(s.IndexMany(Friend.BuildRandomFriends(10))); }); }); observable.DelaySubscription(TimeSpan.FromSeconds(1)).Retry(60).Subscribe(observer); }