eShop Reference Application

repository·main·Indexed 27 days ago

https://github.com/dotnet/eshop

A reference implementation of an e-commerce website built with .NET 9 and .NET Aspire, demonstrating a services-based architecture. The project includes guides for local setup using Visual Studio or the .NET CLI, Azure deployment via the Azure Developer CLI (azd), Azure OpenAI configuration, and Playwright E2E testing. It also details Ordering API commands for managing order lifecycles, including CreateOrderCommand, order status transitions, and shipping.

Tokens
2.1K
Snippets
4
Records
13
Agent score
95%

What's inside eShop

  1. Deploy eShop to Azure using Azure Developer CLI (azd)

    main

    You can deploy the .NET Aspire project to Azure using the azd CLI.

    1. Install/Update azd: Azure Developer CLI.
    2. Login:
      azd auth login
    3. Initialize: Run azd init from the root of the repository.
      • Select Use code in the current directory.
      • Confirm .NET (Aspire) detection.
      • Select services to expose to the Internet (e.g., webapp).
      • Provide an environment name.
    4. Deploy:
      azd up

    azd up will create Azure resources and deploy the sample. Once finished, it will provide the url for the webapp. You can re-run azd up after making changes to re-deploy.

    azd auth login
    azd init
    azd up
  2. Run the eShop solution locally

    main

    Ensure Docker is started before running the application.

    Using Visual Studio (Windows)

    1. Open the eShop.Web.slnf file.
    2. Set eShop.AppHost.csproj as the startup project.
    3. Press Ctrl-F5 to launch via .NET Aspire.

    Using the Terminal

    Run the AppHost project directly using the .NET CLI:

    dotnet run --project src/eShop.AppHost/eShop.AppHost.csproj

    After running, look for the Aspire dashboard login URL in the console output: Login to the dashboard at: http://localhost:XXXXX/login?t=uniquelogincodeforyou

    Note: If you encounter HTTPS issues, you may need to install ASP.NET Core HTTPS development certificates: https://aka.ms/aspnet/https-trust-dev-cert

    dotnet run --project src/eShop.AppHost/eShop.AppHost.csproj
  3. Install and configure the eShop development environment

    main

    To run eShop locally, you must clone the repository and ensure Docker Desktop is installed and running. The setup process varies by operating system and preferred IDE.

    Windows with Visual Studio

    Install Visual Studio 2022 (version 17.10 or newer) with the following workloads:

    • ASP.NET and web development
    • .NET Aspire SDK (found in Individual components)
    • (Optional) .NET Multi-platform App UI development for client apps.

    Alternatively, you can automate the configuration using PowerShell as an Administrator:

    install-Module -Name Microsoft.WinGet.Configuration -AllowPrerelease -AcceptLicense -Force
    $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
    get-WinGetConfiguration -file .\ .\configurations\vside.dsc.yaml | Invoke-WinGetConfiguration -AcceptConfigurationAgreements

    Note: A restart is required after running this script.

    Mac, Linux, or Windows without Visual Studio

    Install the latest .NET 9 SDK.

    To automate environment configuration for VS Code via PowerShell (Administrator):

    install-Module -Name Microsoft.WinGet.Configuration -AllowPrerelease -AcceptLicense  -Force
    $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
    get-WinGetConfiguration -file .\ .\configurations\vscode.dsc.yaml | Invoke-WinGetConfiguration -AcceptConfigurationAgreements

    Note: These commands may require sudo on non-Windows systems. A restart is required after running.

  4. Configure Azure OpenAI in eShop

    main

    To use Azure OpenAI features, update the configuration in eShop.AppHost/appsettings.json and enable the feature in eShop.AppHost/Program.cs.

    1. Add the OpenAi connection string to appsettings.json:
    "ConnectionStrings": {
      "OpenAi": "Endpoint=xxx;Key=xxx;"
    }
    1. In eShop.AppHost/Program.cs, set the useOpenAI flag to true:
    bool useOpenAI = true;
  5. Configure Playwright E2E testing environment

    main

    The eShop project uses Playwright for end-to-end (E2E) testing. The configuration is located in playwright.config.ts and defines how tests are executed, which browsers are used, and how the application server is managed.

    Key Configuration Details:

    • Test Directory: Tests are located in ./e2e.
    • Base URL: The default base URL for actions like page.goto('/') is http://localhost:5045.
    • Authentication: The project uses a shared storage state for authenticated tests, located at playwright/.auth/user.json.
    • CI Behavior: On Continuous Integration (CI), the configuration automatically enables forbidOnly, sets retries to 2, and limits workers to 1 to ensure stability.

    Test Projects:

    • setup: Runs setup scripts matching **/*.setup.ts.
    • e2e tests logged in: Runs specific tests (AddItemTest.spec.ts, RemoveItemTest.spec.ts) that depend on the setup project and use the stored authentication state.
    • e2e tests without logged in: Runs unauthenticated tests (e.g., BrowseItemTest.spec.ts).
  6. Configure the Playwright webServer for eShop

    main

    Playwright is configured to automatically manage the eShop application lifecycle via the webServer option. This ensures the application is running before tests start.

    • Startup Command: dotnet run --project src/eShop.AppHost/eShop.AppHost.csproj
    • Target URL: http://localhost:5045
    • Server Reuse: On local development, the existing server is reused (reuseExistingServer: !process.env.CI) to speed up test cycles.
    • Timeout: The startup timeout is 60 seconds locally, or 5 minutes on CI.
    webServer: {
      command: 'dotnet run --project src/eShop.AppHost/eShop.AppHost.csproj',
      url: 'http://localhost:5045',
      reuseExistingServer: !process.env.CI,
      stderr: 'pipe',
      stdout: 'pipe',
      timeout: process.env.CI ? (5 * 60_000) : 60_000,
    }
  7. Create a new order using CreateOrderCommand

    main

    The CreateOrderCommand is used to initiate the creation of a new order within the Ordering API. It follows the CQRS pattern and is designed to be immutable. To create a command, you must provide user identity details, shipping address information, payment card details, and a list of order items derived from a basket.

    Data Contract Fields:

    FieldTypeDescription
    UserIdstringThe unique identifier of the user
    UserNamestringThe name of the user
    CitystringShipping city
    StreetstringShipping street address
    StatestringShipping state/province
    CountrystringShipping country
    ZipCodestringShipping postal/zip code
    CardNumberstringPayment card number
    CardHolderNamestringName of the cardholder
    CardExpirationDateTimeExpiration date of the card
    CardSecurityNumberstringCard security number (CVV)
    CardTypeIdintIdentifier for the type of card
    OrderItemsIEnumerable<OrderItemDTO>The list of items included in the order