Efficient way to make connection to azure data factory in .Net core API app

net core application. I am trying to call azure data factory from my .net core app. To establish connection and call adf I have created below interface with two methods.

 public interface IAzureDataFactoryRepository
 {
    public DataFactoryManagementClient InitiateConnection(AuthenticationConfig authenticationConfig);

    public Task<string> RunADFPipeline(DataFactoryManagementClient dataFactoryManagementClient, Dictionary<string, object> keyValuePairs, string resourceGroup, string dataFactoryName, string pieplineName);
 }

Then I implemented in class as below.

 public class AzureDataFactoryRepository : IAzureDataFactoryRepository
    {  

        private DataFactoryManagementClient _dataFactoryManagementClient;

        public DataFactoryManagementClient InitiateConnection(AuthenticationConfig authenticationConfig)
        {   
            var context = new AuthenticationContext("https://login.windows.net/" + authenticationConfig.TenantId);
            ClientCredential cc = new ClientCredential(authenticationConfig.ClientId, authenticationConfig.ClientSecret);
            AuthenticationResult result = context.AcquireTokenAsync(
                "https://management.azure.com/", cc).Result;
            ServiceClientCredentials cred = new TokenCredentials(result.AccessToken);
            _dataFactoryManagementClient = new DataFactoryManagementClient(cred)
            {
                SubscriptionId = authenticationConfig.SubscriptionId
            };
            return _dataFactoryManagementClient;
        }

        public async Task<string> RunADFPipeline(DataFactoryManagementClient dataFactoryManagementClient, Dictionary<string,object> keyValuePairs, string resourceGroup, string dataFactoryName, string pieplineName)
        {  
                CreateRunResponse runResponse = dataFactoryManagementClient.Pipelines.CreateRunWithHttpMessagesAsync(resourceGroup, dataFactoryName, pieplineName, parameters: keyValuePairs).Result.Body;
                return runResponse.RunId;
        }
    }

Above Interface and Class I added in Repository Layer. Whenever I want to call my ADF pipeline I am doing like below.

            var adfAuthSettings = Configuration.GetSection("AzureAdF").Get<ADFClient>();

            var azureAdAuthSettings = Configuration.GetSection("AzureAd").Get<AuthenticationConfig>();

            Dictionary<string, object> parameters = new Dictionary<string, object>
                {
                    {"parameter", parameters}
                };

            DataFactoryManagementClient dataFactoryManagementClient = azureDataFactoryRepository.InitiateConnection(azureAdAuthSettings);
            var result = await azureDataFactoryRepository.RunADFPipeline(dataFactoryManagementClient, parameters, adfAuthSettings.ResourceGroupName, adfAuthSettings.DataFactoryName, ApplicationConstants.SciHubPipeline);
            return result;

In the above code whenever I want to call ADF repository RunADFPipeline first I should always call InitiateConnection by passing values from my Configuration. Also each time I should do call adfAuthSettings = Configuration.GetSection("AzureAdF").Get<ADFClient>();to load values to model. Is there any way can we do it only once instead of doing inside all methods?
Then I will be able to RunADFPipeline method. Every time InitiateConnection should be called before calling RunADFPipeline. Is there any way we can write above code more efficiently? Can anyone suggest me better approach. Any help would be appreciated. Thanks

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

This is the model you can use :

  public class AuthenticationConfig : IAuthenticationConfig 
  {
    public AuthenticationConfig ()
    {
    }
    public string Config{ get; set; }           
  }

  public interface IAuthenticationConfig 
  {
     public string Config{ get; set; }
  }

This in startup :

public Startup(IConfiguration configuration)
    {
       Configuration = configuration;
    }  
  
    public IConfiguration Configuration { get; }    

    public void ConfigureServices(IServiceCollection services)
    { 
            services.Configure<AzureSettings>(options =>
            {
               options.Config= 
               Configuration.GetSection("AuthenticationConfig : Config").Value;
          });
            //*inject your repositories 
            services.AddSingleton<IAzureDataFactoryRepository, AzureDataFactoryRepository >();

      }

By configuring this in your starup you make it possible to use it across your soulution :

 public class AzureDataFactoryRepository : IAzureDataFactoryRepository
 {  
    private readonly  AuthenticationConfig _authenticationConfig;
    public AzureDataFactoryRepository(IOptions<AzureSettings> authenticationConfig)
    {            
      _authenticationConfig= _authenticationConfig.Value.Config;           
    }
 //...
 }

Here you have another example of Options pattern:

Dependency injection between two ASP.NET Core projects

You can use it to inject your settings and make them visible for entire solution. Its strongly recomennded by Microsoft.

If you want to read more :
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-3.1


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x