0

Here is my controller class

Registering Service from Constructor Class

 public class NCElasticAPI : Controller
   {
     public NCElasticAPI(IElasticClient elasticClient,IConfiguration _config)
      {
         ConnectionToEs.GetConnectionKey(ConnectionStringKey.AcquirerElasticConnection);
        _elasticClient = elasticClient;
      }
   }

Here i am intiliazing IServiceCollection

I am calling GetConnectionKey Method from ConnectionToES Static Class

 public static IServiceCollection services { get; }

 public static void GetConnectionKey(string key)
    {

        Key_ = key;
        if (Key_ == ConnectionStringKey.AcquirerElasticConnection)
        {

          
            AcqNode = CommonObjects.GetCongifValue(ConfigKeys.AcqDataUrl);
            AcqNodes = new Uri[] { new Uri(AcqNode), };
            AcqPool = new StaticConnectionPool(AcqNodes);
            AcqConnection = new ConnectionSettings(AcqPool)
                   .BasicAuthentication(CommonObjects.GetCongifValue(ConfigKeys.AcqDataUserName), CommonObjects.GetCongifValue(ConfigKeys.AcqDataPassword))
                   .DisableDirectStreaming();


            var AcqClient = new ElasticClient(AcqConnection);
            services.AddSingleton<IElasticClient>(AcqClient);
          >services variables is parameterless or null
        }
    }

I know that in order to use IServiceCollection i have to do something like this

    public static void (this IServiceCollection services){}

but i dont want to call it from Start Up .cs Instead from Constructor Class

Asad
  • 617
  • 2
  • 8
  • 23

1 Answers1

-1

why don't you return AcqConnection, then make the service registration in StartUp.cs

services.AddSingleton<IElasticClient>(new ElasticClient(GetConnectionKey("myKey")));

Edit: You can have conditional dependency resolver.

services.AddTransient<Func<string, IElasticClient>>(serviceProvider => key =>
{
    return GetConnectionKey(key);
});

Then in controller,

public class NCElasticAPI : Controller
{
   public IElasticClient _elasticClient {get; set;} 

   public NCElasticAPI(Func<string, IElasticClient> elasticClientServiceAccessor)
   {
      _elasticClient = elasticClientServiceAccessor("key1");
   }
}

 

public static void GetConnectionKey(string key)
{

    Key_ = key;
    if (Key_ == ConnectionStringKey.AcquirerElasticConnection)
    {

      
        AcqNode = CommonObjects.GetCongifValue(ConfigKeys.AcqDataUrl);
        AcqNodes = new Uri[] { new Uri(AcqNode), };
        AcqPool = new StaticConnectionPool(AcqNodes);
        AcqConnection = new ConnectionSettings(AcqPool)
               .BasicAuthentication(CommonObjects.GetCongifValue(ConfigKeys.AcqDataUserName), CommonObjects.GetCongifValue(ConfigKeys.AcqDataPassword))
               .DisableDirectStreaming();


        var AcqClient = new ElasticClient(AcqConnection);
        return AcqClient ;
    }
}
Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72
  • Just Say i have two keys i want to use key 1 in controller 1 and key 2 in controller 2 then how can i do that in the constructor if i make two singleton transient object – Asad Sep 23 '20 at 07:53
  • This Does not help it goes to the function but i want to register the service from there. – Asad Sep 28 '20 at 11:20