Skip to content

Commit b888456

Browse files
authored
Merge pull request #224619 from scottaddie/scottaddie/update-dotnet-quickstart
Update .NET quickstart for Resource Graph
2 parents f3611e8 + 53b9f27 commit b888456

File tree

1 file changed

+39
-61
lines changed

1 file changed

+39
-61
lines changed

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

Lines changed: 39 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,71 @@
11
---
2-
title: "Quickstart: Your first .NET Core query"
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/19/2023
2+
title: "Quickstart: Your first .NET query"
3+
description: In this quickstart, you follow the steps to enable the Resource Graph NuGet packages for .NET and run your first query.
4+
ms.date: 01/20/2023
55
ms.topic: quickstart
66
ms.custom: devx-track-csharp
77
ms.author: timwarner
88
---
9-
# Quickstart: Run your first Resource Graph query using .NET Core
9+
# Quickstart: Run your first Resource Graph query using .NET
1010

1111
> [!NOTE]
1212
> Special thanks to [Glenn Block](https://github.com/glennblock) for contributing
1313
> the code used in this quickstart.
1414
15-
The first step to using Azure Resource Graph is to check that the required packages for .NET Core
16-
are installed. This quickstart walks you through the process of adding the packages to your .NET
17-
Core installation.
15+
The first step to using Azure Resource Graph is to check that the required NuGet packages are installed. This quickstart walks you through the process of adding the packages to your .NET application.
1816

19-
At the end of this process, you'll have added the packages to your .NET Core installation and run
20-
your first Resource Graph query.
17+
At the end of this process, you'll have added the packages to your .NET application and run your first Resource Graph query.
2118

2219
## Prerequisites
2320

21+
- [.NET SDK 6.0 or later](https://dotnet.microsoft.com/download/dotnet)
2422
- An Azure subscription. If you don't have an Azure subscription, create a
25-
[free](https://azure.microsoft.com/free/) account before you begin.
23+
[free](https://azure.microsoft.com/free/dotnet/) account before you begin.
2624
- An Azure service principal, including the _clientId_ and _clientSecret_. If you don't have a
2725
service principal for use with Resource Graph or want to create a new one, see
2826
[Azure management libraries for .NET authentication](/dotnet/azure/sdk/authentication#mgmt-auth).
29-
Skip the step to install the .NET Core packages as we'll do that in the next steps.
27+
Skip the step to install the NuGet packages, as we'll do that in the next steps.
3028

3129
## Create the Resource Graph project
3230

33-
To enable .NET Core to query Azure Resource Graph, create a new console application and install the
31+
To enable .NET to query Azure Resource Graph, create a new console application and install the
3432
required packages.
3533

36-
1. Check that the latest .NET Core is installed (at least **3.1.5**). If it isn't yet installed,
37-
download it at [dotnet.microsoft.com](https://dotnet.microsoft.com/download/dotnet-core).
38-
39-
1. Initialize a new .NET Core console application named "argQuery":
34+
1. Create a new .NET console application named "argQuery":
4035

4136
```dotnetcli
4237
dotnet new console --name "argQuery"
4338
```
4439

45-
1. Change directories into the new project folder and install the required packages for Azure Resource Graph:
40+
1. Change directories into the new project folder. Install the packages for the Azure Resource Graph and Azure Identity client libraries:
4641

4742
```dotnetcli
48-
# Add the Resource Graph package for .NET Core
49-
dotnet add package Azure.ResourceManager.ResourceGraph --version 1.0.0
50-
51-
# Add the Azure app auth package for .NET Core
52-
dotnet add package Microsoft.Azure.Services.AppAuthentication --version 1.5.0
43+
dotnet add package Azure.ResourceManager.ResourceGraph
44+
dotnet add package Azure.Identity
5345
```
5446

5547
1. Replace the default `Program.cs` with the following code and save the updated file:
5648

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-
```
49+
```csharp
50+
using Azure.Identity;
51+
using Azure.ResourceManager;
52+
using Azure.ResourceManager.ResourceGraph;
53+
using Azure.ResourceManager.ResourceGraph.Models;
54+
55+
string strTenant = args[0];
56+
string strClientId = args[1];
57+
string strClientSecret = args[2];
58+
string strQuery = args[3];
59+
60+
var client = new ArmClient(
61+
new ClientSecretCredential(strTenant, strClientId, strClientSecret));
62+
var tenant = client.GetTenants().First();
63+
//Console.WriteLine($"{tenant.Id} {tenant.HasData}");
64+
var queryContent = new ResourceQueryContent(strQuery);
65+
var response = tenant.GetResources(queryContent);
66+
var result = response.Value;
67+
Console.WriteLine($"Count: {result.Data.ToString()}");
68+
```
9069

9170
> [!NOTE]
9271
> This code creates a tenant-based query. To limit the query to a
@@ -102,20 +81,19 @@ namespace argQuery
10281

10382
## Run your first Resource Graph query
10483

105-
With the .NET Core console application built and published, it's time to try out a simple
84+
With the .NET console application built and published, it's time to try out a simple
10685
tenant-based Resource Graph query. The query returns the first five Azure resources with the
10786
**Name** and **Resource Type** of each resource.
10887

109-
In each call to `argQuery`, replace the variables with your own
110-
values:
88+
In each call to `argQuery`, replace the variables with your own values:
11189

11290
- `{tenantId}` - Replace with your tenant ID
11391
- `{clientId}` - Replace with the client ID of your service principal
11492
- `{clientSecret}` - Replace with the client secret of your service principal
11593

11694
1. Change directories to the `{run-folder}` you defined with the earlier `dotnet publish` command.
11795

118-
1. Run your first Azure Resource Graph query using the compiled .NET Core console application:
96+
1. Run your first Azure Resource Graph query using the compiled .NET console application:
11997

12098
```bash
12199
argQuery "{tenantId}" "{clientId}" "{clientSecret}" "Resources | project name, type | limit 5"
@@ -151,12 +129,12 @@ top five results.
151129

152130
## Clean up resources
153131

154-
If you wish to remove the .NET Core console application and installed packages, you can do so by
132+
If you wish to remove the .NET console application and installed packages, you can do so by
155133
deleting the `argQuery` project folder.
156134

157135
## Next steps
158136

159-
In this quickstart, you've created a .NET Core console application with the required Resource Graph
137+
In this quickstart, you've created a .NET console application with the required Resource Graph
160138
packages and run your first query. To learn more about the Resource Graph language, continue to the
161139
query language details page.
162140

0 commit comments

Comments
 (0)