efcore icon indicating copy to clipboard operation
efcore copied to clipboard

System.TypeInitializationException Unable to load DLL : sni.dll when attempting to get SQL data but DLL is present

Open dan-dragan opened this issue 10 years ago • 26 comments

This is an issue in DNX and dotnet CLI (tracked by aspnet/dnx#3316 & dotnet/cli#881). We are leaving this issue open to track the high level scenario on the EF side and ensure everything works end-to-end for the RC2 release.


Stack trace:
Exception thrown: 'System.TypeInitializationException' in EntityFramework.Core.dll
Exception caught: The type initializer for 'System.Data.SqlClient.TdsParser' threw an exception.Exception caught: The type initializer for 'System.Data.SqlClient.SNILoadHandle' threw an exception.Exception caught: Unable to load DLL 'C:\Users\ddragan\.dnx\packages\runtime.win7-x64.System.Data.SqlClient.sni\4.0.0-beta-23616\runtimes\win7-x64\native\sni.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)Exception thrown: 'System.TypeInitializationException' in Platform.Authentication
Exception thrown: 'System.TypeInitializationException' in mscorlib.ni.dll
Microsoft.AspNet.Diagnostics.ExceptionHandlerMiddleware:Error: An unhandled exception has occurred: The type initializer for 'System.Data.SqlClient.TdsParser' threw an exception.
System.TypeInitializationException: The type initializer for 'System.Data.SqlClient.TdsParser' threw an exception. ---> System.TypeInitializationException: The type initializer for 'System.Data.SqlClient.SNILoadHandle' threw an exception. ---> System.DllNotFoundException: Unable to load DLL 'C:\Users\ddragan\.dnx\packages\runtime.win7-x64.System.Data.SqlClient.sni\4.0.0-beta-23616\runtimes\win7-x64\native\sni.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
   at System.Runtime.Loader.AssemblyLoadContext.InternalLoadUnmanagedDllFromPath(String unmanagedDllPath)
   at Microsoft.Dnx.Runtime.Loader.PackageAssemblyLoader.LoadUnmanagedLibrary(String name)
   at Microsoft.Dnx.Host.LoaderContainer.LoadUnmanagedLibrary(String name)
   at Microsoft.Dnx.Runtime.Loader.LoadContext.LoadUnmanagedDll(String unmanagedDllName)
   at System.Runtime.Loader.AssemblyLoadContext.ResolveUnmanagedDll(String unmanagedDllName, IntPtr gchManagedAssemblyLoadContext)
   at System.Data.SqlClient.SNINativeMethodWrapper.SNIInitialize(IntPtr pmo)
   at System.Data.SqlClient.SNILoadHandle..ctor()
   at System.Data.SqlClient.SNILoadHandle..cctor()
   --- End of inner exception stack trace ---
   at System.Data.SqlClient.TdsParser..cctor()
   --- End of inner exception stack trace ---
   at ExternalDataAccessor.GetFirm(String firmName)

runtime info :

D:\Work\repo\Web>dnvm list

Active Version           Runtime Architecture OperatingSystem Alias
------ -------           ------- ------------ --------------- -----
       1.0.0-beta5       clr     x64          win
       1.0.0-beta5       clr     x86          win
       1.0.0-beta5       coreclr x64          win
       1.0.0-beta5       coreclr x86          win
       1.0.0-beta6       clr     x64          win
       1.0.0-beta6       clr     x86          win
       1.0.0-beta6       coreclr x64          win
       1.0.0-beta6       coreclr x86          win
       1.0.0-beta8       clr     x64          win
       1.0.0-beta8       clr     x86          win
       1.0.0-beta8       coreclr x64          win
       1.0.0-beta8       coreclr x86          win
       1.0.0-rc1-final   clr     x86          win
       1.0.0-rc1-final   coreclr x64          win
       1.0.0-rc1-final   coreclr x86          win
       1.0.0-rc1-update1 clr     x64          win
       1.0.0-rc1-update1 clr     x86          win             default
       1.0.0-rc1-update1 coreclr x64          win
       1.0.0-rc1-update1 coreclr x86          win
       1.0.0-rc2-16258   clr     x86          win

Using VS2015 debugger: dnx runtime

project.json content:

{
  "webroot": "wwwroot",
  "version": "1.0.0-beta1-*",

  "content": "EnvironmentSettings.json",

  "dependencies": {

    "Platform.AspNet.Authentication.Diagnostics": "1.0.0-beta1-*",
    "Platform.AspNet.Authentication.DPerms": "1.0.0-beta1-*",
    "Platform.AspNet.Discovery": "1.0.0-beta1-*",
    "Platform.AspNet.Monitoring": "1.0.0-beta1-*"
  },

  "commands": {
    "kestrel": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.Kestrel --server.urls http://*:5004"
  },

  "frameworks": {
    "dnxcore50": {
      "dependencies": {
        "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
        "Microsoft.AspNet.Mvc.Core": "6.0.0-rc1-final",
        "Microsoft.AspNet.Mvc.ViewFeatures": "6.0.0-rc1-final",
        "Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final",
        "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
        "Microsoft.AspNet.Server.WebListener": "1.0.0-rc1-final",
        "Microsoft.Extensions.DependencyInjection.Abstractions": "1.0.0-rc1-final",
        "Platform.Authentication": "1.0.0-rc1-final",
        "Microsoft.Extensions.OptionsModel": "1.0.0-rc1-final",
        "EntityFramework.Core": "7.0.0-rc1-final",
        "Microsoft.AspNet.Authentication": "1.0.0-rc1-final",
        "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
        "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
        "Microsoft.AspNet.FileProviders.Physical": "1.0.0-rc1-final",
        "Microsoft.Extensions.Configuration.Binder": "1.0.0-rc1-final",
        "Microsoft.AspNet.Hosting.Abstractions": "1.0.0-rc1-final",

        "Microsoft.AspNet.Identity": "3.0.0-rc1-final",
        "Microsoft.AspNet.Identity.EntityFramework": "3.0.0-rc1-final",
        "Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final",
        "Microsoft.Extensions.Logging.Debug": "1.0.0-rc1-final",
        "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc1-final",
        "Microsoft.AspNet.Diagnostics": "1.0.0-rc1-final",
        "System.Linq": "4.0.1-beta-*"
      }
    }
  },

  "publishExclude": [
    "node_modules",
    "bower_components",
    "**.xproj",
    "**.user",
    "**.vspscc"
  ],
  "exclude": [
    "wwwroot",
    "node_modules",
    "bower_components"
  ]
}

File exists: fileexists

Thanks for looking into this:

dan-dragan avatar Dec 22 '15 23:12 dan-dragan

:-(

dan-dragan avatar Dec 30 '15 19:12 dan-dragan

Duplicate of https://github.com/aspnet/EntityFramework/issues/3787 ? (see also https://github.com/dotnet/corefx/issues/3760)

natemcmaster avatar Dec 30 '15 23:12 natemcmaster

Don't think so. Firstly I have VS2012 installed and so its runtime is there so the workaround is in place . Second, unlike what the other issues say, in my case, the dll is not missing but actually there.

Thanks for looking into this, I am blocked on a live project.

-------- Original message --------
From: Nate McMaster [email protected]
Date:12/30/2015 5:41 PM (GMT-06:00)
To: aspnet/EntityFramework [email protected]
Cc: dan-dragan [email protected]
Subject: Re: [EntityFramework] System.TypeInitializationException Unable to load DLL : sni.dll when attempting to get SQL data but DLL is present (#4162)
Duplicate of #3787 ?

— Reply to this email directly or view it on GitHub.

dan-dragan avatar Dec 31 '15 02:12 dan-dragan

@dan-dragan The error most likely comes from not having the C++ redistributable . It could be that the x64 version is missing. It appears you are using .NET Core x86 on the command line and x64 in Visual Studio. Can you please ensure x86 and x64 are installed? https://www.microsoft.com/en-us/download/details.aspx?id=30679

natemcmaster avatar Dec 31 '15 17:12 natemcmaster

As I said, I had them both but did install again from the linked location msvcrt2012x32installed msvcrt2012x64installed

same result. Is it a matter of access rights perhaps? As I said, the file is in place.

dan-dragan avatar Jan 01 '16 00:01 dan-dragan

Good day, I'm still aslo having the exact same issue. The sni.dll file is indeed pressend, but somehow is not exesable when I run my web app with IIS. But I would like to add that this issue only occurs if I run the web app with IIS. If I run the web app on the production server with the “web” command manually (after stopping it in IIS), then I can access the web app and get database connections without any issues.

I’m running on coreclr-x64-rc1-update1 with IIS 7.5 on Windows Server 2008 R2 with C++ redistributable installed.

juswes avatar Jan 04 '16 13:01 juswes

@juswes your issue may be the same as https://github.com/dotnet/corefx/issues/4503 (resolved with https://github.com/aspnet/Hosting/issues/481)

Also, please note that code and issues for System.Data.SqlClient live in https://github.com/dotnet/corefx. There have been many bug fixes for SqlClient since RC1. These fixes are available in the RC2 nightly builds.

There is no actionable item for the Entity Framework.

natemcmaster avatar Jan 04 '16 18:01 natemcmaster

From the stack trace it looks like this is actually an EF scenario and not just raw SQL Client

Exception thrown: 'System.TypeInitializationException' in EntityFramework.Core.dll

Even if the underlying issue is in SQL Client, we should still keep this active to track making sure the EF scenario gets fixed.

rowanmiller avatar Jan 04 '16 18:01 rowanmiller

More info: I can only reproduce "Exception thrown: 'System.TypeInitializationException' in EntityFramework.Core.dll" in Win7 by running without C++ redistributable. This issue continues appear for some users even with vcredist installed. It could be a DNX issue, IIS and permissions, PATH problems, or something else. I have been unable to reproduce the error with C++ redistributable installed.

I am blocked on a live project.

@dan-dragan have you tried using full .NET instead? In VS, this is ".NET Framework" instead of ".NET Core". Using dnvm, this can be changed by executing dnvm use 1.0.0-rc1-update1 -r clr

natemcmaster avatar Jan 04 '16 19:01 natemcmaster

Thanks again for looking into this matter. No, I have not tried using full .NET, since I need multiple OS deployment capabilities. I will try it and let you know - for the purpose of troubleshooting ....

dan-dragan avatar Jan 04 '16 19:01 dan-dragan

I need multiple OS deployment capabilities

@dan-dragan heads up: SqlClient on .NET Core for Linux has an issue with MARS and with package restore. RC2 should fix these issues.

natemcmaster avatar Jan 04 '16 19:01 natemcmaster

Ack, thanks for the heads up.

dan-dragan avatar Jan 04 '16 20:01 dan-dragan

I thought I've already added this, but turns out I did not : dependency walker on the sni.dll in questions. All is not good :-( sni dll_dependencies

dan-dragan avatar Jan 04 '16 20:01 dan-dragan

@dan-dragan the "missing" dependencies you are seeing in dependency walker are in %USERPROFILE%/.dnx/runtimes/dnx-coreclr-win-x64.1.0.0-rc1-update1/bin/ and are loaded by DNX.

cc @victorhurdugaci @Tratcher do we have known downlevel issues in hosting/dnx regarding loading these runtime dependencies?

natemcmaster avatar Jan 04 '16 21:01 natemcmaster

@natemcmaster well, that sinks one promising theory/explanation...

dan-dragan avatar Jan 04 '16 22:01 dan-dragan

Hey,

Could someone who is seeing this issue confirm that they see it with just SQL Client (and no EF). Here is some sample code you could run to use SQL Client against LocalDb. You shouldn't need to install any packages as SQL Client is brought in by EF.

using (var conn = new SqlConnection(@"Server=(localdb)\mssqllocaldb;Database=master;Trusted_Connection=True;"))
{
    var cmd = conn.CreateCommand();
    cmd.CommandText = "SELECT COUNT(*) FROM sys.objects";
    conn.Open();
    var count = cmd.ExecuteScalar();
    conn.Close();
}

~Rowan

rowanmiller avatar Jan 07 '16 19:01 rowanmiller

@rowanmiller I've pretty much exactly the same issue & setup as @juswes - coreclr-x64-rc1-update1 with IIS 7.5 on Windows Server 2008 R2 with C++ redistributable installed (x64 & x86).

I'm just using SQL Client directly no EF.

SQL calls run fine if I start the app on the server with dnx web on localhost:5000. When I publish and try using IIS, SNI.dll issue occurs.

Before installing the C++ redistributable, SNI.dll issue happened with both dnx web and IIS. After installing the redistributable, dnx web was fixed but not IIS.

Tried with 1.0.0-rc2-16357 coreclr x64 with no success.

JohnBicknell avatar Jan 08 '16 13:01 JohnBicknell

Opened https://github.com/dotnet/corefx/issues/5252 to move this on to SQL Client's plate as there is nothing EF related here. Leaving this issue open though as this is a critical scenario for us and we want to track it to make sure it is fixed for RC2.

rowanmiller avatar Jan 08 '16 18:01 rowanmiller

So after reading through all the threads that this one was forked into my original issue seems to be a runtime PATH resolution problem.The location of the [correct runtime for your case] is not added to the PATH , which prevents loading of sni.dll because some dependencies of it - in my case API-MS-Win-Core-StringAnsi-L1-1-0.dll - cannot be located.

Workarounds (thanks to @jamesgibbs100) are:

  1. Copy the dependency dll(s) (that are matching your project settings (version/platform) as in coreclr-win-x64.1.0.0-rc1-final) in your project root folder.

or

2)Add the runtime path (in my case : C:\Users\ddragan.dnx\runtimes\dnx-coreclr-win-x64.1.0.0-rc1-final\bin ) to your path via command line tool before starting VS from the same command line instance (to preserve the setting).

I don't know what the final resolution might be, most threads that this one was referred to are closed now.

dan-dragan avatar Jan 19 '16 20:01 dan-dragan

BTW the issues currently tracking the root cause are aspnet/dnx#3316 and dotnet/cli#881

rowanmiller avatar Jan 19 '16 20:01 rowanmiller