Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Blazor web app .NET 9: I can open and manage SQLite database in debug mode, but not in the hosted web app in IIS

$
0
0

Today was the second day since I noticed this exception:

A database operation failed while processing the request. SqliteException: SQLite Error 14: 'unable to open database file'.


Applying existing migrations may resolve this issue There aremigrations that have not been applied to the following database(s):

BlazorWebAppAdminContext

  • 20250708123456_MyArgumentName
  • 20250709789101_MyNewArgumentName1.0.0
  • 20250710111213_MyNewArgumentName1.0.2

Apply Migrations this is actually a button

In Visual Studio, you can use the Package Manager Console to apply pending migrations to the database:

PM> Update-Database

Alternatively, you can apply pending migrations from a command prompt at your project directory:

dotnet ef database update

NOTE: my .NET Core hosting bundle on Windows Server installed is dotnet-hosting-9.0.6-win.exe

I failed to apply the specific solution to this issue, even though I already did these workarounds:

  1. Ensuring that IIS_IUSRS has read and write permissions:

    IIS_IUSRS permissions

  2. In development (debug mode), I ensure that the Blazor web app is already using the updated data model and has successfully applied migrations and updated the SQLite database:

using System.ComponentModel.DataAnnotations;namespace MyBlazorWebAppName.Models;public class Admin{    [Key]    public string? UserID { get; set; }    public string? Username { get; set; }    [Required]    [StringLength(100)]    [RegularExpression(@"^[A-Z]+[a-zA-Z\s-]*$")]    public string? GivenName { get; set; }    public string? MiddleName { get; set; }    [Required]    [StringLength(100)]    [RegularExpression(@"^[A-Z]+[a-zA-Z\s-]*$")]    public string? FamilyName { get; set; }    [Required]    [StringLength(150, MinimumLength = 11)]    public string? EmailAdd { get; set; }    [Required]    [StringLength(100, MinimumLength = 3)]    [RegularExpression(@"^[A-Z]+[a-zA-Z\s-]*$")]    public string? RoleType { get; set; }}

working web app project

Relevant references: Edit Application Pool; IIS Application pools CLR v4.0 vs No managed code

  1. Ensuring that the Blazor web app that is hosted in IIS is configured properly:

    app_pools_adv_settings

  2. Ensuring that every folder and file generated by this command dotnet publish --configuration Release and located in {project directory}\bin\Release\net9.0\publish is properly in place at the designated physical path:

    IIS_web_app_advanced_settings

Here is my relevant source code:

Program.cs:

using MyBlazorWebAppName.Components;using Microsoft.EntityFrameworkCore;using MyBlazorWebAppName.Data;var builder = WebApplication.CreateBuilder(args);builder.Services.AddDbContextFactory<BlazorWebAppAdminContext>(options =>    options.UseSqlite(builder.Configuration.GetConnectionString("BlazorWebAppAdminContext") ?? throw new InvalidOperationException("Connection string 'BlazorWebAppAdminContext' not found.")));builder.Services.AddQuickGridEntityFrameworkAdapter();builder.Services.AddDatabaseDeveloperPageExceptionFilter();// Add services to the container.builder.Services.AddRazorComponents()    .AddInteractiveServerComponents();var app = builder.Build();// Configure the HTTP request pipeline.if (!app.Environment.IsDevelopment()){    app.UseExceptionHandler("/Error", createScopeForErrors: true);    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.    app.UseHsts();    app.UseMigrationsEndPoint();}app.UseHttpsRedirection();app.UseAntiforgery();app.MapStaticAssets();app.MapRazorComponents<App>()    .AddInteractiveServerRenderMode();app.Run();

BlazorWebAppAdminContext.cs:

using Microsoft.EntityFrameworkCore;namespace MyBlazorWebAppName.Data{    public class BlazorWebAppAdminContext(DbContextOptions<BlazorWebAppAdminContext> options) : DbContext(options)    {        public DbSet<Models.Admin> Admin { get; set; } = default!;    }}

Note: if you find this different from the default generated file's source code after following the tutorial: Build a Blazor movie database app (Overview), it's because I simply followed the code refactoring suggestions provided by the .NET framework. Nevertheless, it's still working in debug mode.

appsettings.json:

{"Logging": {"LogLevel": {"Default": "Information","Microsoft.AspNetCore": "Warning"    }  },"AllowedHosts": "*","ConnectionStrings": {"BlazorWebAppAdminContext": "Data Source=Data\\BlazorWebAppAdminContext-12345679-10111213-141516.db"  }}

Take note: for reproducible concerns, on the generated file web.config, I needed to add this configuration to reveal the actual error, which I've provided at the very top portion of this question thread:

<environmentVariables><environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" /></environmentVariables>

Reference: ASP.NET Core deployment to IIS error: Development environment should not be enabled in deployed applications

Here's the full configuration of web.config:

<?xml version="1.0" encoding="utf-8"?><configuration><location path="." inheritInChildApplications="false"><system.webServer><handlers><add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" /></handlers><aspNetCore processPath="dotnet" arguments=".\MyBlazorWebAppName.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess"><environmentVariables><environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" /></environmentVariables></aspNetCore></system.webServer></location></configuration>

So, here are my questions after all these struggles in deploying my Blazor web app (.NET 9 [using Visual Studio Code]) with SQLite to IIS 8.5 on SQL Server 2012 R2 Standard:

  • Has anybody else resolved the same issue as mine?
  • Is this a bug or what?
  • Do you see a misconfiguration on the project I am working with?
  • Am I missing something here?

EDIT: I've found out that I was using the incompatible ASP.NET Core Runtime for .NET SDK 9.0.302

Regarding the given findings, I downloaded the aspnetcore-runtime-9.0.7-win-x64.exe and dotnet-hosting-9.0.7-win.exe and then installed both executable files. Source: Download .NET 9.0 - ASP.NET Core Runtime 9.0.7.

Thanks to this YouTube video: Deploy ASP.Net Core Web Application in IIS - Dot Net Core Hosting for the tutorial from which I then realized I was missing something and using an incorrect version of the hosting bundle.

Moreover, according to this thread: IIS app does not have permissions to access an SQLite db file (which I found that it's relevant), which I'll quote (let's jump directly to the answer to be straightforward):

You only need to [assign] the iis_iusrs and iusr permission to the inetpub folder.

So, I checked the inetpub folder on the Windows Server, and I found out that it is not yet added there.

I go to the inetpub properties > edit > (group of user names) add > advanced... (since I wasn't familiar with the absolute name) > find now > I look for {your server name}\IIS_IUSRS and IUSR

And in the permission for IIS_IUSRS and IUSR

I ensure these configurations:

inetpub_permissions

Instead of this command:

dotnet publish --configuration Release

I found it more applicable to use this command instead (because my Windows Server is 64-bit):

dotnet publish -c Release -r win-x64

But, unfortunately, the problem persists.


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>