.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). For a more formal definition, see 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 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
.Pro Tip: In Visual Studio 2022, you can drag and drop the project onto another project to add a reference.
-
-
When these references are added, helper classes are automatically generated to help add them to the app model 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 Default 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 page by clicking the Endpoint for the
MyWeatherHub
project resource which will be https://localhost:7274. -
Notice that both the
Api
andMyWeatherHub
projects are running and can communicate with each other the same way as before using configuration settings. -
Back on the Aspire Dashboard, click on the
View Logs
button to see the console logs from theApi
andMyWeatherHub
projects. -
Select the
Traces
tab and selectView
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, clicking on 5 different cities will generate at least one error. -
After generating the errors, the
Structured
tab will automatically update on the dashboard and display the errors. -
Click on the
Trace
or theDetails
to see the error message and stack trace.