Creating your first application with .NET Aspire can be an exciting journey into the world of cloud-native, distributed applications. .NET Aspire is designed to streamline the development of such applications, providing a set of tools and patterns that help you build and run your services efficiently.

Getting Started with .NET Aspire

Before diving into code, ensure you have the following prerequisites installed:

Creating a Basic Airline Application

Let’s create a simple airline application that allows users to book flights. We’ll start by setting up a .NET Aspire project and then add functionality for flight booking.

Step 1: Setting Up the Project

First, create a new .NET Aspire project using either Visual Studio or the .NET CLI. In Visual Studio:

  1. Navigate to File > New > Project.
  2. Search for Aspire and select .NET Aspire Starter Application.
  3. Enter a project name, like AirlineApp, and follow the prompts to create the project1.

Step 2: Adding a Flight Model

Create a Flight model to represent the flights in our system.

public class Flight
{
    public int Id { get; set; }
    public string Origin { get; set; }
    public string Destination { get; set; }
    public DateTime DepartureTime { get; set; }
}

Step 3: Implementing Flight Booking

Next, we’ll add a service to handle flight bookings. This service will include methods to list available flights and book a seat.

public class BookingService
{
    private readonly List<Flight> _flights;

    public BookingService()
    {
        _flights = new List<Flight>();
    }

    public IEnumerable<Flight> GetAvailableFlights() => _flights;

    public bool BookFlight(int flightId)
    {
        var flight = _flights.FirstOrDefault(f => f.Id == flightId);
        if (flight == null) return false;

        // Logic to book the flight
        return true;
    }
}

Step 4: Creating an API Endpoint

Now, let’s expose our booking service through an API endpoint using Minimal APIs in .NET.

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/flights", (BookingService bookingService) =>
{
    return bookingService.GetAvailableFlights();
});

app.MapPost("/book-flight/{flightId}", (int flightId, BookingService bookingService) =>
{
    return bookingService.BookFlight(flightId) ? Results.Ok() : Results.NotFound();
});

app.Run();

Testing the Application

After setting up your application, you can test it locally to ensure everything is working as expected. Use tools like Swagger or Postman to interact with your API and test the booking functionality.

Summary

In this blog post, we’ve covered the basics of creating a .NET Aspire application with a focus on building a simple airline booking system. We started by setting up the project, then we defined a model for our flights, implemented a booking service, and finally, we created API endpoints to interact with our service.

.NET Aspire makes it easier to build and run distributed applications by providing a consistent set of tools and patterns. With .NET Aspire, you can focus on writing your business logic while it takes care of the underlying cloud-native complexities.

Remember, this is just a starting point. As you grow your application, you can explore more features of .NET Aspire, such as service discovery, caching with Redis, and integrating with other cloud services to build a robust, scalable airline application2.

Getting started with .NET Aspire

Johannes Rest


.NET Architekt und Entwickler


Beitragsnavigation


Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert