Skip to content

Commit 8b301cd

Browse files
authored
Merge pull request #224392 from timwarner-msft/timwarner-dotnetnew
Update quickstart to use new packages and APIs
2 parents 5f792f4 + aaf7442 commit 8b301cd

File tree

1 file changed

+43
-41
lines changed

1 file changed

+43
-41
lines changed

articles/governance/resource-graph/first-query-dotnet.md

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
---
22
title: "Quickstart: Your first .NET Core query"
33
description: In this quickstart, you follow the steps to enable the Resource Graph NuGet packages for .NET Core and run your first query.
4-
ms.date: 01/06/2023
4+
ms.date: 01/19/2023
55
ms.topic: quickstart
66
ms.custom: devx-track-csharp
77
ms.author: timwarner
88
---
99
# Quickstart: Run your first Resource Graph query using .NET Core
1010

11+
> [!NOTE]
12+
> Special thanks to [Glenn Block](https://github.com/glennblock) for contributing
13+
> the code used in this quickstart.
14+
1115
The first step to using Azure Resource Graph is to check that the required packages for .NET Core
1216
are installed. This quickstart walks you through the process of adding the packages to your .NET
1317
Core installation.
@@ -48,43 +52,41 @@ required packages.
4852
dotnet add package Microsoft.Azure.Services.AppAuthentication --version 1.5.0
4953
```
5054

51-
1. Replace the default `program.cs` with the following code and save the updated file:
52-
53-
```csharp
54-
using System;
55-
using System.Collections.Generic;
56-
using System.Threading.Tasks;
57-
using Microsoft.IdentityModel.Clients.ActiveDirectory;
58-
using Microsoft.Rest;
59-
using Azure.ResourceManager.ResourceGraph;
60-
using Azure.ResourceManager.ResourceGraph.Models;
61-
62-
namespace argQuery
63-
{
64-
class Program
65-
{
66-
static async Task Main(string[] args)
67-
{
68-
string strTenant = args[0];
69-
string strClientId = args[1];
70-
string strClientSecret = args[2];
71-
string strQuery = args[3];
72-
73-
AuthenticationContext authContext = new AuthenticationContext("https://login.microsoftonline.com/" + strTenant);
74-
AuthenticationResult authResult = await authContext.AcquireTokenAsync("https://management.core.windows.net", new ClientCredential(strClientId, strClientSecret));
75-
ServiceClientCredentials serviceClientCreds = new TokenCredentials(authResult.AccessToken);
76-
77-
ResourceGraphClient argClient = new ResourceGraphClient(serviceClientCreds);
78-
QueryRequest request = new QueryRequest();
79-
request.Query = strQuery;
80-
81-
QueryResponse response = argClient.Resources(request);
82-
Console.WriteLine("Records: " + response.Count);
83-
Console.WriteLine("Data:\n" + response.Data);
84-
}
85-
}
86-
}
87-
```
55+
1. Replace the default `Program.cs` with the following code and save the updated file:
56+
57+
```csharp
58+
using System;
59+
using System.Collections.Generic;
60+
using System.Threading.Tasks;
61+
using Azure.Core;
62+
using Azure.Identity;
63+
using Azure.ResourceManager;
64+
using Azure.ResourceManager.Resources;
65+
using Azure.ResourceManager.ResourceGraph;
66+
using Azure.ResourceManager.ResourceGraph.Models;
67+
68+
namespace argQuery
69+
{
70+
class Program
71+
{
72+
static async Task Main(string[] args)
73+
{
74+
string strTenant = args[0];
75+
string strClientId = args[1];
76+
string strClientSecret = args[2];
77+
string strQuery = args[3];
78+
79+
var client = new ArmClient(new ClientSecretCredential(strTenant, strClientId, strClientSecret));
80+
var tenant = client.GetTenants().First();
81+
//Console.WriteLine($"{tenant.Id} {tenant.HasData}");
82+
var queryContent = new ResourceQueryContent(strQuery);
83+
var response = tenant.GetResources(queryContent);
84+
var result = response.Value;
85+
Console.WriteLine($"Count: {result.Data.ToString()}");
86+
}
87+
}
88+
}
89+
```
8890

8991
> [!NOTE]
9092
> This code creates a tenant-based query. To limit the query to a
@@ -104,14 +106,14 @@ With the .NET Core console application built and published, it's time to try out
104106
tenant-based Resource Graph query. The query returns the first five Azure resources with the
105107
**Name** and **Resource Type** of each resource.
106108

107-
In each call to `argQuery`, there are variables that are used that you need to replace with your own
109+
In each call to `argQuery`, replace the variables with your own
108110
values:
109111

110112
- `{tenantId}` - Replace with your tenant ID
111113
- `{clientId}` - Replace with the client ID of your service principal
112114
- `{clientSecret}` - Replace with the client secret of your service principal
113115

114-
1. Change directories to the `{run-folder}` you defined with the previous `dotnet publish` command.
116+
1. Change directories to the `{run-folder}` you defined with the earlier `dotnet publish` command.
115117

116118
1. Run your first Azure Resource Graph query using the compiled .NET Core console application:
117119

@@ -121,7 +123,7 @@ values:
121123

122124
> [!NOTE]
123125
> As this query example does not provide a sort modifier such as `order by`, running this query
124-
> multiple times is likely to yield a different set of resources per request.
126+
> many times is likely to yield a different set of resources per request.
125127
126128
1. Change the final parameter to `argQuery.exe` and change the query to `order by` the **Name**
127129
property:

0 commit comments

Comments
 (0)