Skip to content

Commit

Permalink
Updates for modules 1 through 3
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianEdwards committed Sep 6, 2024
1 parent 399fd49 commit 3fc1d13
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 106 deletions.
15 changes: 0 additions & 15 deletions start-with-api/MyWeatherHub/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:9865",
"sslPort": 44355
}
},
"profiles": {
"http": {
"commandName": "Project",
Expand All @@ -26,13 +18,6 @@
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
15 changes: 9 additions & 6 deletions workshop/1-setup.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
# Machine Setup
# Machine Setup

This workshop will be using the following tools:

- [.NET 8 SDK](https://dot.net/download)
- [.NET Aspire Workload](https://learn.microsoft.com/dotnet/aspire/fundamentals/setup-tooling?tabs=dotnet-cli%2Cunix#install-net-aspire)
- [Docker Desktop](https://docs.docker.com/engine/install/) or [Podman](https://podman.io/getting-started/installation)
- [Visual Studio 2022](https://visualstudio.microsoft.com/vs/) or [Visual Studio Code](https://code.visualstudio.com/) with [C# DevKit](https://code.visualstudio.com/docs/csharp/get-started)
- [Visual Studio 2022](https://visualstudio.microsoft.com/vs/) or [Visual Studio Code](https://code.visualstudio.com/) with [C# Dev Kit](https://code.visualstudio.com/docs/csharp/get-started)

For the best experience, we recommend using Visual Studio 2022 with the .NET Aspire workload. However, you can use Visual Studio Code with the C# Dev Kit and .NET Aspire workload. Below are setup guides for each platform.

## Windows with Visual Studio
- Install [Visual Studio 2022 version 17.10 or newer](https://visualstudio.microsoft.com/vs/).

- Install [Visual Studio 2022 version 17.11 or newer](https://visualstudio.microsoft.com/vs/).
- Any edition will work including the [free to use Visual Studio Community](https://visualstudio.microsoft.com/free-developer-offers/)
- Select the following workloads:
- `ASP.NET and web development` workload.
- `.NET Aspire SDK` component in `Individual components`.

## Mac, Linux, & Windows without Visual Studio

- Install the latest [.NET 8 SDK](https://dot.net/download?cid=eshop)
- Install the [.NET Aspire workload](https://learn.microsoft.com/dotnet/aspire/fundamentals/setup-tooling?tabs=dotnet-cli%2Cunix#install-net-aspire) with the following commands:

Expand All @@ -28,12 +31,12 @@ dotnet workload install aspire
- Install [Visual Studio Code with C# Dev Kit](https://code.visualstudio.com/docs/csharp/get-started)

> Note: When running on Mac with Apple Silicon (M series processor), Rosetta 2 for grpc-tools.
> Note: When running on Mac with Apple Silicon (M series processor), Rosetta 2 for grpc-tools.
## Test Installation

To test your installation, see the [Build your first .NET Aspire project](https://learn.microsoft.com/dotnet/aspire/get-started/build-your-first-aspire-app) for more information.

## Open Workshop Start Solution

To start the workshop open `start-with-api/MyWeatherHub.sln` in Visual Studio 2022. If you are using Visual Studio code open the `start-with-api` folder and when prompted by the C# Dev Kit which solution to open, select **MyWeatherHub.snl**.
To start the workshop open `start-with-api/MyWeatherHub.sln` in Visual Studio 2022. If you are using Visual Studio code open the `start-with-api` folder and when prompted by the C# Dev Kit which solution to open, select **MyWeatherHub.sln**.
86 changes: 41 additions & 45 deletions workshop/2-servicedefaults.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Service Defaults aka Smart Defaults

## Introduction

.NET Aspire provides a set of smart defaults for services that are commonly used in .NET applications. These defaults are designed to help you get started quickly and provide a consistent experience across different types of applications. This includes:

- Telemetry: Metrics, Tracing, Logging
Expand All @@ -13,75 +14,70 @@
### Visual Studio & Visual Studio Code

1. Add a new project to the solution called `ServiceDefaults`:
- Right-click on the solution and select `Add` > `New Project`.
- Select the `.NET Aspire Service Defaults` project template.
- Name the project `ServiceDefaults`.
- Click `Next` > `Create`.

- Right-click on the solution and select `Add` > `New Project`.
- Select the `.NET Aspire Service Defaults` project template.
- Name the project `ServiceDefaults`.
- Click `Next` > `Create`.

*Visual Studio*
![Visual Studio dialog to add a service defaults project](./media/vs-add-servicedefaults.png)

*Visual Studio Code*
![Visual Studio Code dialog to add a service defaults project](./media/vsc-add-servicedefaults.png)
*Visual Studio*
![Visual Studio dialog to add a service defaults project](./media/vs-add-servicedefaults.png)

*Visual Studio Code*
![Visual Studio Code dialog to add a service defaults project](./media/vsc-add-servicedefaults.png)

### Command Line

1. Create a new project using the `dotnet new aspire-servicedefaults` command:

```bash
dotnet new aspire-servicedefaults -n ServiceDefaults
```
```bash
dotnet new aspire-servicedefaults -n ServiceDefaults
```

## Configure Service Defaults

1. Add a reference to the `ServiceDefaults` project in the `Api` and `MyWeatherHub` projects:
- Right-click on the `Api` project and select `Add` > `Reference`.
- Check the `ServiceDefaults` project and click `OK`.
- Right-click on the `MyWeatherHub` project and select `Add` > `Reference`.
- Check the `ServiceDefaults` project and click `OK`.

- Right-click on the `Api` project and select `Add` > `Reference`.
- Check the `ServiceDefaults` project and click `OK`.
- Right-click on the `MyWeatherHub` project and select `Add` > `Reference`.
- Check the `ServiceDefaults` project and click `OK`.

> Pro Tip: In Visual Studio 2022, you can drag and drop the project onto another project to add a reference.
> Pro Tip: In Visual Studio 2022, you can drag and drop the project onto another project to add a reference.
1. In both the `Api` and `MyWeatherHub` projects, update their `Program.cs` files, adding the following line immediately after their `var builder = WebApplication.CreateBuilder(args);` line:

```csharp
builder.AddServiceDefaults();
```
1. In both the `Api` and `MyWeatherHub` projects, update their `Program.cs` files,adding the following line immediately after their `var app = builder.Build();` line:

```csharp
app.MapDefaultEndpoints();
```
```csharp
builder.AddServiceDefaults();
```

## Run the application
1. In both the `Api` and `MyWeatherHub` projects, update their `Program.cs` files,adding the following line immediately after their `var app = builder.Build();` line:

1. Run the application using a multiple-project launch configuration in Visual Studio or Visual Studio Code.
```csharp
app.MapDefaultEndpoints();
```

- Visual Studio: Right click on the `MyWeatherHub` solution and go to properties. Select the `Api` and `MyWeatherHub` as startup projects, select `OK`.
- ![Visual Studio solution properties](./media/vs-multiproject.png)
- Click `Start` to start and debug both projects.
- Visual Studio Code: Run the `Api` and `MyWeatherHub` projects using the `Run and Debug` panel. We have provided a `launch.json` file with the necessary configurations to run both.
## Run the application

1. Run the application using a multiple-project launch configuration in Visual Studio or Visual Studio Code:
- Visual Studio: Right click on the `MyWeatherHub` solution and go to properties. Select the `Api` and `MyWeatherHub` as startup projects, select `OK`.
- ![Visual Studio solution properties](./media/vs-multiproject.png)
- Click `Start` to start and debug both projects.
- Visual Studio Code: Run the `Api` and `MyWeatherHub` projects using the `Run and Debug` panel. We have provided a `launch.json` file with the necessary configurations to run both.
1. Test the application by navigating to the following URLs:

- [https://localhost:7032/swagger/index.html](https://localhost:7032/swagger/index.html) - API
- [https://localhost:7274/](https://localhost:7274/) - MyWeatherHub

- [https://localhost:7032/swagger/index.html](https://localhost:7032/swagger/index.html) - API
- [https://localhost:7274/](https://localhost:7274/) - MyWeatherHub
1. You should see the Swagger UI for the API and the MyWeatherHub home page.
1. You can also view the health checks for the API by navigating to [https://localhost:7032/health](https://localhost:7032/health).
1. You can also view the health checks for the MyWeatherHub by navigating to [https://localhost:7274/health](https://localhost:7274/health).
1. View the logs in the terminal to see the health checks and other telemetry data such as resiliency with Polly:

```bash
Polly: Information: Execution attempt. Source: '-standard//Standard-Retry', Operation Key: '', Result: '200', Handled: 'False', Attempt: '0', Execution Time: '13.0649'
```
```bash
Polly: Information: Execution attempt. Source: '-standard//Standard-Retry', Operation Key: '', Result: '200', Handled: 'False', Attempt: '0', Execution Time: '13.0649'
```

1. Click on 5 different cities and a "random" error will be thrown. You will see the Polly retry policy in action.

```bash
Polly: Warning: Execution attempt. Source: '-standard//Standard-Retry', Operation Key: '', Result: '500', Handled: 'True', Attempt: '0', Execution Time: '9732.8258'
Polly: Warning: Resilience event occurred. EventName: 'OnRetry', Source: '-standard//Standard-Retry', Operation Key: '', Result: '500'
System.Net.Http.HttpClient.NwsManager.ClientHandler: Information: Sending HTTP request GET http://localhost:5271/forecast/AKZ318
```
```bash
Polly: Warning: Execution attempt. Source: '-standard//Standard-Retry', Operation Key: '', Result: '500', Handled: 'True', Attempt: '0', Execution Time: '9732.8258'
Polly: Warning: Resilience event occurred. EventName: 'OnRetry', Source: '-standard//Standard-Retry', Operation Key: '', Result: '500'
System.Net.Http.HttpClient.NwsManager.ClientHandler: Information: Sending HTTP request GET http://localhost:5271/forecast/AKZ318
```
80 changes: 40 additions & 40 deletions workshop/3-dashboard-apphost.md
Original file line number Diff line number Diff line change
@@ -1,67 +1,65 @@
# Dashboard & Orchestration with .NET Aspire App Host

.NET Aspire provides APIs for expressing resources and dependencies within your distributed application. In addition to these APIs, there's tooling that enables some compelling scenarios. The orchestrator is intended for local development purposes.
.NET Aspire provides APIs for modeling resources and dependencies within your distributed application. In addition to these APIs, there's tooling that enables some compelling scenarios. The orchestrator is intended for local development purposes.

Before continuing, consider some common terminology used in .NET Aspire:

* *App model*: A collection of resources that make up your distributed application ([DistributedApplication](https://learn.microsoft.com/dotnet/api/aspire.hosting.distributedapplication)). For a more formal definition, see [Define the app model](https://learn.microsoft.com/dotnet/aspire/fundamentals/app-host-overview?tabs=docker#define-the-app-model).
* *App host/Orchestrator project*: The .NET project that orchestrates the *app model*, named with the **.AppHost* suffix (by convention).
* *Resource*: A [resource](https://learn.microsoft.com/dotnet/aspire/fundamentals/app-host-overview?tabs=docker#built-in-resource-types) represents a part of an application whether it be a .NET project, container, or executable, or some other resource like a database, cache, or cloud service (such as a storage service).
* *Reference*: A reference defines a connection between resources, expressed as a dependency using the `WithReference` API. For more information, see [Reference resources](https://learn.microsoft.com/dotnet/aspire/fundamentals/app-host-overview?tabs=docker#reference-resources).

- *App model*: A collection of resources that make up your distributed application ([DistributedApplication](https://learn.microsoft.com/dotnet/api/aspire.hosting.distributedapplication)). For a more formal definition, see [Define the app model](https://learn.microsoft.com/dotnet/aspire/fundamentals/app-host-overview?tabs=docker#define-the-app-model).
- *App host/Orchestrator project*: The .NET project that defines and orchestrates the *app model*, named with the **.AppHost* suffix (by convention).
- *Resource*: A [resource](https://learn.microsoft.com/dotnet/aspire/fundamentals/app-host-overview?tabs=docker#built-in-resource-types) represents a part of an application whether it be a .NET project, container, or executable, or some other resource like a database, cache, or cloud service (such as a storage service).
- *Reference*: A reference defines a connection between resources, expressed as a dependency using the `WithReference` API. For more information, see [Reference resources](https://learn.microsoft.com/dotnet/aspire/fundamentals/app-host-overview?tabs=docker#reference-resources).

## Create App Host Project

### Visual Studio & Visual Studio Code

1. Add a new project to the solution called `AppHost`:
- Right-click on the solution and select `Add` > `New Project`.
- Select the `.NET Aspire App Host` project template.
- Name the project `AppHost`.
- Click `Next` > `Create`.

- Right-click on the solution and select `Add` > `New Project`.
- Select the `.NET Aspire App Host` project template.
- Name the project `AppHost`.
- Click `Next` > `Create`.

*Visual Studio*
![Visual Studio dialog to add a app host project](./media/vs-add-apphost.png)

*Visual Studio Code*
![Visual Studio Code dialog to add a app host project](./media/vsc-add-apphost.png)
*Visual Studio*
![Visual Studio dialog to add a app host project](./media/vs-add-apphost.png)

*Visual Studio Code*
![Visual Studio Code dialog to add a app host project](./media/vsc-add-apphost.png)

### Command Line

1. Create a new project using the `dotnet new aspire-apphost` command:

```bash
dotnet new aspire-apphost -n AppHost
```
```bash
dotnet new aspire-apphost -n AppHost
```

## Add Project References

1. Add a reference to the `Api` and `MyWeatherHub` projects in the new `AppHost` project:
- Right-click on the `AppHost` project and select `Add` > `Reference`.
- Check the `Api` and `MyWeatherHub` projects and click `OK`.

- Right-click on the `AppHost` project and select `Add` > `Reference`.
- Check the `Api` and `MyWeatherHub` projects and click `OK`.
> Pro Tip: In Visual Studio 2022, you can drag and drop the project onto another project to add a reference.

> Pro Tip: In Visual Studio 2022, you can drag and drop the project onto another project to add a reference.
1. When these references are added Source Generators automatically generate the necessary code to reference the projects in the App Host.
1. When these references are added, helper classes are automatically generated to help add them to the app model in the App Host.

## Orchestrate the Application

1. In the `AppHost` project, update the `Program.cs` file, adding the following line immediately after the `var builder = DistributedApplication.CreateBuilder(args);` line:

```csharp
var api = builder.AddProject<Projects.Api>("api");
```csharp
var api = builder.AddProject<Projects.Api>("api");
var web = builder.AddProject<Projects.MyWeatherHub>("myweatherhub");
```
var web = builder.AddProject<Projects.MyWeatherHub>("myweatherhub");
```

## Run the application

1. Set the `AppHost` project as the startup project in Visual Studio by right clicking on the `AppHost` and clicking `Set Default Project`.
1. If you are using Visual Studio Code open the `launch.json` and replace all of the contents with the following:
```json
{

```json
{
"version": "0.2.0",
"configurations": [
{
Expand All @@ -72,29 +70,31 @@ Before continuing, consider some common terminology used in .NET Aspire:
}
]
}
```
```

1. Run the App Host using the `Run and Debug` panel in Visual Studio Code or Visual Studio.
1. The .NET Aspire Dashboard will open in your default browser and display the resources and dependencies of your application.

![.NET Aspire Dashboard](./media/dashboard.png)
![.NET Aspire Dashboard](./media/dashboard.png)

1. Open the weather dashboard by clicking one the Endpoint for the `MyWeatherHub` which will be [https://localhost:7274](https://localhost:7274).
1. Notice that the `Api` and `MyWeatherHub` projects are running in the same process and can communicate with each other the same was as before using configuration settings.
1. Click on the `View Logs` button to see the logs from the `Api` and `MyWeatherHub` projects.
1. Select the `Traces` tab and slect the `View` on a trace where the API is being called.
1. Open the weather page by clicking the Endpoint for the `MyWeatherHub` project resource which will be [https://localhost:7274](https://localhost:7274).
1. Notice that both the `Api` and `MyWeatherHub` projects are running and can communicate with each other the same way as before using configuration settings.
1. Back on the Aspire Dashboard, click on the `View Logs` button to see the console logs from the `Api` and `MyWeatherHub` projects.
1. Select the `Traces` tab and select `View` on a trace where the API is being called.

![.NET Aspire Dashboard](./media/dashboard-trace.png)]
![.NET Aspire Dashboard](./media/dashboard-trace.png)]

1. Explore the `Metrics` tab to see the metrics for the `Api` and `MyWeatherHub` projects.

![.NET Aspire Dashboard](./media/dashboard-metrics.png)
![.NET Aspire Dashboard](./media/dashboard-metrics.png)

## Create an error

1. Open the `Structured` tab on the dashboard.
1. Set the `Level` to `Error` and notice that no errors appear
1. On the `MyWeatherApp` website click on several different cities to generate errors. Usually 5 different cities will generate an error.
1. After generating the errors, the `Structured` tab will automatically update on the dashboard and notice that the errors are displayed.
1. On the `MyWeatherApp` website, click on several different cities to generate errors. Usually, clicking on 5 different cities will generate at least one error.
1. After generating the errors, the `Structured` tab will automatically update on the dashboard and display the errors.

![.NET Aspire Dashboard](./media/dashboard-error.png)

![.NET Aspire Dashboard](./media/dashboard-error.png)
1. Click on the `Trace` or the `Details` to see the error message and stack trace.

0 comments on commit 3fc1d13

Please sign in to comment.