You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/digital-twins/tutorial-code.md
+30-30Lines changed: 30 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ titleSuffix: Azure Digital Twins
4
4
description: Follow this tutorial to learn how to write the minimal code for an Azure Digital Twins client app, using the .NET (C#) SDK.
5
5
author: baanders
6
6
ms.author: baanders # Microsoft employees only
7
-
ms.date: 06/29/2023
7
+
ms.date: 2/14/2025
8
8
ms.topic: tutorial
9
9
ms.service: azure-digital-twins
10
10
ms.custom: devx-track-dotnet
@@ -53,11 +53,11 @@ Once in the project directory, create an empty .NET console app project. In the
53
53
dotnet new console
54
54
```
55
55
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.
57
57
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.
59
59
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.
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:
70
70
* Authenticating against the service
71
71
* Uploading a model
72
72
* Catching errors
@@ -76,19 +76,19 @@ In this section, you'll begin writing the code for your new app project to work
76
76
77
77
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.
78
78
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:
80
80
81
81
:::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":::
82
82
83
83
First, add some `using` lines at the top of the code to pull in necessary dependencies.
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.
88
88
89
89
### Authenticate against the service
90
90
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.
92
92
93
93
To authenticate, you need the host name of your Azure Digital Twins instance.
94
94
@@ -105,15 +105,15 @@ In your command window, run the code with this command:
105
105
dotnet run
106
106
```
107
107
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.
111
111
112
112
[!INCLUDE [Azure Digital Twins: DefaultAzureCredential known issue note](../../includes/digital-twins-defaultazurecredential-note.md)]
113
113
114
114
### Upload a model
115
115
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)*.
117
117
118
118
The first step in creating an Azure Digital Twins solution is defining at least one model in a DTDL file.
119
119
@@ -122,12 +122,12 @@ In the directory where you created your project, create a new .json file called
> 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 newlycreated 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.
126
126
127
127
> [!TIP]
128
128
> 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).
129
129
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.
131
131
132
132
First, add a few `using` statements to the top of the file:
133
133
@@ -138,7 +138,7 @@ Next, prepare to use the asynchronous methods in the C# service SDK, by changing
> 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`.
142
142
143
143
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.
144
144
@@ -151,21 +151,21 @@ In your command window, run the program with this command:
151
151
```cmd/sh
152
152
dotnet run
153
153
```
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.
155
155
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:
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.
161
161
162
162
With this information in mind, run the program again with this command in your command window:
163
163
164
164
```cmd/sh
165
165
dotnet run
166
166
```
167
167
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.
169
169
170
170
The next section talks about exceptions like this and how to handle them in your code.
171
171
@@ -175,27 +175,27 @@ To keep the program from crashing, you can add exception code around the model u
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`.
179
179
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.
181
181
182
182
### Create digital twins
183
183
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.
185
185
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.
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.
191
191
192
192
Then, run the program again.
193
193
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 isa *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.
195
195
196
196
### Create relationships
197
197
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.
199
199
200
200
Add a new static method to the `Program` class, underneath the `Main` method (the code now has two methods):
201
201
@@ -207,11 +207,11 @@ Next, add the following code to the end of the `Main` method, to call the `Creat
207
207
208
208
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.
209
209
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.
211
211
212
212
### List relationships
213
213
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.
215
215
216
216
Add the following new method to the `Program` class:
217
217
@@ -221,7 +221,7 @@ Then, add the following code to the end of the `Main` method to call the `ListRe
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:
225
225
226
226
:::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":::
227
227
@@ -245,7 +245,7 @@ In your command window, run the program with `dotnet run`. You should see all th
245
245
246
246
## Complete code example
247
247
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*:
0 commit comments