Removed Immediate.Apis, Added AOT Testing Scripts.

This commit is contained in:
2026-05-05 18:47:11 +05:00
parent d7ff6f112a
commit bcdd543916
27 changed files with 623 additions and 422 deletions
+97
View File
@@ -0,0 +1,97 @@
# AOT Testing Guide
This directory contains scripts for building and testing the Htmx.ApiDemo application with Ahead-of-Time (AOT) compilation enabled. AOT compilation helps identify potential trimming issues that may occur at runtime.
## Directory Structure
```
Testing/
└── AOT/
├── build-aot.ps1 # Windows PowerShell script
├── build-aot.sh # Linux/POP_OS bash script
├── Publish/ # Output directory (created during build)
└── README.md # This file
```
## Prerequisites
- .NET SDK (version 10.0 or later)
- For Windows: PowerShell 5.1 or later
- For Linux/POP_OS: Bash shell
## Usage
### Windows (PowerShell)
**Build and Run:**
```powershell
.\build-aot.ps1
```
**Build Only:**
```powershell
.\build-aot.ps1 -BuildOnly
```
**Run Only (if already built):**
```powershell
.\build-aot.ps1 -RunOnly
```
### Linux/POP_OS (Bash)
Make the script executable first:
```bash
chmod +x build-aot.sh
```
**Build and Run:**
```bash
./build-aot.sh
```
**Build Only:**
```bash
./build-aot.sh --build-only
```
**Run Only (if already built):**
```bash
./build-aot.sh --run-only
```
## What These Scripts Do
1. **Clean**: Removes any previous publish directory
2. **Build**: Publishes the application with the following AOT settings:
- `PublishAot=true` - Enables AOT compilation
- `TrimMode=link` - Uses link-time trimming
- `PublishTrimmed=true` - Enables trimming
- `SelfContained=true` - Creates a self-contained executable
- Debug symbols are disabled for optimized output
3. **Run**: Launches the compiled application to test for any trimming-related issues
## Output
The compiled application and all dependencies are published to the `Testing/AOT/Publish` directory.
## Troubleshooting
### Build Failures
If the AOT build fails, check the error messages for:
- **Trimming issues**: Indicates code that cannot be safely trimmed
- **Reflection warnings**: APIs that use reflection may not work properly
- **Missing dependencies**: Required libraries that aren't properly configured
### Common Issues
1. **"Executable not found"**: The build may have failed silently. Check the build output for errors.
2. **Runtime crashes**: Trimming may have removed necessary code. Consider adding trimming configuration in your project file or using `[DynamicallyAccessedMembers]` attributes.
## Related Documentation
- [AOT Deployment](https://learn.microsoft.com/en-us/dotnet/core/deploying/native-aot/)
- [Trimming .NET Applications](https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/trim-self-contained)
- [Reflections and Trimming](https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/trim-self-contained#reflections)
+122
View File
@@ -0,0 +1,122 @@
# AOT Build and Test Script for Windows PowerShell
# This script builds the Htmx.ApiDemo application with AOT compilation enabled
# and runs the application to check for trimming issues
param(
[switch]$RunOnly,
[switch]$BuildOnly
)
# Set error handling
$ErrorActionPreference = "Stop"
# Define paths
$ProjectRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
$ApiDemoProject = Join-Path $ProjectRoot "Htmx.ApiDemo"
$PublishPath = Join-Path $PSScriptRoot "Publish"
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "AOT Build and Test Script" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
if (-not $RunOnly) {
Write-Host "Starting AOT Build Process..." -ForegroundColor Yellow
Write-Host "Project Path: $ApiDemoProject" -ForegroundColor Gray
Write-Host "Publish Path: $PublishPath" -ForegroundColor Gray
Write-Host ""
# Kill any running instance of the app before cleaning (it locks the publish folder)
$AppName = "Htmx.ApiDemo"
$Running = Get-Process -Name $AppName -ErrorAction SilentlyContinue
if ($Running) {
Write-Host "Stopping running instance of $AppName..." -ForegroundColor Yellow
$Running | Stop-Process -Force
Start-Sleep -Milliseconds 500
}
# Clean previous publish folder if it exists
if (Test-Path $PublishPath) {
Write-Host "Cleaning previous publish folder..." -ForegroundColor Yellow
Remove-Item -Path $PublishPath -Recurse -Force
}
# Create publish folder
New-Item -Path $PublishPath -ItemType Directory -Force | Out-Null
try {
# Run dotnet publish with AOT enabled
# PublishAot=true is already set in the .csproj
# A Runtime Identifier (RID) is required for AOT compilation
Write-Host "Publishing with AOT compilation enabled (win-x64)..." -ForegroundColor Yellow
Push-Location $ApiDemoProject
dotnet publish -c Release -r win-x64 -o $PublishPath `
-p:DebugSymbols=false `
-p:DebugType=none
if ($LASTEXITCODE -ne 0) {
Write-Host "Build failed!" -ForegroundColor Red
Pop-Location
exit 1
}
Pop-Location
Write-Host "AOT build completed successfully!" -ForegroundColor Green
Write-Host ""
# Copy appsettings.Development.json over appsettings.json in the publish folder
# so the AOT executable has the correct connection strings for local testing
$DevSettings = Join-Path $ApiDemoProject "appsettings.Development.json"
$PublishedSettings = Join-Path $PublishPath "appsettings.json"
if (Test-Path $DevSettings) {
Write-Host "Copying appsettings.Development.json -> appsettings.json in publish folder..." -ForegroundColor Yellow
Copy-Item -Path $DevSettings -Destination $PublishedSettings -Force
Write-Host "Done." -ForegroundColor Green
Write-Host ""
} else {
Write-Host "Warning: appsettings.Development.json not found, skipping copy." -ForegroundColor DarkYellow
}
}
catch {
Write-Host "Error during build: $_" -ForegroundColor Red
Pop-Location
exit 1
}
}
if (-not $BuildOnly) {
Write-Host "Starting Application..." -ForegroundColor Yellow
Write-Host ""
# Find the executable
$Executable = Get-ChildItem -Path $PublishPath -Filter "*.exe" -Recurse | Select-Object -First 1
if (-not $Executable) {
Write-Host "Executable not found in publish directory!" -ForegroundColor Red
exit 1
}
Write-Host "Running: $($Executable.FullName)" -ForegroundColor Yellow
Write-Host "Press Ctrl+C to stop the application" -ForegroundColor Gray
Write-Host ""
try {
# Use Start-Process with WorkingDirectory so appsettings.json is found,
# and the shell CWD is never changed (safe against Ctrl+C)
$proc = Start-Process -FilePath $Executable.FullName `
-WorkingDirectory $PublishPath `
-NoNewWindow `
-PassThru
$proc.WaitForExit()
}
catch {
Write-Host "Error running application: $_" -ForegroundColor Red
exit 1
}
}
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "AOT Test Complete" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
+114
View File
@@ -0,0 +1,114 @@
#!/bin/bash
# AOT Build and Test Script for POP_OS/Linux
# This script builds the Htmx.ApiDemo application with AOT compilation enabled
# and runs the application to check for trimming issues
# Set error handling
set -e
# Initialize flags
RUN_ONLY=false
BUILD_ONLY=false
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
--run-only)
RUN_ONLY=true
shift
;;
--build-only)
BUILD_ONLY=true
shift
;;
*)
echo "Unknown option: $1"
echo "Usage: $0 [--run-only] [--build-only]"
exit 1
;;
esac
done
# Define paths
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)"
API_DEMO_PROJECT="$PROJECT_ROOT/Htmx.ApiDemo"
PUBLISH_PATH="$SCRIPT_DIR/Publish"
echo "========================================"
echo "AOT Build and Test Script"
echo "========================================"
echo ""
if [ "$RUN_ONLY" = false ]; then
echo "Starting AOT Build Process..."
echo "Project Path: $API_DEMO_PROJECT"
echo "Publish Path: $PUBLISH_PATH"
echo ""
# Clean previous publish folder if it exists
if [ -d "$PUBLISH_PATH" ]; then
echo "Cleaning previous publish folder..."
rm -rf "$PUBLISH_PATH"
fi
# Create publish folder
mkdir -p "$PUBLISH_PATH"
# Navigate to project directory
cd "$API_DEMO_PROJECT" || exit 1
# Run dotnet publish with AOT enabled
# PublishAot=true is already set in the .csproj
# A Runtime Identifier (RID) is required for AOT compilation
echo "Publishing with AOT compilation enabled (linux-x64)..."
if ! dotnet publish -c Release -r linux-x64 -o "$PUBLISH_PATH" \
-p:DebugSymbols=false \
-p:DebugType=none; then
echo "Build failed!"
exit 1
fi
echo "AOT build completed successfully!"
echo ""
# Copy appsettings.Development.json over appsettings.json in the publish folder
# so the AOT executable has the correct connection strings for local testing
DEV_SETTINGS="$API_DEMO_PROJECT/appsettings.Development.json"
PUBLISHED_SETTINGS="$PUBLISH_PATH/appsettings.json"
if [ -f "$DEV_SETTINGS" ]; then
echo "Copying appsettings.Development.json -> appsettings.json in publish folder..."
cp -f "$DEV_SETTINGS" "$PUBLISHED_SETTINGS"
echo "Done."
echo ""
else
echo "Warning: appsettings.Development.json not found, skipping copy."
fi
fi
if [ "$BUILD_ONLY" = false ]; then
echo "Starting Application..."
echo ""
# Find the executable
EXECUTABLE=$(find "$PUBLISH_PATH" -type f -perm /u+x ! -name "*.so" ! -name "*.a" ! -name "*.o" | head -n 1)
if [ -z "$EXECUTABLE" ]; then
echo "Executable not found in publish directory!"
exit 1
fi
echo "Running: $EXECUTABLE"
echo "Press Ctrl+C to stop the application"
echo ""
# cd into publish dir so appsettings.json is found relative to the executable
cd "$PUBLISH_PATH" || exit 1
"$EXECUTABLE"
fi
echo ""
echo "========================================"
echo "AOT Test Complete"
echo "========================================"