# Blazor Server Guidelines

## Application Startup Convention

### Prerequisites

```xml
<PackageReference Include="Dinaup" Version="*" />
```

***

### 1. Startup Banner

Identifies if a startup failure is caused by Docker or the application itself.

```csharp
Console.WriteLine("══════════════════════════════════════════════════════════════");
Console.WriteLine("  [APP_NAME] - Starting application");
Console.WriteLine($"  Date/Time: {DateTime.Now:yyyy-MM-dd HH:mm:ss}");
Console.WriteLine("══════════════════════════════════════════════════════════════");
```

***

### 2. Network Connectivity Check

Prevents firewall or network issues from causing errors in other services that are harder to diagnose.

```csharp
while (await Dinaup.Extensions.CheckNetworkConnectivityAsync() == false)
{
    Console.WriteLine("[ERROR] No internet connection. Retrying in 1 second...");
    await Task.Delay(1000);
}
```

***

### 3. Debug Port

Prevents port conflicts with other apps during development. In production, ports are configured via environment variables.

```csharp
const int DEBUG_PORT = 6114;

// Only applies when debugging
if (Debugger.IsAttached)
    builder.WebHost.UseUrls("http://*:" + DEBUG_PORT.ToString());
```

***

### 4. Version Endpoint

```csharp
app.MapGet("/Version", () => Results.Ok(new 
{ 
    version = Assembly.GetExecutingAssembly().GetName().Version.ToString() 
}));
```

***

### 5. Fatal Error Handling

```csharp
retryinitialization:

try
{
    var builder = WebApplication.CreateBuilder(args);
    
    if (Debugger.IsAttached)
        builder.WebHost.UseUrls("http://*:" + DEBUG_PORT.ToString());

    // ... initialization ...

    app.Run();
}
catch (Exception ex)
{
    Console.WriteLine("══════════════════════════════════════════════════════════════");
    Console.WriteLine("  [ERROR] Fatal error during initialization");
    Console.WriteLine($"  Message: {ex.Message}");
    Console.WriteLine($"  Type: {ex.GetType().Name}");
    Console.WriteLine($"  Date/Time: {DateTime.Now:yyyy-MM-dd HH:mm:ss}");
    Console.WriteLine("══════════════════════════════════════════════════════════════");
    Console.WriteLine(ex.ToString());

    try { Dinaup.Logs.Write_Flag("INI", ex); } catch { }

    var builderRecovery = WebApplication.CreateBuilder(args);
    
    if (Debugger.IsAttached)
        builderRecovery.WebHost.UseUrls("http://*:" + DEBUG_PORT.ToString());

    var appRecovery = builderRecovery.Build();
    appRecovery.UseMiddleware<ErrorFatalMiddleware>();
    
    Console.WriteLine("[LOG] Recovery mode. Retrying in 10 seconds...");
    using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
    await appRecovery.RunAsync(cts.Token);
    
    goto retryinitialization;
}
finally
{
    Dinaup.Logs.CloseAndFlush();
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.dinaup.com/desarrollo/c-code-style-guidelines/blazor-server-guidelines.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
