1

Passing AcqClient and NcClient to the same interface

//AcqClient and NcClient are static ReadOnly Variables

      public static void Connection(){
        AcqClient = new ElasticClient(AcqConnection);
        NcClient= new ElasticClient(NcConnection);

Creating Singleton Object

        services.AddSingleton<IElasticClient>(AcqClient);
        services.AddSingleton<IElasticClient>(NcClient);
     }

Implementing in Startup.cs

  service.Connection(configuration)

On my controller class when i am doing this i am getting the last one object

    public class controller:Controller{
     public controller(IElasticClient elasticclient){
     
        _elasticclient=elasticlient;
        }
     }
Osama Rizwan
  • 615
  • 1
  • 7
  • 19
Asad
  • 617
  • 2
  • 8
  • 23
  • Please refer this https://stackoverflow.com/questions/39174989/how-to-register-multiple-implementations-of-the-same-interface-in-asp-net-core which will help you to get answer. – Harish Sep 18 '20 at 04:52
  • In this question classess are implementing interfaces and i want to pass connection string variables – Asad Sep 18 '20 at 05:16
  • Asad your comment does not seem to be related to question in its current state - question shows registering the same interface for 2 objects which is exactly what post suggested by @Hearty show... Could you please clarify why you believe question asks something else? (Ideally you would [edit] question explaining how that linked question did not help in addition to clarification of what you want to achieve) – Alexei Levenkov Sep 18 '20 at 06:10
  • @AlexeiLevenkov please check i have updated the question – Asad Sep 18 '20 at 06:13
  • @Asad I don't think it changed anything... but clearly you have some problem... I just don't get what it is (you essentially claim it is not related to registering interface in the container - which is where I get lost on what you are trying to get help with) – Alexei Levenkov Sep 18 '20 at 06:16
  • wait a minute brother i am updating question – Asad Sep 18 '20 at 06:20

1 Answers1

1

I have done it like this

IServiceCollection Delegate

public delegate IServiceCollection ServiceResolver(string key);

Creating Elastic Search Singleton Object

   var firstobject = new ElasticClient(firstconnection);
   var secondobject= new ElasticClient(secondconnection);

Creating Object using ServiceResolver


    services.AddTransient<ServiceResolver>(serviceProvider => key =>
             {
                if (key== "A")
                {
                   services.AddSingleton<IElasticClient>(firstobject);
                }
                else if (key == "N")
                {
                    services.AddSingleton<IElasticClient>(secondobject);
                }
                return services;
        });

In One Controller Different Node Connection Of ElasticSearch

   private static IElasticClient _elasticClient;
        private readonly IServiceCollection _aService;

        public static string clsName = "ElasticRandDController";

        public FirstConstructor(ServiceResolver serviceAccessor)
        {
          
            _aService = serviceAccessor("N");
             var serviceProvider = _aService.BuildServiceProvider();
            _elasticClient = serviceProvider.GetService<IElasticClient>();
        }

In Other Controller

     private static IElasticClient _elasticClient;
        private readonly IServiceCollection _aService;
        public SecondConstructor(ServiceResolver serviceAccessor)
        {
            _aService = serviceAccessor("A");
            var serviceProvider = _aService.BuildServiceProvider();
            _elasticClient = serviceProvider.GetService<IElasticClient>();
        }
Asad
  • 617
  • 2
  • 8
  • 23