Skip to content

Commit e803215

Browse files
Merge pull request #294679 from DENKEN02MSFT/ADTFreshnessTop40_Row23
ADT freshness review - row 23
2 parents ef1cccb + a956936 commit e803215

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

articles/digital-twins/tutorial-code.md

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ titleSuffix: Azure Digital Twins
44
description: Follow this tutorial to learn how to write the minimal code for an Azure Digital Twins client app, using the .NET (C#) SDK.
55
author: baanders
66
ms.author: baanders # Microsoft employees only
7-
ms.date: 06/29/2023
7+
ms.date: 2/14/2025
88
ms.topic: tutorial
99
ms.service: azure-digital-twins
1010
ms.custom: devx-track-dotnet
@@ -53,11 +53,11 @@ Once in the project directory, create an empty .NET console app project. In the
5353
dotnet new console
5454
```
5555

56-
This command will create several files inside your directory, including one called *Program.cs* where you'll write most of your code.
56+
This command creates several files inside your directory, including one called *Program.cs* where you write most of your code.
5757

58-
Keep the command window open, as you'll continue to use it throughout the tutorial.
58+
Keep the command window open, as you continue to use it throughout the tutorial.
5959

60-
Next, add two dependencies to your project that will be needed to work with Azure Digital Twins. The first is the package for the [Azure Digital Twins SDK for .NET](/dotnet/api/overview/azure/digitaltwins.core-readme), the second provides tools to help with authentication against Azure.
60+
Next, add two dependencies to your project that are needed to work with Azure Digital Twins. The first dependency is the package for the [Azure Digital Twins SDK for .NET](/dotnet/api/overview/azure/digitaltwins.core-readme). The second dependency provides tools to help with authentication against Azure.
6161

6262
```cmd/sh
6363
dotnet add package Azure.DigitalTwins.Core
@@ -66,7 +66,7 @@ dotnet add package Azure.Identity
6666

6767
## Get started with project code
6868

69-
In this section, you'll begin writing the code for your new app project to work with Azure Digital Twins. The actions covered include:
69+
In this section, you begin writing the code for your new app project to work with Azure Digital Twins. The actions covered include:
7070
* Authenticating against the service
7171
* Uploading a model
7272
* Catching errors
@@ -76,19 +76,19 @@ In this section, you'll begin writing the code for your new app project to work
7676

7777
There's also a section showing the complete code at the end of the tutorial. You can use this section as a reference to check your program as you go.
7878

79-
To begin, open the file *Program.cs* in any code editor. You'll see a minimal code template that looks something like this:
79+
To begin, open the file *Program.cs* in any code editor. You see a minimal code template that looks something like this:
8080

8181
:::image type="content" source="media/tutorial-code/starter-template.png" alt-text="Screenshot of a snippet of sample code in a code editor." lightbox="media/tutorial-code/starter-template-large.png":::
8282

8383
First, add some `using` lines at the top of the code to pull in necessary dependencies.
8484

8585
:::code language="csharp" source="~/digital-twins-docs-samples/sdks/csharp/fullClientApp.cs" id="Azure_Digital_Twins_dependencies":::
8686

87-
Next, you'll add code to this file to fill out some functionality.
87+
Next, you add code to this file to fill out some functionality.
8888

8989
### Authenticate against the service
9090

91-
The first thing your app will need to do is authenticate against the Azure Digital Twins service. Then, you can create a service client class to access the SDK functions.
91+
The first thing your app needs to do is authenticate against the Azure Digital Twins service. Then, you can create a service client class to access the SDK functions.
9292

9393
To authenticate, you need the host name of your Azure Digital Twins instance.
9494

@@ -105,15 +105,15 @@ In your command window, run the code with this command:
105105
dotnet run
106106
```
107107

108-
This command will restore the dependencies on first run, and then execute the program.
109-
* If no error occurs, the program will print: "Service client created - ready to go".
110-
* Since there isn't yet any error handling in this project, if there are any issues, you'll see an exception thrown by the code.
108+
This command restores the dependencies on first run, and then executes the program.
109+
* If no error occurs, the program prints: "Service client created - ready to go".
110+
* Since there isn't yet any error handling in this project, if there are any issues, you see an exception thrown by the code.
111111

112112
[!INCLUDE [Azure Digital Twins: DefaultAzureCredential known issue note](../../includes/digital-twins-defaultazurecredential-note.md)]
113113

114114
### Upload a model
115115

116-
Azure Digital Twins has no intrinsic domain vocabulary. The types of elements in your environment that you can represent in Azure Digital Twins are defined by you, using *models*. [Models](concepts-models.md) are similar to classes in object-oriented programming languages; they provide user-defined templates for [digital twins](concepts-twins-graph.md) to follow and instantiate later. They're written in a JSON-like language called *Digital Twins Definition Language (DTDL)*.
116+
Azure Digital Twins has no intrinsic domain vocabulary. You use *models* to define the types of elements in your environment that you can represent in Azure Digital Twins. [Models](concepts-models.md) are similar to classes in object-oriented programming languages; they provide user-defined templates for [digital twins](concepts-twins-graph.md) to follow and instantiate later. They're written in a JSON-like language called *Digital Twins Definition Language (DTDL)*.
117117

118118
The first step in creating an Azure Digital Twins solution is defining at least one model in a DTDL file.
119119

@@ -122,12 +122,12 @@ In the directory where you created your project, create a new .json file called
122122
:::code language="json" source="~/digital-twins-docs-samples/models/SampleModel.json":::
123123

124124
> [!TIP]
125-
> If you're using Visual Studio for this tutorial, you may want to select the newly-created JSON file and set the **Copy to Output Directory** property in the Property inspector to **Copy if Newer** or **Copy Always**. This will enable Visual Studio to find the JSON file with the default path when you run the program with F5 during the rest of the tutorial.
125+
> If you're using Visual Studio for this tutorial, you might want to select the newly created JSON file and set the **Copy to Output Directory** property in the Property inspector to **Copy if Newer** or **Copy Always**. This property value enables Visual Studio to find the JSON file with the default path when you run the program with F5 during the rest of the tutorial.
126126
127127
> [!TIP]
128128
> You can check model documents to make sure the DTDL is valid using the [DTDLParser library](https://www.nuget.org/packages/DTDLParser). For more about using this library, see [Parse and validate models](how-to-parse-models.md).
129129
130-
Next, add some more code to *Program.cs* to upload the model you've created into your Azure Digital Twins instance.
130+
Next, add some more code to *Program.cs* to upload the model you created into your Azure Digital Twins instance.
131131

132132
First, add a few `using` statements to the top of the file:
133133

@@ -138,7 +138,7 @@ Next, prepare to use the asynchronous methods in the C# service SDK, by changing
138138
:::code language="csharp" source="~/digital-twins-docs-samples/sdks/csharp/fullClientApp.cs" id="Async_signature":::
139139

140140
> [!NOTE]
141-
> Using `async` is not strictly required, as the SDK also provides synchronous versions of all calls. This tutorial practices using `async`.
141+
> Using `async` isn't strictly required, as the SDK also provides synchronous versions of all calls. This tutorial practices using `async`.
142142
143143
Next comes the first bit of code that interacts with the Azure Digital Twins service. This code loads the DTDL file you created from your disk, and then uploads it to your Azure Digital Twins service instance.
144144

@@ -151,21 +151,21 @@ In your command window, run the program with this command:
151151
```cmd/sh
152152
dotnet run
153153
```
154-
"Upload a model" will be printed in the output, indicating that this code was reached, but there's no output yet to indicate whether the upload was successful.
154+
"Upload a model" is printed in the output to indicate that this code was reached. However, there's no output yet to indicate whether the upload was successful.
155155

156-
To add a print statement showing all models that have been successfully uploaded to the instance, add the following code right after the previous section:
156+
To add a print statement showing all models that are successfully uploaded to the instance, add the following code right after the previous section:
157157

158158
:::code language="csharp" source="~/digital-twins-docs-samples/sdks/csharp/fullClientApp.cs" id="Print_model":::
159159

160-
Before you run the program again to test this new code, recall that the last time you ran the program, you uploaded your model already. Azure Digital Twins won't let you upload the same model twice, so if you attempt to upload the same model again, the program should throw an exception.
160+
Before you run the program again to test this new code, recall that the last time you ran the program, you uploaded your model already. Azure Digital Twins doesn't let you upload the same model twice, so if you attempt to upload the same model again, the program should throw an exception.
161161

162162
With this information in mind, run the program again with this command in your command window:
163163

164164
```cmd/sh
165165
dotnet run
166166
```
167167

168-
The program should throw an exception. When you attempt to upload a model that has been uploaded already, the service returns a "bad request" error via the REST API. As a result, the Azure Digital Twins client SDK will in turn throw an exception, for every service return code other than success.
168+
The program should throw an exception. When you attempt to upload a model that is already uploaded, the service returns a "bad request" error via the REST API. As a result, the Azure Digital Twins client SDK throws an exception, for every service return code other than success.
169169

170170
The next section talks about exceptions like this and how to handle them in your code.
171171

@@ -175,27 +175,27 @@ To keep the program from crashing, you can add exception code around the model u
175175

176176
:::code language="csharp" source="~/digital-twins-docs-samples/sdks/csharp/fullClientApp.cs" id="Model_try_catch":::
177177

178-
Run the program again with `dotnet run` in your command window. You'll see that you get back more details about the model upload issue, including an error code stating that `ModelIdAlreadyExists`.
178+
Run the program again with `dotnet run` in your command window. You see that you get back more details about the model upload issue, including an error code stating that `ModelIdAlreadyExists`.
179179

180-
From this point forward, the tutorial will wrap all calls to service methods in try/catch handlers.
180+
From this point forward, the tutorial wraps all calls to service methods in try/catch handlers.
181181

182182
### Create digital twins
183183

184-
Now that you've uploaded a model to Azure Digital Twins, you can use this model definition to create *digital twins*. [Digital twins](concepts-twins-graph.md) are instances of a model, and represent the entities within your business environment—things like sensors on a farm, rooms in a building, or lights in a car. This section creates a few digital twins based on the model you uploaded earlier.
184+
Now that you uploaded a model to Azure Digital Twins, you can use this model definition to create *digital twins*. [Digital twins](concepts-twins-graph.md) are instances of a model, and represent the entities within your business environment—things like sensors on a farm, rooms in a building, or lights in a car. This section creates a few digital twins based on the model you uploaded earlier.
185185

186-
Add the following code to the end of the `Main` method to create and initialize three digital twins based on this model.
186+
To create and initialize three digital twins based on this model, add the following code to the end of the `Main` method.
187187

188188
:::code language="csharp" source="~/digital-twins-docs-samples/sdks/csharp/fullClientApp.cs" id="Initialize_twins":::
189189

190190
In your command window, run the program with `dotnet run`. In the output, look for the print messages that sampleTwin-0, sampleTwin-1, and sampleTwin-2 were created.
191191

192192
Then, run the program again.
193193

194-
Notice that no error is thrown when the twins are created the second time, even though the twins already exist after the first run. Unlike model creation, twin creation is, on the REST level, a *PUT* call with *upsert* semantics. Using this kind of REST call means that if a twin already exists, an attempt to create the same twin again will just replace the original twin. No error is thrown.
194+
Notice that no error is thrown when the twins are created the second time, even though the twins already exist after the first run. Unlike model creation, twin creation is a *PUT* call with *upsert* semantics at the REST level. Using this kind of REST call means that if a twin already exists, an attempt to create the same twin again just replaces the original twin. No error is thrown.
195195

196196
### Create relationships
197197

198-
Next, you can create *relationships* between the twins you've created, to connect them into a *twin graph*. [Twin graphs](concepts-twins-graph.md) are used to represent your entire environment.
198+
Next, you can create *relationships* between the twins you created, to connect them into a *twin graph*. [Twin graphs](concepts-twins-graph.md) are used to represent your entire environment.
199199

200200
Add a new static method to the `Program` class, underneath the `Main` method (the code now has two methods):
201201

@@ -207,11 +207,11 @@ Next, add the following code to the end of the `Main` method, to call the `Creat
207207

208208
In your command window, run the program with `dotnet run`. In the output, look for print statements saying that the two relationships were created successfully.
209209

210-
Azure Digital Twins won't let you create a relationship if another relationship with the same ID already exists—so if you run the program multiple times, you'll see exceptions on relationship creation. This code catches the exceptions and ignores them.
210+
Azure Digital Twins doesn't let you create a relationship if another relationship with the same ID already exists. As a result, you see exceptions on relationship creation if you run the program multiple times. This code catches the exceptions and ignores them.
211211

212212
### List relationships
213213

214-
The next code you'll add allows you to see the list of relationships you've created.
214+
The next code you'll add allows you to see the list of relationships you created.
215215

216216
Add the following new method to the `Program` class:
217217

@@ -221,7 +221,7 @@ Then, add the following code to the end of the `Main` method to call the `ListRe
221221

222222
:::code language="csharp" source="~/digital-twins-docs-samples/sdks/csharp/fullClientApp.cs" id="Use_list_relationships":::
223223

224-
In your command window, run the program with `dotnet run`. You should see a list of all the relationships you've created in an output statement that looks like this:
224+
In your command window, run the program with `dotnet run`. You should see a list of all the relationships you created in an output statement that looks like this:
225225

226226
:::image type="content" source= "media/tutorial-code/list-relationships.png" alt-text="Screenshot of a console showing the program output, which results in a message that lists the twin relationships." lightbox="media/tutorial-code/list-relationships.png":::
227227

@@ -245,7 +245,7 @@ In your command window, run the program with `dotnet run`. You should see all th
245245

246246
## Complete code example
247247

248-
At this point in the tutorial, you have a complete client app that can perform basic actions against Azure Digital Twins. For reference, the full code of the program in *Program.cs* is listed below:
248+
At this point in the tutorial, you have a complete client app that can perform basic actions against Azure Digital Twins. For reference, the following example lists the full code of the program in *Program.cs*:
249249

250250
:::code language="csharp" source="~/digital-twins-docs-samples/sdks/csharp/fullClientApp.cs":::
251251

@@ -259,7 +259,7 @@ After completing this tutorial, you can choose which resources you want to remov
259259

260260
[!INCLUDE [digital-twins-cleanup-basic.md](../../includes/digital-twins-cleanup-basic.md)]
261261

262-
You may also want to delete the project folder from your local machine.
262+
You might also want to delete the project folder from your local machine.
263263

264264
## Next steps
265265

0 commit comments

Comments
 (0)