About This Page
This page is part of the Azure documentation. It contains code examples and configuration instructions for working with Azure services.
Bias Analysis
Bias Types:
⚠️
missing_linux_example
⚠️
windows_first
Summary:
The tutorial uses generic 'command window' language and presents all command-line examples in a way that works on both Windows and Linux (e.g., 'dotnet' commands). However, it implicitly assumes a Windows-first perspective by referencing Visual Studio-specific tips and not mentioning Linux-specific editors or shell environments. There are no explicit Linux or macOS examples, nor are there any references to common Linux tools or editors. The only editor called out by name is Visual Studio, and the only tip for file management is specific to Visual Studio, which is primarily a Windows tool. There are no explicit PowerShell or Windows command prompt commands, but the lack of Linux-specific guidance or parity tips is a subtle bias.
Recommendations:
- Include explicit references to Linux and macOS environments, such as mentioning common editors (e.g., VS Code, Vim, nano) and shell environments (e.g., Bash, Zsh).
- Provide Linux/macOS-specific tips where appropriate, such as how to copy files or set file properties using common Linux tools.
- When giving Visual Studio-specific tips, also provide equivalent instructions for cross-platform editors like VS Code or JetBrains Rider.
- Clarify that all 'dotnet' CLI commands work on Linux, macOS, and Windows, and show example terminal prompts for each platform where relevant.
- Add a section or callout confirming that the tutorial has been tested on Linux and macOS, and note any platform-specific caveats.
Create pull request
Flagged Code Snippets
This command restores the dependencies on first run, and then executes the program.
* If no error occurs, the program prints: "Service client created - ready to go".
* Since there isn't yet any error handling in this project, if there are any issues, you see an exception thrown by the code.
[!INCLUDE [Azure Digital Twins: DefaultAzureCredential known issue note](includes/digital-twins-defaultazurecredential-note.md)]
### Upload a model
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)*.
The first step in creating an Azure Digital Twins solution is defining at least one model in a DTDL file.
In the directory where you created your project, create a new .json file called *SampleModel.json*. Paste in the following file body:
:::code language="json" source="~/digital-twins-docs-samples/models/SampleModel.json":::
> [!TIP]
> 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.
> [!TIP]
> 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).
Next, add some more code to *Program.cs* to upload the model you created into your Azure Digital Twins instance.
First, add a few `using` statements to the top of the file:
:::code language="csharp" source="~/digital-twins-docs-samples/sdks/csharp/fullClientApp.cs" id="Model_dependencies":::
Next, prepare to use the asynchronous methods in the C# service SDK, by changing the `Main` method signature to allow for async execution.
:::code language="csharp" source="~/digital-twins-docs-samples/sdks/csharp/fullClientApp.cs" id="Async_signature":::
> [!NOTE]
> Using `async` isn't strictly required, as the SDK also provides synchronous versions of all calls. This tutorial practices using `async`.
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.
Paste in the following code under the authorization code you added earlier.
:::code language="csharp" source="~/digital-twins-docs-samples/sdks/csharp/fullClientApp_excerpt_model.cs" id="ClientExcerptModel":::
In your command window, run the program with this command:
## Get started with project code
In this section, you begin writing the code for your new app project to work with Azure Digital Twins. The actions covered include:
* Authenticating against the service
* Uploading a model
* Catching errors
* Creating digital twins
* Creating relationships
* Querying digital twins
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.
To begin, open the file *Program.cs* in any code editor. You see a minimal code template that looks something like this:
:::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":::
First, add some `using` lines at the top of the code to pull in necessary dependencies.
:::code language="csharp" source="~/digital-twins-docs-samples/sdks/csharp/fullClientApp.cs" id="Azure_Digital_Twins_dependencies":::
Next, you add code to this file to fill out some functionality.
### Authenticate against the service
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.
To authenticate, you need the host name of your Azure Digital Twins instance.
In *Program.cs*, paste the following code below the "Hello, World!" print line in the `Main` method.
Set the value of `adtInstanceUrl` to your Azure Digital Twins instance host name.
:::code language="csharp" source="~/digital-twins-docs-samples/sdks/csharp/fullClientApp.cs" id="Authentication_code":::
Save the file.
In your command window, run the code with this command: