elasticsearch - c#: how to implement singleton pattern to return client that should be initialised just once in application life cycle? -


currently, initialising elastic search client. want initialise elasticclient once in our application , re-use avoid unnecessary initialisation every time make elastic search call.

the code looks like:

public sealed class elasticclientinstance {     private static readonly elasticclientinstance _clientinstance = new elasticclientinstance();      private elasticclient _client;      static elasticclientinstance () {      }      public static elasticclient getinstance() {         return _clientinstance._client;     }      private elasticclientinstance()     {         try         {              uri[] nodes = configurationmanager.appsettings["elastichosturl"].split(';')                             .select(s => new uri("http://" + s)).toarray();             var connectionpool = new sniffingconnectionpool(nodes);             var settings = new connectionsettings(...); //some code not mentioned here             _client = new elasticclient(settings);         }         catch (exception ex)          {             //do necessary handling         }      } } 

anytime need elastic client, elasticclient.getinstance(). correct way make client singleton?

i read initialising "_clientinstance" readonly makes thread safe? performance oriented , safe implementation of singleton pattern?


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -