.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.
Before continuing, consider some common terminology used in .NET Aspire:
- App model: A collection of resources that make up your distributed application (DistributedApplication). For a more formal definition, see 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 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.
-
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
-
Create a new project using the
dotnet new aspire-apphost
command:dotnet new aspire-apphost -n AppHost
-
Add a reference to the
Api
andMyWeatherHub
projects in the newAppHost
project:- Right-click on the
AppHost
project and selectAdd
>Reference
.- Check the
Api
andMyWeatherHub
projects and clickOK
.
- Check the
Pro Tip: In Visual Studio 2022, you can drag and drop the project onto another project to add a reference.
- Right-click on the
-
When these references are add Source Generators automatically generate the necessary code to reference the projects in the App Host.
-
In the
AppHost
project, update theProgram.cs
file, adding the following line immediately after thevar builder = DistributedApplication.CreateBuilder(args);
line:var api = builder.AddProject<Projects.Api>("api"); var web = builder.AddProject<Projects.MyWeatherHub>("myweatherhub");
-
Set the
AppHost
project as the startup project in Visual Studio by right clicking on theAppHost
and clickingSet Defaul Project
. -
If you are using Visual Studio Code open the
launch.json
and replace all of the contents with the following:{ "version": "0.2.0", "configurations": [ { "name": "Run AppHost", "type": "dotnet", "request": "launch", "projectPath": "${workspaceFolder}\\AppHost\\AppHost.csproj" } ] }
-
Run the App Host using the
Run and Debug
panel in Visual Studio Code or Visual Studio. -
The .NET Aspire Dashboard will open in your default browser and display the resources and dependencies of your application.
-
Open the weather dashboard by clicking one the Endpoint for the
MyWeatherHub
which will be https://localhost:7274. -
Notice that the
Api
andMyWeatherHub
projects are running in the same process and can communicate with each other the same was as before using configuration settings. -
Click on the
View Logs
button to see the logs from theApi
andMyWeatherHub
projects. -
Select the
Traces
tab and slect theView
on a trace where the API is being called. -
Explore the
Metrics
tab to see the metrics for theApi
andMyWeatherHub
projects.
-
Open the
Structured
tab on the dashboard. -
Set the
Level
toError
and notice that no errors appear -
On the
MyWeatherApp
website click on several different cities to generate errors. Usually 5 different cities will generate an error. -
After generating the errors, the
Structured
tab will automatically update on the dashboard and notice that the errors are displayed. -
Click on the
Trace
or theDetails
to see the error message and stack trace.