How to connect to Fabric Datawarehouse or Fabric DataLake using C#

Debashis Jena 71 Reputation points
2024-05-28T13:04:48.9766667+00:00

We've a Data ware house present inside our Azure Fabric environment. We want to do crud operation on top of this.
How can we connect to Fabric Data warehouse or Fabric DataLake using C#.

Microsoft Fabric Training
Microsoft Fabric Training
Microsoft Fabric: A Microsoft unified data platform.Training: Instruction to develop new skills.
22 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Rakesh Gurram 5,070 Reputation points Microsoft Vendor
    2024-05-31T03:17:42.1733333+00:00

    Hi Debashis Jena,

    Thanks for reaching out to us on the Microsoft Q&A forum.

    You can connect to Microsoft Fabric Lakehouse using C# with the help of a service principal. For instructions on creating a service principal, please refer to the provided link: Service principal authentication for Microsoft purview.

    After creating a Service Principal in Azure Entra ID, you need to add it to Fabric workspaces as follows:

    CB1

    Ensure that access to service principals is allowed by enabling the following option in the Admin Portal:

    CB2

    After granting access, you should be able to establish a connection using the Service Principal. To connect to the Lakehouse in C# by generating a token with the client credentials flow, you can use the following sample code:

    #region
    using Microsoft.Identity.Client;
    using System.Net.Http.Headers;
    
    string ClientId = "appId";
    string ClientSecret = "secret"; 
    string Authority = "https://login.microsoftonline.com/tenantId";
    #endregion
    
    #region 
    string[] scopes = new string[] { "https://api.fabric.microsoft.com/.default" };
    
    ConfidentialClientApplicationBuilder confidentialClientAppBuilder =
        ConfidentialClientApplicationBuilder.Create(ClientId)
        .WithClientSecret(ClientSecret)
        .WithAuthority(Authority);
    
    IConfidentialClientApplication confidentialClientApplication = confidentialClientAppBuilder.Build();
    
    AuthenticationResult result = await confidentialClientApplication.AcquireTokenForClient(scopes)
        .ExecuteAsync()
        .ConfigureAwait(false);
    
    Console.WriteLine(result.AccessToken);
    Console.WriteLine();
    #endregion
    
    
    // Create client 
    HttpClient client = new HttpClient();
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
    string baseUrl = "https://api.fabric.microsoft.com/v1/";
    client.BaseAddress = new Uri(baseUrl);
    
    // Call list workspaces API 
    HttpResponseMessage response = await client.GetAsync("workspaces");
    string responseBody = await response.Content.ReadAsStringAsync();
    Console.WriteLine(responseBody);
    
    

    Please don't hesitate to reach out to us if you have any other queries.

    If you found the information helpful, we would greatly appreciate it if you could acknowledge it by selecting the Accept Answer & Upvote options.

    0 comments No comments