1
1
---
2
2
title : " Quickstart: Your first .NET Core query"
3
3
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
5
5
ms.topic : quickstart
6
6
ms.custom : devx-track-csharp
7
7
ms.author : timwarner
8
8
---
9
9
# Quickstart: Run your first Resource Graph query using .NET Core
10
10
11
+ > [ !NOTE]
12
+ > Special thanks to [ Glenn Block] ( https://github.com/glennblock ) for contributing
13
+ > the code used in this quickstart.
14
+
11
15
The first step to using Azure Resource Graph is to check that the required packages for .NET Core
12
16
are installed. This quickstart walks you through the process of adding the packages to your .NET
13
17
Core installation.
@@ -48,43 +52,41 @@ required packages.
48
52
dotnet add package Microsoft.Azure.Services.AppAuthentication --version 1.5.0
49
53
```
50
54
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
+ ```
88
90
89
91
> [ !NOTE]
90
92
> 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
104
106
tenant-based Resource Graph query. The query returns the first five Azure resources with the
105
107
** Name** and ** Resource Type** of each resource.
106
108
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
108
110
values:
109
111
110
112
- ` {tenantId} ` - Replace with your tenant ID
111
113
- ` {clientId} ` - Replace with the client ID of your service principal
112
114
- ` {clientSecret} ` - Replace with the client secret of your service principal
113
115
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.
115
117
116
118
1 . Run your first Azure Resource Graph query using the compiled .NET Core console application:
117
119
@@ -121,7 +123,7 @@ values:
121
123
122
124
> [ !NOTE]
123
125
> 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.
125
127
126
128
1 . Change the final parameter to ` argQuery.exe ` and change the query to ` order by ` the ** Name**
127
129
property:
0 commit comments