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