Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using SqliteConnection from an ASP.NET MVC (.NET Framework) application results in a few different exceptions #32614

Closed
thatnerdyguy opened this issue Dec 14, 2023 · 4 comments · Fixed by #32938
Labels
area-adonet-sqlite closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression Servicing-approved type-bug
Milestone

Comments

@thatnerdyguy
Copy link

Example project from following steps below: WebApplication1.zip

  1. (In Visual Studio) Create a new ASP.NET Web Application (.NET Framework), chose MVC
  2. Add a NuGet reference to Microsoft.Data.Sqlite, version 8.0.0
  3. In the HomeController.cs, change the Index() action to:
        public ActionResult Index()
        {
            using (var conn = new SqliteConnection())
            {
                System.Console.WriteLine("SqliteConnection was created!");
            }

            return View();
        }
  1. Run it locally under Debug, and you get something like:
Library e_sqlite3 not found
plat: win
suffix: DLL
possibilities (2):
   1) C:\Users\TheUser\AppData\Local\Temp\Temporary ASP.NET Files\vs\68a120e0\7682694\assembly\dl3\8ffaeae5\008bde07_f1d5d901\runtimes\win-x64\native\e_sqlite3.dll
   2) C:\Users\TheUser\AppData\Local\Temp\Temporary ASP.NET Files\vs\68a120e0\7682694\assembly\dl3\8ffaeae5\008bde07_f1d5d901\e_sqlite3.dll
win TryLoad: C:\Users\TheUser\AppData\Local\Temp\Temporary ASP.NET Files\vs\68a120e0\7682694\assembly\dl3\8ffaeae5\008bde07_f1d5d901\runtimes\win-x64\native\e_sqlite3.dll
thrown: System.ComponentModel.Win32Exception (0x80004005): The specified module could not be found
  at SQLitePCL.NativeLibrary.TryLoad(String name, Loader plat, Action`1 log, IntPtr& h)
win TryLoad: C:\Users\TheUser\AppData\Local\Temp\Temporary ASP.NET Files\vs\68a120e0\7682694\assembly\dl3\8ffaeae5\008bde07_f1d5d901\e_sqlite3.dll
thrown: System.ComponentModel.Win32Exception (0x80004005): The specified module could not be found
  at SQLitePCL.NativeLibrary.TryLoad(String name, Loader plat, Action`1 log, IntPtr& h)
NOT FOUND

Now, even if you get around that (we got around it here by doing a runtime copy of e_sqlite.dll to the shadow copy location of SQLitePCLRaw.core.DLL), you can still get exceptions (as of version 8.0.0) from the type initialization of SqliteConnection, which you can approximate (in the attached example project), but uncommenting the ApproximateSqlConnectionStaticCtor() call in HomeController.Index(). In which case, you get:

[ArgumentException: The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))]
   System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName, ObjectHandleOnStack type) +0
   System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName) +96
   System.Type.GetType(String typeName) +62
   WebApplication1.Controllers.HomeController.ApproximateSqlConnectionStaticCtor() in C:\Code\WebApplication1\Controllers\HomeController.cs:50
   WebApplication1.Controllers.HomeController.Index() in C:\Code\WebApplication1\Controllers\HomeController.cs:18
   lambda_method(Closure , ControllerBase , Object[] ) +87
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +35
   System.Web.Mvc.Async.<>c.<BeginInvokeSynchronousActionMethod>b__9_0(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +39
   System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +77
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +42
   System.Web.Mvc.Async.<>c__DisplayClass11_0.<InvokeActionMethodFilterAsynchronouslyRecursive>b__0() +80
   System.Web.Mvc.Async.<>c__DisplayClass11_2.<InvokeActionMethodFilterAsynchronouslyRecursive>b__2() +396
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +42
   System.Web.Mvc.Async.<>c__DisplayClass3_6.<BeginInvokeAction>b__4() +50
   System.Web.Mvc.Async.<>c__DisplayClass3_1.<BeginInvokeAction>b__1(IAsyncResult asyncResult) +188
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +38
   System.Web.Mvc.<>c.<BeginExecuteCore>b__152_1(IAsyncResult asyncResult, ExecuteCoreState innerState) +29
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +73
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +52
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +39
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +38
   System.Web.Mvc.<>c.<BeginProcessRequest>b__20_1(IAsyncResult asyncResult, ProcessRequestState innerState) +43
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +73
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +38
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +646
   System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +182
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +134

at

Line 48:                 try
Line 49:                 {
Line 50:                     appDataType = Type.GetType("Windows.Storage.ApplicationData, Windows, ContentType=WindowsRuntime")
Line 51:                         ?? Type.GetType("Windows.Storage.ApplicationData, Microsoft.Windows.SDK.NET");
Line 52: 

Include version information

Microsoft.Data.Sqlite version: 8.0.0
Target framework: net48
Operating system: Windows

@ErikEJ
Copy link
Contributor

ErikEJ commented Dec 15, 2023

I had to run this in order to test:

Uninstall-package Microsoft.CodeDom.Providers.DotNetCompilerPlatform

After that, I was able to repro.. something like this would fix the breaking check (but still get the Library e_sqlite3 not found error)

catch (Exception ex)
{
    if (ex is FileLoadException || ex is ArgumentException)
    {
        // Ignore "Could not load assembly.";
        // Ignore "The parameter is incorrect."
    }
    else
    {
        throw;
    }
}

/cc @ericsink

@ajcvickers
Copy link
Member

Note for triage: requires VS to investigate.

@ericsink
Copy link

Using native code with .NET Framework is a never-ending fountain of edge cases.

@atrishin
Copy link

atrishin commented Jan 10, 2024

SqliteConnection from Microsoft.Data.Sqlite.Core package behaves differently in 8.x than it was in 7.x when it comes to .NET Framework IIS application running under app pool identity user.

In 7.x call Type.GetType("Windows.Storage.ApplicationData, Windows, ContentType=WindowsRuntime") would throw but it was in try/catch block

In 8.x same call throws but now it's catch (FileLoadException), not a generic catch leading to TypeInitializationException for SqliteConnection...

[System.ArgumentException]: The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
   at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName, ObjectHandleOnStack type)
   at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName)
   at System.Type.GetType(String typeName)
   at Microsoft.Data.Sqlite.SqliteConnection..cctor()

ajcvickers added a commit that referenced this issue Jan 27, 2024
…hat might not exist

Since it appears that they can exist but be the wrong version for us to use.

Fixes #32614
@ajcvickers ajcvickers self-assigned this Jan 27, 2024
@ajcvickers ajcvickers modified the milestones: 9.0.0, 8.0.x Jan 27, 2024
@ajcvickers ajcvickers added Servicing-consider closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. labels Jan 27, 2024
ajcvickers added a commit that referenced this issue Jan 28, 2024
…hat might not exist (#32938)

Since it appears that they can exist but be the wrong version for us to use.

Fixes #32614
@ajcvickers ajcvickers reopened this Jan 28, 2024
ajcvickers added a commit that referenced this issue Jan 28, 2024
…hat might not exist (#32938)

Since it appears that they can exist but be the wrong version for us to use.

Fixes #32614
@ajcvickers ajcvickers modified the milestones: 8.0.x, 8.0.3 Feb 2, 2024
@ajcvickers ajcvickers removed their assignment Aug 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-adonet-sqlite closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression Servicing-approved type-bug
Projects
None yet
5 participants