Files
PowderCoatingLogix/TROUBLESHOOTING_STARTUP.md
2026-04-23 21:38:24 -04:00

352 lines
8.2 KiB
Markdown

# Troubleshooting: App Not Starting
## 🐛 Problem: "Nothing happens when I run the app"
This usually means the application is hanging during startup, most likely during database migration or seeding.
---
## 🔍 Quick Diagnostics
### Step 1: Check What's in the Console
When you run `dotnet run`, you should see output. What do you see?
#### ✅ GOOD - You should see this:
```
Building...
info: Microsoft.Hosting.Lifetime[14]
Now listening on: https://localhost:7001
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5001
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
```
#### ❌ BAD - If you see this (or nothing):
```
Building...
[Hangs here - nothing else appears]
```
This means it's **stuck during startup**, likely during database migration.
---
## 🛠️ Solution 1: Skip Automatic Migrations (Recommended)
The issue is that the app tries to migrate the database on startup. Let's disable that temporarily.
### Update Program.cs
**Comment out the automatic migration section:**
Find this section in `src/PowderCoating.Web/Program.cs` (around line 112-133):
```csharp
// Seed database with initial data
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var context = services.GetRequiredService<ApplicationDbContext>();
var userManager = services.GetRequiredService<UserManager<ApplicationUser>>();
var roleManager = services.GetRequiredService<RoleManager<IdentityRole>>();
// Run migrations
await context.Database.MigrateAsync();
// Seed roles and admin user
await SeedData.InitializeAsync(services, userManager, roleManager);
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occurred while migrating or seeding the database.");
}
}
```
**Replace it with:**
```csharp
// NOTE: Database seeding is disabled for now
// Run migrations manually with: dotnet ef database update
// Seeding will happen on first database update
// Uncomment below after you've created the database manually:
/*
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var userManager = services.GetRequiredService<UserManager<ApplicationUser>>();
var roleManager = services.GetRequiredService<RoleManager<IdentityRole>>();
// Seed roles and admin user (no migration here)
await SeedData.InitializeAsync(services, userManager, roleManager);
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occurred while seeding the database.");
}
}
*/
```
### Then Manually Create the Database
```bash
cd src/PowderCoating.Web
# Create migration
dotnet ef migrations add InitialCreate --project ../PowderCoating.Infrastructure
# Apply migration
dotnet ef database update --project ../PowderCoating.Infrastructure
```
### Now Run the App Again
```bash
dotnet run
```
It should start immediately!
---
## 🛠️ Solution 2: Check SQL Server Connection
If the app is hanging, it might be trying to connect to SQL Server but failing.
### Is SQL Server Running?
**Windows - Check Service:**
```powershell
Get-Service MSSQL$SQLEXPRESS
# If not running:
Start-Service MSSQL$SQLEXPRESS
```
**Or use Services.msc:**
1. Press `Win + R`
2. Type `services.msc`
3. Find "SQL Server (SQLEXPRESS)"
4. Check if it's running
### Test Connection String
The app uses this connection string (in `appsettings.json`):
```json
"Server=.\\SQLEXPRESS;Database=PowderCoatingDb;Trusted_Connection=true;MultipleActiveResultSets=true;TrustServerCertificate=true"
```
**Test it manually:**
1. Open SQL Server Management Studio (SSMS) or Azure Data Studio
2. Connect to: `.\SQLEXPRESS`
3. If you can connect, SQL Server is running
If you **can't connect**, SQL Express might not be installed.
---
## 🛠️ Solution 3: Use LocalDB Instead
If SQL Express is giving you trouble, switch to LocalDB:
### Update Connection String
Edit `src/PowderCoating.Web/appsettings.json`:
**Change from:**
```json
"ConnectionStrings": {
"DefaultConnection": "Server=.\\SQLEXPRESS;Database=PowderCoatingDb;Trusted_Connection=true;MultipleActiveResultSets=true;TrustServerCertificate=true"
}
```
**To:**
```json
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=PowderCoatingDb;Trusted_Connection=true;MultipleActiveResultSets=true"
}
```
Also update `src/PowderCoating.Api/appsettings.json` the same way.
### Create Database
```bash
dotnet ef database update --project src/PowderCoating.Infrastructure --startup-project src/PowderCoating.Web
```
### Run the App
```bash
cd src/PowderCoating.Web
dotnet run
```
---
## 🛠️ Solution 4: Verbose Logging
Let's see exactly where it's hanging.
### Run with Detailed Logging
```bash
cd src/PowderCoating.Web
dotnet run --verbosity diagnostic
```
This will show you EXACTLY where it stops.
### Check the Log File
Look in: `logs/powdercoating-YYYYMMDD.txt`
The last line before it hangs will tell you what's wrong.
---
## 🛠️ Solution 5: Complete Clean Rebuild
Sometimes old build artifacts cause issues.
```bash
# From solution root
dotnet clean
rm -rf **/bin **/obj
# Restore packages
dotnet restore
# Build
dotnet build
# Run
cd src/PowderCoating.Web
dotnet run
```
---
## 📋 Diagnostic Checklist
Work through these in order:
- [ ] **Check console output** - What's the last line you see?
- [ ] **Is SQL Server running?** - Check the service
- [ ] **Can you connect to SQL Server?** - Try SSMS/Azure Data Studio
- [ ] **Comment out auto-migration** - Use Solution 1 above
- [ ] **Create database manually** - Use `dotnet ef database update`
- [ ] **Try running again** - Does it work now?
- [ ] **Check logs folder** - Look at the log file
- [ ] **Try LocalDB** - If SQL Express isn't working
---
## 🎯 Most Likely Issue
**90% of the time, the issue is:**
The app is trying to connect to SQL Express but:
1. SQL Express isn't running, OR
2. SQL Express isn't installed, OR
3. The connection string is wrong
**Quick fix:**
1. Comment out the auto-migration code (Solution 1)
2. Use LocalDB instead (Solution 3)
3. Run the app - it should start immediately
4. Create the database manually with `dotnet ef database update`
---
## 💡 Expected Behavior
### When Working Correctly:
```bash
$ cd src/PowderCoating.Web
$ dotnet run
Building...
Build succeeded.
0 Warning(s)
0 Error(s)
info: Microsoft.Hosting.Lifetime[14]
Now listening on: https://localhost:7001
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5001
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: C:\Projects\PowderCoatingApp\src\PowderCoating.Web
```
**Then:** Open browser to https://localhost:7001 and you see the home page.
---
## 🆘 Still Not Working?
Share these details:
1. **What OS are you on?** (Windows, Mac, Linux)
2. **What's the last line you see** when running `dotnet run`?
3. **Is SQL Server installed?** How did you install it?
4. **What's in the log file?** (`logs/powdercoating-*.txt`)
5. **Does this work?** `dotnet ef --version`
With these details, I can provide more specific help!
---
## ✅ Quick Win
**Try this right now:**
1. **Stop the app** if it's running (Ctrl+C)
2. **Edit Program.cs** - Comment out lines 112-133 (the entire seeding block)
3. **Run the app:**
```bash
dotnet run
```
4. **You should see:**
```
Now listening on: https://localhost:7001
```
5. **Open browser** to https://localhost:7001
6. **You'll see an error** about missing database - that's OK!
7. **Stop the app** (Ctrl+C)
8. **Create database:**
```bash
dotnet ef database update --project ../PowderCoating.Infrastructure
```
9. **Run again:**
```bash
dotnet run
```
10. **Now it should work!** Navigate to https://localhost:7001/Identity/Account/Login
This bypasses the automatic migration that's causing the hang.