0

I have a requirement to connect to the server and collect data for processing. Below is my core class, which is responsible for looping through all the servers and try connecting them for processing.

public class CoreDataProcessingEngine : ICoreDataProcessingEngine 
{
     private readonly COMLib.ServerGateway _aServerGw;
     private COMLib.ServerErrorInfo _aServerErrorInfo;

     Public CoreDataProcessingEngine()
     {
          _aServerGw = new COMLib.ServerGateway();
          _aServerErrorInfo = new COMLib.ServerErrorInfo();
     }

     //When service starts, I am collecting all the server details from config and trying to connect ONE BY ONE.

     public async Task Start()
     {
           List<Server> servers = ConfigurationManager.GetSection("servers") as List<Server>;
           foreach (var serverdetails in servers)
           {
                var data = Task.Run(() => ConnectToServer(serverdetails ));
           }
     }
}

Here is my ConnectToServer method

  private async void ConnectToGateway(ServerDetails serverdetails )
  {
        await _aServerGw.connectToServerByName(serverdetails.serveraddress); 
  }

I have extended the connectToServerByName method as follow , which is in separate static class.

 public static class ComLibraryExtensions
 {

   public static Task connectToServerByName(this ProxyGW @this, string serveraddress)
    {
        var tcs = new TaskCompletionSource<object>();
        Action onSuccess = null;
        Action<int> onFailed = null;
        onSuccess = () =>
        {
            @this.onConnectSucceeded -= HandleManager_OnConnectSucceeded;
            @this.onConnectFailed -= HandleManager_OnConnectFailed;
            tcs.TrySetResult(null);
        };
        onFailed = hr =>
        {
            @this.onConnectSucceeded -= HandleManager_OnConnectSucceeded;
            @this.onConnectFailed -= HandleManager_OnConnectFailed;
            tcs.TrySetException(Marshal.GetExceptionForHR(hr));
        };
        @this.onConnectSucceeded += HandleManager_OnConnectSucceeded;
        @this.onConnectFailed += HandleManager_OnConnectFailed;
        @this.connectToGatewayByNameEx(serveraddress);
        return tcs.Task;
    }

    private static void HandleManager_OnConnectFailed(int hr)
    {
         //How do I get access to dependent objects here?    
         //Like ILogger ??
         _logger.Information(hr);
    }

    private static void HandleManager_OnConnectSucceeded()
    {
         //How do I get access @this??              
         @this.enableNotifications(true);//fails , it says @this does not exists
    }
   }

Question is:

  1. How do I get access to _aServerGw in HandleManager_OnConnectSucceeded event, because I want to set some property based on the success event.
  2. How do I get access to dependent objects here in extension classes like ILogger?
kudlatiger
  • 3,028
  • 8
  • 48
  • 98
  • [Question one](https://stackoverflow.com/questions/4215845/c-sharp-passing-extra-parameters-to-an-event-handler/4215916). Can you elaborate on Question 2? What is a "dependent object"? – ProgrammingLlama Jul 19 '19 at 06:55
  • on success, I want to update the database, so database repository is required to pass it as an injection to this extension class. so I used the term dependent objects – kudlatiger Jul 19 '19 at 07:07
  • 1
    You mean dependencies? You'll have to pass them in when you call `connectToServerByName`, or make them available via the `ProxyGW` object. – ProgrammingLlama Jul 19 '19 at 07:10
  • yes got it. what about question number 1? – kudlatiger Jul 19 '19 at 07:12
  • @John it says can not declare instance members in a static class while I pass ILogger and try to assign in to local variable . only through static constructor, we can assign dependent object. could u show some light? – kudlatiger Jul 19 '19 at 08:40

0 Answers0