Overview
This guide covers deployment options for Sistema Venta API, including IIS, Azure App Service, Docker containers, and self-hosted scenarios.Pre-Deployment Checklist
{
"Logging": {
"LogLevel": {
"Default": "Warning",
"Microsoft.AspNetCore": "Warning",
"Microsoft.EntityFrameworkCore": "Warning"
}
},
"AllowedHosts": "yourdomain.com",
"ConnectionStrings": {
"cadenaSQL": "${SQL_CONNECTION_STRING}"
}
}
builder.Services.AddCors(options => {
options.AddPolicy("NuevaPolitica", app => {
app.WithOrigins("https://yourdomain.com", "https://www.yourdomain.com")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
});
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseHttpsRedirection();
}
app.UseCors("NuevaPolitica");
app.UseAuthorization();
app.MapControllers();
Deployment Options
Option 1: IIS (Internet Information Services)
- Download from: https://dotnet.microsoft.com/download/dotnet/7.0
- Install “ASP.NET Core Runtime 7.0.x - Windows Hosting Bundle”
- Restart IIS:
iisreset
- Open “Turn Windows features on or off”
- Enable “Internet Information Services”
- Enable “World Wide Web Services” → “Application Development Features” → “ASP.NET 4.8”
- Site name: SistemaVentaAPI
- Physical path: Path to your
publishfolder - Binding:
- Type: https
- Port: 443
- Host name: api.yourdomain.com
- SSL certificate: Select or import your SSL certificate
- Right-click your site → Manage Website → Advanced Settings
- Application Pool: Create new pool
- .NET CLR Version: No Managed Code
- Managed Pipeline Mode: Integrated
- For SQL Windows Auth: Use ApplicationPoolIdentity or domain account
- Grant this identity access to SQL Server
system.webServer/aspNetCoreTroubleshooting IIS:
- Check Event Viewer → Windows Logs → Application for errors
- Enable detailed errors: Set
ASPNETCORE_ENVIRONMENT=Developmenttemporarily - Verify .NET Hosting Bundle is installed: Check for
aspnetcorev2.dllin IIS modules - Ensure application pool identity has file system permissions
Option 2: Azure App Service
az sql server create \
--name sistemaventa-sql \
--resource-group your-resource-group \
--location eastus \
--admin-user sqladmin \
--admin-password YourStrongPassword123!
az sql db create \
--resource-group your-resource-group \
--server sistemaventa-sql \
--name DBVENTA \
--service-objective S0
az appservice plan create \
--name sistemaventa-plan \
--resource-group your-resource-group \
--sku B1 \
--is-linux
az webapp create \
--resource-group your-resource-group \
--plan sistemaventa-plan \
--name sistemaventa-api \
--runtime "DOTNET|7.0"
az webapp config connection-string set \
--resource-group your-resource-group \
--name sistemaventa-api \
--settings cadenaSQL="Server=tcp:sistemaventa-sql.database.windows.net,1433;Database=DBVENTA;User ID=sqladmin;Password=YourStrongPassword123!;Encrypt=True;" \
--connection-string-type SQLAzure
# Build and publish
dotnet publish --configuration Release --output ./publish
# Create deployment package
cd publish
zip -r deploy.zip .
# Deploy to Azure
az webapp deployment source config-zip \
--resource-group your-resource-group \
--name sistemaventa-api \
--src deploy.zip
ASPNETCORE_ENVIRONMENT=ProductionWEBSITE_RUN_FROM_PACKAGE=1
- Stack: .NET
- Major version: 7
- HTTPS Only: On
Azure Pricing Considerations:
- B1 App Service Plan: ~$13/month
- S0 SQL Database: ~$15/month
- Consider Azure SQL Database serverless tier for development
Option 3: Docker Container
# Build stage
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
# Copy project files
COPY ["SistemaVenta.API/SistemaVenta.API.csproj", "SistemaVenta.API/"]
COPY ["SistemaVenta.BLL/SistemaVenta.BLL.csproj", "SistemaVenta.BLL/"]
COPY ["SistemaVenta.DAL/SistemaVenta.DAL.csproj", "SistemaVenta.DAL/"]
COPY ["SistemaVenta.DTO/SistemaVenta.DTO.csproj", "SistemaVenta.DTO/"]
COPY ["SistemaVenta.IOC/SistemaVenta.IOC.csproj", "SistemaVenta.IOC/"]
COPY ["SistemaVenta.Model/SistemaVenta.Model.csproj", "SistemaVenta.Model/"]
COPY ["SistemaVenta.Utility/SistemaVenta.Utility.csproj", "SistemaVenta.Utility/"]
# Restore dependencies
RUN dotnet restore "SistemaVenta.API/SistemaVenta.API.csproj"
# Copy everything else
COPY . .
# Build
WORKDIR "/src/SistemaVenta.API"
RUN dotnet build "SistemaVenta.API.csproj" -c Release -o /app/build
# Publish
FROM build AS publish
RUN dotnet publish "SistemaVenta.API.csproj" -c Release -o /app/publish /p:UseAppHost=false
# Runtime stage
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS final
WORKDIR /app
EXPOSE 80
EXPOSE 443
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "SistemaVenta.API.dll"]
version: '3.8'
services:
api:
build:
context: .
dockerfile: Dockerfile
ports:
- "5000:80"
environment:
- ASPNETCORE_ENVIRONMENT=Production
- ConnectionStrings__cadenaSQL=Server=sqlserver;Database=DBVENTA;User Id=sa;Password=YourStrong@Password;TrustServerCertificate=True;
depends_on:
- sqlserver
networks:
- sistemaventa-network
sqlserver:
image: mcr.microsoft.com/mssql/server:2022-latest
environment:
- ACCEPT_EULA=Y
- SA_PASSWORD=YourStrong@Password
- MSSQL_PID=Express
ports:
- "1433:1433"
volumes:
- sqlserver-data:/var/opt/mssql
networks:
- sistemaventa-network
volumes:
sqlserver-data:
networks:
sistemaventa-network:
driver: bridge
# Build the image
docker build -t sistemaventa-api:latest .
# Run with docker-compose
docker-compose up -d
# View logs
docker-compose logs -f api
# Stop services
docker-compose down
# Tag for Docker Hub
docker tag sistemaventa-api:latest yourusername/sistemaventa-api:latest
# Push to Docker Hub
docker login
docker push yourusername/sistemaventa-api:latest
# Or push to Azure Container Registry
az acr login --name yourregistry
docker tag sistemaventa-api:latest yourregistry.azurecr.io/sistemaventa-api:latest
docker push yourregistry.azurecr.io/sistemaventa-api:latest
Option 4: Self-Hosted (Kestrel)
[Unit]
Description=Sistema Venta API
After=network.target
[Service]
Type=notify
User=www-data
WorkingDirectory=/var/www/sistemaventa-api
ExecStart=/usr/bin/dotnet /var/www/sistemaventa-api/SistemaVenta.API.dll
Restart=always
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=sistemaventa-api
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
[Install]
WantedBy=multi-user.target
sudo systemctl enable sistemaventa-api
sudo systemctl start sistemaventa-api
sudo systemctl status sistemaventa-api
server {
listen 80;
listen [::]:80;
server_name api.yourdomain.com;
location / {
return 301 https://$server_name$request_uri;
}
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name api.yourdomain.com;
ssl_certificate /etc/ssl/certs/yourdomain.crt;
ssl_certificate_key /etc/ssl/private/yourdomain.key;
location / {
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Environment-Specific Configuration
Development
appsettings.Development.json
Staging
appsettings.Staging.json
Production
appsettings.Production.json
Security Best Practices
builder.Services.AddAuthentication("ApiKey")
.AddScheme<ApiKeyAuthenticationOptions, ApiKeyAuthenticationHandler>("ApiKey", options => { });
builder.Services.AddRateLimiter(options =>
{
options.AddFixedWindowLimiter("fixed", opt =>
{
opt.PermitLimit = 100;
opt.Window = TimeSpan.FromMinutes(1);
});
});
Health Checks
Add health check endpoint:Program.cs
curl https://api.yourdomain.com/health
Monitoring and Logging
Application Insights (Azure)
Program.cs
Serilog
Program.cs
Performance Optimization
Enable Response Compression
Add Response Caching
Enable Output Caching (.NET 7+)
Rollback Strategy
IIS
- Keep previous publish folder:
publish-backup-{date} - Update IIS site physical path to previous folder
- Restart application pool
Azure App Service
- Go to Deployment slots
- Swap staging slot back to production
- Or use Deployment Center → Logs → Redeploy previous version
Docker
Post-Deployment Verification
Troubleshooting
502 Bad Gateway (IIS)
502 Bad Gateway (IIS)
Causes:
- Application pool stopped
- .NET Hosting Bundle not installed
- Application startup error
- Check Event Viewer logs
- Enable stdout logging in
web.config: - Verify application pool is running
- Restart IIS:
iisreset
Database Connection Timeout
Database Connection Timeout
Causes:
- Incorrect connection string
- Firewall blocking connection
- SQL Server not accepting remote connections
- Test connection string locally
- For Azure SQL: Add App Service IP to firewall rules
- Verify SQL Server TCP/IP is enabled
- Check connection string format
CORS Errors in Production
CORS Errors in Production
Error:
Access to fetch at 'https://api.yourdomain.com' from origin 'https://app.yourdomain.com' has been blocked by CORS policySolution:
Update CORS policy in Program.cs:14 to include your frontend origin.Continuous Deployment
GitHub Actions
Create.github/workflows/deploy.yml:
Next Steps
Configuration
Review configuration and dependency injection
Database Setup
Configure Entity Framework and database