If you have existing microservices and .NET web app, you can add .NET Aspire to it and get all the included features and benefits. In this article, you add .NET Aspire orchestration to a simple, preexisting .NET 9 project. You learn how to:
Understand the structure of the existing microservices app.
Enroll existing projects in .NET Aspire orchestration.
Understand the changes enrollment makes in the projects.
Start the .NET Aspire project.
Prerequisites
To work with .NET Aspire, you need the following installed locally:
To start debugging the solution, press F5 or select Start.
Two pages open in the browser:
A page displays products in JSON format from a call to the Products Web API.
A page displays the homepage of the website. In the menu on the left, select Products to see the catalog obtained from the Web API.
To stop debugging, close the browser, and then select the Stop button twice (once for each running debug instance).
Open a terminal window and change directories into the newly cloned repository.
To start the Products app, run the following command:
.NET CLI
dotnetrun --project ./Products/Products.csproj
A browser page opens, displaying the JSON for the products.
In a separate terminal window, again change directories to cloned repository.
Start the Store app by running the following command:
.NET CLI
dotnetrun --project ./Store/Store.csproj
The browser opens a page that displays the homepage of the website. In the menu on the left, select Products to see the catalog obtained from the Web API.
To stop debugging, close the browser, and press Ctrl+C in both terminals.
Add .NET Aspire to the Store web app
Now, let's enroll the Store project, which implements the web user interface, in .NET Aspire orchestration:
In Visual Studio, in the Solution Explorer, right-click the Store project, select Add, and then select .NET Aspire Orchestrator Support.
In the Add .NET Aspire Orchestrator Support dialog, select OK.
You should now have two new projects, both added to the solution:
eShopLite.AppHost: An orchestrator project designed to connect and configure the different projects and services of your app. The orchestrator is set as the Startup project, and it depends on the eShopLite.Store project.
eShopLite.ServiceDefaults: A .NET Aspire shared project to manage configurations that are reused across the projects in your solution related to resilience, service discovery, and telemetry.
In the eShopLite.AppHost project, open the Program.cs file. Notice this line of code, which registers the Store project in the .NET Aspire orchestration:
In Visual Studio, in the Solution Explorer, right-click the Products project, select Add, and then select .NET Aspire Orchestrator Support.
A dialog indicating that .NET Aspire Orchestrator project already exists, select OK.
In the eShopLite.AppHost project, open the Program.cs file. Notice this line of code, which registers the Products project in the .NET Aspire orchestration:
Also notice that the eShopLite.AppHost project, now depends on both the Store and Products projects.
Create an app host project
In order to orchestrate the existing projects, you need to create a new app host project. To create a new app host project from the available .NET Aspire templates, use the following .NET CLI command:
After the app host project is created, you need to create a new service defaults project. To create a new service defaults project from the available .NET Aspire templates, use the following .NET CLI command:
Both the Store and Products projects need to reference the service defaults project so that they can easily include service discovery. To add a reference to the service defaults project in the Store project, use the following .NET CLI command:
In both the Store and Products projects, update their Program.cs files, adding the following line immediately after their var builder = WebApplication.CreateBuilder(args); line:
C#
builder.AddServiceDefaults();
Update the app host project
Open the Program.cs file of the app host project, and replace its contents with the following C# code:
C#
var builder = DistributedApplication.CreateBuilder(args);
builder.AddProject<Projects.Store>("store");
builder.AddProject<Projects.Products>("products");
builder.Build().Run();
The preceding code:
Creates a new DistributedApplicationBuilder instance.
Adds the Store project to the orchestrator.
Adds the Products project to the orchestrator.
Builds and runs the orchestrator.
Service Discovery
At this point, both projects are part of .NET Aspire orchestration, but the Store needs to be able to discover the Products backend address through .NET Aspire's service discovery. To enable service discovery, open the Program.cs file in eShopLite.AppHost and update the code that the Store adds a reference to the Products project:
C#
var builder = DistributedApplication.CreateBuilder(args);
var products = builder.AddProject<Projects.Products>("products");
builder.AddProject<Projects.Store>("store")
.WithExternalHttpEndpoints()
.WithReference(products);
builder.Build().Run();
The preceding code expresses that the Store project depends on the Products project. For more information, see .NET Aspire app host: Reference resources. This reference is used to discover the address of the Products project. Additionally, the Store project is configured to use external HTTP endpoints. If you later choose to deploy this app, you'd need the call to WithExternalHttpEndpoints to ensure that it's public to the outside world.
Next, update the appsettings.json in the Store project with the following JSON:
The addresses for both the endpoints now uses the "products" name that was added to the orchestrator in the app host. These names are used to discover the address of the Products project.
Explore the enrolled app
Let's start the solution and examine the new behavior that .NET Aspire provides.
Piezīme
Notice that the eShopLite.AppHost project is the new startup project.
In Visual Studio, to start debugging, press F5 Visual Studio builds the projects.
If the Start Docker Desktop dialog appears, select Yes. Visual Studio starts the Docker engine and creates the necessary containers. When the deployment is complete, the .NET Aspire dashboard is displayed.
In the dashboard, select the endpoint for the products project. A new browser tab appears and displays the product catalog in JSON format.
In the dashboard, select the endpoint for the store project. A new browser tab appears and displays the home page for the web app.
In the menu on the left, select Products. The product catalog is displayed.
To stop debugging, close the browser.
Delete the launch.json file that you created earlier, it no longer serves a purpose. Instead, start the app host project, which orchestrates the other projects:
Start the app host project by right-clicking the eShopLite.AppHost project in the Solution Explorer and selecting Debug > Start New Instance:
Piezīme
If Docker Desktop (or Podman) isn't running, you'll experience an error. Start the OCI compliant container engine and try again.
Start the app host project by running the following command:
If Docker Desktop (or Podman) isn't running, you'll experience an error. Start the OCI compliant container engine and try again.
In the dashboard, select the endpoint for the products project. A new browser tab appears and displays the product catalog in JSON format.
In the dashboard, select the endpoint for the store project. A new browser tab appears and displays the home page for the web app.
In the menu on the left, select Products. The product catalog is displayed.
To stop debugging, close the browser.
Congratulations, you added .NET Aspire orchestration to your pre-existing web app. You can now add .NET Aspire integrations and use the .NET Aspire tooling to streamline your cloud-native web app development.
Sadarbojieties ar mums vietnē GitHub
Šī satura avotu var atrast vietnē GitHub, kur varat arī izveidot un pārskatīt problēmas un atgādāšanas pieprasījumus. Lai iegūtu papildinformāciju, skatiet mūsu līdzstrādnieku rokasgrāmatu.
.NET Aspire atsauksmes
.NET Aspire ir atklātā pirmkoda projekts. Atlasiet saiti, lai sniegtu atsauksmes:
Pievienojieties meetup sērijai, lai kopā ar citiem izstrādātājiem un ekspertiem izveidotu mērogojamus AI risinājumus, kuru pamatā ir reālas lietošanas gadījumi.
Build end-to-end solutions in Microsoft Azure to create Azure Functions, implement and manage web apps, develop solutions utilizing Azure storage, and more.