ASP.NET Core Web 3.1 - IIS Express can fetch data from localdb but local IIS can not
Asked Answered
S

1

1

I have a very weird problem as stated above.

The code running is exactly the same, the only difference is the Debug profile used. I'm using Microsoft Visual Studio Community 2019 Version 16.6.3.

When using the IIS profile the code says I have no Media in my database.

enter image description here

enter image description here

Switching profile to IIS Express, now I have Media using the exakt same connection string as can be seen in Immediate Windows.

enter image description here

enter image description here

I first suspected that the ASP.NET Core Web 3.1 had a problem fetching the connection string from appsettings.json and that it was needed in web.config as well. Tried adding it but it did not make a difference. Checking context.Database.GetDbConnection().ConnectionString also shows the same for both profiles so this is not the case.

<connectionStrings>
  <add name="DefaultConnection" connectionString="Server=(localdb)\\mssqllocaldb;Database=<MYDB>;Trusted_Connection=True;MultipleActiveResultSets=true" providerName="System.Data.SqlClient" />
</connectionStrings>

If I look at the database via SSMS I can see my Media as expected.

enter image description here

The IIS Application Pool runs as LocalSystem. If I switch to a user that does not have access to the database like ApplicationPoolIdentity I get an exception as expected in Windows Event Viewer and the application does not start correctly.

Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50

  • Local Database Runtime error occurred. Cannot create an automatic instance. See the Windows Application event log for error details.

enter image description here

Using LocalSystem everything looks fine in Event Viewer but it says I have no Media or any other data.

enter image description here

How can two identical connection strings using the same code show different results?

Sandrocottus answered 8/7, 2020 at 14:39 Comment(2)
Does this help?Romona
@Romona Thank you and yes it did!Sandrocottus
S
4

Tried switching Application Pool Identity to my currently logged in user that I know has access via SSMS:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Error occurred during LocalDB instance startup: SQL Server process failed to start. )

With this and the comment from @nilsK I fixed it like this:

Start by using Developer PowerShell or Developer Command Prompt for Visual Studio as Administrator and confirm that you only have one instance of LocalDb.

Type sqllocaldb info to see your LocalDb instances.

enter image description here

I then followed this article and had Application Pool Identity still set to my currently logged in user, credit: https://mcmap.net/q/591416/-how-to-allow-iis-to-use-local-database-from-asp-net-mvc-project

https://learn.microsoft.com/en-us/archive/blogs/sqlexpress/using-localdb-with-full-iis-part-1-user-profile

In there I found that it is not enough to have Load User Profile set to true for your Application Pool, you also need to set setProfileEnvironment to true in applicationHost.config normally located at C:\Windows\System32\inetsrv\config. With this configuration it worked:

enter image description here

Sandrocottus answered 9/7, 2020 at 8:43 Comment(1)
This worked for me. +1. Thank you. I am hosting my website from my own PC. Using IIS on Windows 10 and SQL Express.Overset

© 2022 - 2024 — McMap. All rights reserved.