Opening SqlConnection results in DllNotFoundException (Unable to load sni.dll) in case of executing from Visual Studio
works fine from command line
code
var connection = new SqlConnection("valid connection string");
connection.Open();
project.json:
{
"version": "1.0.0-*",
"description": "ConsoleApp10 Console Application",
"authors": [ "sady4850" ],
"tags": [ "" ],
"projectUrl": "",
"licenseUrl": "",
"compilationOptions": {
"emitEntryPoint": true
},
"dependencies": {
"System.Data.SqlClient": "4.0.0-beta-23516"
},
"commands": {
"ConsoleApp10": "ConsoleApp10"
},
"frameworks": {
"dnxcore50": {
"dependencies": {
"Microsoft.CSharp": "4.0.1-beta-23516",
"System.Collections": "4.0.11-beta-23516",
"System.Console": "4.0.0-beta-23516",
"System.Linq": "4.0.1-beta-23516",
"System.Threading": "4.0.11-beta-23516"
}
}
}
}
exception:
at System.Runtime.Loader.AssemblyLoadContext.InternalLoadUnmanagedDllFromPath(String unmanagedDllPath)
at System.Runtime.Loader.AssemblyLoadContext.LoadUnmanagedDllFromPath(String unmanagedDllPath)
at Microsoft.Dnx.Runtime.Loader.LoadContext.LoadUnmanagedLibraryFromPath(String path)
at Microsoft.Dnx.Runtime.Loader.PackageAssemblyLoader.LoadUnmanagedLibrary(String name)
at Microsoft.Dnx.Host.LoaderContainer.LoadUnmanagedLibrary(String name)
at Microsoft.Dnx.Host.DefaultLoadContext.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()
SQL Client: Unable to load sni.dll (DllNotFoundException)
see also dotnet/runtime#16048
Erm. Isn't this a duplicate of the issue I had open at https://github.com/aspnet/EntityFramework/issues/4162 that got merged into https://github.com/dotnet/corefx/issues/5252 that got closed but got tracked into aspnet/dnx#3316 and dotnet/cli#881 ?
Is this the main tracking issue? The few libs hitting this (runtime bindings?) are the biggest pain in RC1, I'd consider this high-priority for RC2.
Please note that DNX is getting replaced by the new .NET CLI (https://github.com/dotnet/cli), so we should verify if this is still an issue when running on the .NET CLI. We are not fixing DNX specific issues at this point.
Is there any post explaining the hole thing about dnx and cli. I'm a little confused about what will survive, what will die and what will be modified. How do we test if this problem exists in net cli?
You can get the latest .NET CLI build from http://dotnet.github.io.
I just verified that .NET CLI won't have this issue any more because the runtime will be set as local.
https://github.com/dotnet/cli/issues/881
What do you mean with "the runtime will be set as local"? Local to the application??
@kkurni Why are linking IIS publishing issue here ? This issue is not related to the IIS at all.
@airomero in Dotnet CLI, the runtime is published in the same folder (local) as the compiled application. this will make sure that the published runtime is used instead of installed runtime
In DNX, the runtime is published on different folder which need something to register this path to enviroment variables to use that runtime. (this can potentially conflict with the installed runtime.
@sady4850 this could be the same issue as DNX always assume to use installed runtime which registered in your "path" environment variables.
I cannot repro this issue while debugging console app in visual studio.

Here is my project.json

Try to troubleshoot this in your machine.
- Open Process Monitor to your console application.
- Check which DNX runtime that visual studio open. (is it using the correct runtime ?)

- Search for SNI.dll (Where Sni.dll is loaded ? Success /Fail ?)

- Search for stringansi-l1 (Check whether api-ms-win-core-stringansi-l1-1-0.dll is loaded ?) It should api-ms-win-core-stringansi-l1-1-0.dll from dnx runtime instead of packages.

- if it is failed to load stringansi-l1, check your environment variables. It should register dnx runtime path there.

If you still can't figure out, send me the screenshot of your process manager which shows when dnx is loaded, Sni.dll and stringansi.
@kkurni ye, there wasn't dnx path in PATH. Still an open question why it is disappeared from there (because it worked and obviously I didn't remove it manually)
To find out why it works on console app (without VS Debugger), you can attach process monitor as well on console application without using VS debugger. (check where sni.dll and stringansi.dll is loaded).
it will be hard to answer why this path is disappeared. probably some other installation of other frameworks / version which override / remove this runtime path.
I will close this as we have the root cause why we get SNI.dll error. And The DNX is being retired in favor of the new dotnet CLI command line tools