Deployment Guide
Relevant source files
The following files were used as context for generating this wiki page:This guide provides a comprehensive overview of the configuration and setup required to deploy and run the WEBAPI project. The project is an ASP.NET Core Web API designed to serve as a backend for an e-commerce platform, managing entities such as users, products, and orders.
The guide covers essential deployment topics including application configuration, database setup using Entity Framework Core with SQL Server, service dependencies, and application startup procedures. All information is derived directly from the source code to ensure accuracy.
Application Configuration
Proper configuration is essential for deploying the application. Key settings are managed through appsettings.json for environment-agnostic values and launchSettings.json for development-time launch profiles.
Connection Strings
The application requires a connection string to a SQL Server database. This is configured in appsettings.json.
{
"ConnectionStrings": {
"UserDataBase": "..."
}
}
This connection string, named UserDataBase, is read during application startup to configure the UserDBContext.
// In Program.cs
builder.Services.AddDbContext<UserDBContext>(option =>
{
option.UseSqlServer(builder.Configuration.GetConnectionString("UserDataBase"));
});
The UserDBContextFactory also uses this connection string, which is crucial for design-time operations like creating database migrations.
Sources: Program.cs:22-25, Data/UserDBContextFactory.cs:13-16
Launch Profiles
The Properties/launchSettings.json file defines multiple profiles for launching the application during development. These profiles configure URLs, environment variables, and whether to launch a browser.
| Profile Name | Command | Launch Browser | Application URL | Environment |
|---|---|---|---|---|
http | Project | true | http://localhost:5048 | Development |
https | Project | true | https://localhost:7032;http://localhost:5048 | Development |
IIS Express | IISExpress | true | http://localhost:19162 (IIS) | Development |
For all profiles, the ASPNETCORE_ENVIRONMENT is set to Development, which enables features like the Swagger UI. The launchUrl is set to swagger, directing the browser to the API documentation page on startup.
Sources: Properties/launchSettings.json:13-39
Core Services
The application relies on several services configured for dependency injection at startup.
The following diagram shows the service registration flow in Program.cs.
This diagram illustrates the services being added to the dependency injection container.
Sources: Program.cs:12-40
Database Setup
The application uses Entity Framework Core to manage its database schema and interactions. The target database is SQL Server.
Database Context
The UserDBContext class is the main entry point for database operations. It defines DbSet properties for all major entities and applies entity configurations on model creation.
// In Data/UserDBContext.cs
public class UserDBContext : DbContext
{
// ... constructor ...
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new UserConfiguration());
modelBuilder.ApplyConfiguration(new OrderConfiguration());
modelBuilder.ApplyConfiguration(new ProductConfiguration());
// ... other configurations ...
modelBuilder.Seed();
}
public DbSet<User> User { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<Product> Products { get; set; }
// ... other DbSets ...
}
Sources: Data/UserDBContext.cs:8-36
Database Schema
The database contains tables for users, products, categories, orders, and administrators. The relationships between these entities are defined using EF Core's fluent API in configuration classes.
The following ER diagram shows the database schema.
This diagram visualizes the tables and their foreign key relationships as defined in the EF Core model snapshot.
Sources: Migrations/UserDBContextModelSnapshot.cs, Configuration/OrderConfiguration.cs, Configuration/ProductConfiguration.cs, Configuration/UserConfiguration.cs
Data Seeding
The application includes a data seeder to populate the database with initial data. A default user is created when the database model is being built.
// In Seeders/DatabaseSeeder.cs
public static class DatabaseSeeder
{
public static void Seed(this ModelBuilder modelBuilder)
{
_ = modelBuilder.Entity<User>().HasData(
new User()
{
Id = 1,
Email = "chihtt123@gmail.com",
UserName = "chi",
Password = "123",
Role = true
}
);
}
}
This seeder is called from UserDBContext.OnModelCreating.
Sources: Seeders/DatabaseSeeder.cs:7-20, Data/UserDBContext.cs:19
Application Startup and Request Pipeline
The Program.cs file configures the application's HTTP request pipeline. The configuration is environment-dependent.
The following flowchart illustrates the request pipeline configuration.
In a development environment, Swagger and SwaggerUI are enabled for API testing and documentation. A permissive CORS policy is also applied, allowing requests from any origin, which should be restricted for production deployments.
Sources: Program.cs:45-62
API Endpoints
The application exposes RESTful API endpoints through controllers. For example, the OrderController manages order-related operations.
The sequence diagram below shows the flow for creating a new order via the POST /api/v1/order endpoint.
This diagram shows how a client request flows through the controller to the service layer, which then interacts with the database via the DbContext to persist the new order.
Sources: Controllers/Order/OrderController.cs:22-60
Summary
To deploy the WEBAPI project, the following steps are crucial:
- Configure
appsettings.json: Provide a valid SQL Server connection string forUserDataBase. - Apply Database Migrations: Use EF Core tools to apply migrations and create the database schema based on the defined models and configurations. This will also seed the initial user data.
- Host the Application: Use a suitable hosting environment for an ASP.NET Core application, such as IIS or a container service.
- Production Configuration: For a production deployment, ensure that the
ASPNETCORE_ENVIRONMENTis set toProduction. This will disable development-only features like the Swagger UI. Additionally, configure a more restrictive CORS policy to enhance security.
Sources: Program.cs, Properties/launchSettings.json, Data/UserDBContextFactory.cs