user14650668
user14650668

Reputation: 41

How can I invoke an Azure Data Factory pipeline from C# code?

I need some ideas for invoking an ADF (Azure Data Factory) job from C# code. If you can share some examples implementing this, it will really be helpful.

Thank you for looking into my post.

Upvotes: 3

Views: 8345

Answers (2)

Alex Nolasco
Alex Nolasco

Reputation: 19456

Just in case you need to pass a parameter

private async Task<int> SynchronizeProvidersAsync(string sender)
{
    var credential = new DefaultAzureCredential();
    var armClient = new ArmClient(credential);
    var dataFactoryResourceId = DataFactoryResource.CreateResourceIdentifier(
            _azurePipelineSettings.SubscriptionId,
            _azurePipelineSettings.GroupName,
            _azurePipelineSettings.DataFactoryName
        );
    var dataFactory = armClient.GetDataFactoryResource(dataFactoryResourceId);
    var quotedSender = $"\"{sender?.ToLowerInvariant() ?? string.Empty}\"";
    var parameters = new Dictionary<string, BinaryData>
    {
        { "sender", BinaryData.FromString(quotedSender) }
    };

    DataFactoryPipelineResource pipeline = await dataFactory.GetDataFactoryPipelineAsync(_azurePipelineSettings.SynchronizePipelineName);

    await pipeline.CreateRunAsync(parameters);
    return 0;
}

Using

Azure.ResourceManager.DataFactory 1.4.0, Azure.Identity 1.13.0

Upvotes: 1

Connell.O&#39;Donnell
Connell.O&#39;Donnell

Reputation: 3703

Install the latest version of the Microsoft.Azure.Management.DataFactory NuGet package. Then you can trigger an ADF pipeline like this:

private DataFactoryManagementClient CreateClient(string subscriptionId, string tenantId)
    {
        // AzureServiceTokenProvider uses developer credentials when running locally
        // and uses managed identity when deployed to Azure.
        // If getting an exception when running locally, run "az login" command in Azure CLI
        var provider = new AzureServiceTokenProvider();
        var token = provider.GetAccessTokenAsync("https://management.azure.com", tenantId).Result;
        ServiceClientCredentials credentials = new TokenCredentials(token);

        DataFactoryManagementClient client = new DataFactoryManagementClient(credentials);
        client.SubscriptionId = subscriptionId;
        return client;
    }

    public async Task<string> TriggerPipeline(string pipelineName, IDictionary<string, object> parameters)
    {
        var run = await client.Pipelines.CreateRunWithHttpMessagesAsync(resourceGroupName, dataFactoryName, pipelineName, parameters: parameters);
        return run.Body.RunId;
    }

Upvotes: 15

Related Questions