如何在C# .NET中按资源组列出Azure Workspaces?
Great question! Let’s walk through your options here—since you’ve already pinpointed the correct REST API endpoint, we’ll cover both using the Azure .NET SDK (if available) and directly calling the API with HttpClient.
First: Check for a .NET SDK Equivalent
Before diving into raw HTTP calls, it’s worth verifying if the Azure .NET SDK wraps this operation. The Databricks management functionality lives in the Microsoft.Azure.Management.Databricks NuGet package.
First, install the package if you haven’t already:
Install-Package Microsoft.Azure.Management.Databricks
Then you can use the DatabricksManagementClient to list workspaces by resource group directly:
using Microsoft.Azure.Management.Databricks; using Microsoft.Azure.Management.Databricks.Models; using Microsoft.Rest; using Azure.Identity; using System.Threading.Tasks; public async Task<IList<Workspace>> GetDatabricksWorkspacesByResourceGroup(string subscriptionId, string resourceGroupName) { // Fetch an access token using Azure AD's default credential flow var credential = new DefaultAzureCredential(); var token = await credential.GetTokenAsync(new Azure.Core.TokenRequestContext(new[] { "https://management.azure.com/.default" })); var tokenCredentials = new TokenCredentials(token.Token); // Initialize the Databricks management client var databricksClient = new DatabricksManagementClient(tokenCredentials) { SubscriptionId = subscriptionId }; // List all workspaces in the target resource group var workspaceList = await databricksClient.Workspaces.ListByResourceGroupAsync(resourceGroupName); return workspaceList; }
This is the preferred approach if it works, as the SDK handles token refresh, response deserialization, and error handling automatically.
If the SDK Doesn’t Have the Method: Use HttpClient Directly
If you can’t find the ListByResourceGroupAsync method (maybe the SDK version is outdated or the operation hasn’t been wrapped yet), calling the REST API directly with HttpClient is a totally valid workaround. Here’s how to do it properly with Azure AD authentication:
First, install the required NuGet packages:
Install-Package Azure.Identity Install-Package System.Text.Json
Then implement the API call:
using Azure.Identity; using System.Net.Http; using System.Net.Http.Headers; using System.Text.Json; using System.Threading.Tasks; using System.Collections.Generic; public async Task<List<Workspace>> GetWorkspacesViaRestApi(string subscriptionId, string resourceGroupName) { // Authenticate and retrieve an access token var credential = new DefaultAzureCredential(); var token = await credential.GetTokenAsync(new Azure.Core.TokenRequestContext(new[] { "https://management.azure.com/.default" })); // Set up HttpClient with authorization header using var httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Token); // Construct the full API URL var apiUrl = $"https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Databricks/workspaces?api-version=2018-04-01"; // Send the GET request and validate success var response = await httpClient.GetAsync(apiUrl); response.EnsureSuccessStatusCode(); // Throws an exception if the request fails // Deserialize the JSON response into a strongly typed object var responseContent = await response.Content.ReadAsStringAsync(); var workspaceResponse = JsonSerializer.Deserialize<WorkspaceListResponse>(responseContent); return workspaceResponse.Value; } // Helper classes to match the structure of the API response public class WorkspaceListResponse { public List<Workspace> Value { get; set; } } public class Workspace { public string Id { get; set; } public string Name { get; set; } public string Type { get; set; } public WorkspaceProperties Properties { get; set; } } public class WorkspaceProperties { public string ProvisioningState { get; set; } public string WorkspaceUrl { get; set; } // Add any other properties you need from the API response here }
Key Notes
- Use
DefaultAzureCredentialfor authentication—it seamlessly handles local development, Azure App Service identities, VM managed identities, and more without hardcoding credentials. - When using
HttpClient, reuse instances (or useIHttpClientFactoryin ASP.NET Core) to avoid socket exhaustion issues. - Double-check the API version (
2018-04-01) to ensure it’s still valid—you can find the latest supported version in Azure’s official REST API documentation.
内容的提问来源于stack exchange,提问作者Zack ISSOIR

