You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Error regarding "Microsoft.Data.SqlClient.SNI.x64.dll" when running from UNC network path #1416
When running an ASP.NET MVC with full .NET Framework 4.8 through IIS from an UNC network location (e.g. "\\myserver\myshare\mywebapp"), I get the following (German) error message:
Die DLL "Microsoft.Data.SqlClient.SNI.x64.dll": Das angegebene Modul wurde nicht gefunden. (Ausnahme von HRESULT: 0x8007007E) kann nicht geladen werden.
Manually translated to English, this should read:
The DLL "Microsoft.Data.SqlClient.SNI.x64.dll": The specified module could not be found (Exception from HRESULT: 0x8007007E) cannot be loaded.
I've ensured that the DLL is present.
The error went away after putting the DLL "Microsoft.Data.SqlClient.SNI.x64.dll" into a local path (i.e. not a network share) that is in the search path. For testing purposes, I've put it into "C:\Windows\System32". After that (and a reboot/IISRESET), the web application worked correctly.
...I bet the load failed due to lack of privileges for loading unmanaged code from a network share...
While I do not fully understand what this means, I've ensured that the CASPOL includes the network share, but this made no difference.
I've also looked into the SqlClient source code and found nothing special beside normal DllImport attributes that correctly point to "Microsoft.Data.SqlClient.SNI.x64.dll".
I even suspected the domain user under which the IIS Application Pools runs, but when giving this user Domain Admin permission, the erroneous behavior is the same.
My questions
Is there any known reason why this fails when running from an UNC share?
Is there any known workaround?
I'm thinking about implementing a call to AddDllDirectory in my web application (see also), but again I doubt that this is of any help at all.
To my surprise, this works correctly, even when serving the web application from an UNC location.
So I'm not sure, whether my initial web application does something wrong, or whether version 4.0.1 of your library has fixed something along the way.
Will let my co-workers later take a look at my test application and deployment.
Update 1:
I've accidentally created a .NET 6 (Core) application. The initial issue was a .NET Full Framework application.
Will change that now and test again.
Update 2:
With .NET Full Framework 4.8, the error occurs, too. Will check further now.
Update 3:
I'm completely confused now. Even when letting the App Pool run under the same user credentials as the user that can sucessfully run the console application via UNC path, the IIS website still reports the same error that it cannot load the native DLL.
The DLL "Microsoft.Data.SqlClient.SNI.x64.dll": The specified module could not be found (Exception from HRESULT: 0x8007007E) cannot be loaded.
The App Pool user is now a domain admin user and still no change in behavior.
I am really clueless on where to look next. IIS must do something different in terms of user permissions or .NET Framework restrictions or something like that.
It seems not restricted to only M.D.S driver but rather to native dlls for other packages as well.
I can suggest you to try SO workaround if any of this work for you. You can open issue on Microsoft Community portal to get further support. As this does not seem an issue with the SqlClient driver but an IIS issue with UNC path configuration, I am closing this as an external.
So the Shadow Copy thing was giving me the right hint:
The IIS Shadow Copy feature seems to omit copying native DLLs. Therefore the DllImport of Microsoft.Data.SqlClient cannot find the native library when being run by IIS in that very shadow copy folder.
I've also tried serving my test application from a local path (like "C:\Inetpub\wwwroot") and here, the error does not occur, even when not calling SetDllDirectory.
I'm still not sure why the error occurs for UNC paths only, and not for local paths, as I would expect that the shadow copied managed assemblies to fail the DllImports, too.
I'm still not sure why the error occurs for UNC paths only, and not for local paths, as I would expect that the shadow copied managed assemblies to fail the DllImports, too.
Activity
JRahnama commentedon Dec 3, 2021
@UweKeim we are looking into the issue. Will update here shortly.
Kaur-Parminder commentedon Dec 21, 2021
@UweKeim You have already relocated SNI dll to local path and it worked fine. Can you try running simple exe App on same network path which will use SNI and see if that App works? It will clarify if the issue is related to permissions. May be try to work with just exe and try to play with these permissions: https://stackoverflow.com/questions/2787203/unc-path-to-a-folder-on-my-local-computer
UweKeim commentedon Dec 22, 2021
Thank a lot. I've quickly stitched together this .NET Full Framework 4.8 console app:
And just for reference purposes, I also stitched together this .NET Core 6 console app:
To my surprise both apps it worked correctly out-of-the-box without even adjusting ACL permissions, when running with a domain admin account.
So my next step will be to test this again with the IIS application pool user and adjust permissions as the SO article that you linked to suggested.
Will post updates here.
Kaur-Parminder commentedon Jan 20, 2022
@UweKeim Did you get chance to try to play with IIS application pool user?
UweKeim commentedon Jan 21, 2022
Thanks, @Kaur-Parminder, I've just stitched together a small test application with Microsoft.Data.SqlClient version 4.0.1.
To my surprise, this works correctly, even when serving the web application from an UNC location.
So I'm not sure, whether my initial web application does something wrong, or whether version 4.0.1 of your library has fixed something along the way.
Will let my co-workers later take a look at my test application and deployment.
Update 1:
I've accidentally created a .NET 6 (Core) application. The initial issue was a .NET Full Framework application.
Will change that now and test again.
Update 2:
With .NET Full Framework 4.8, the error occurs, too. Will check further now.
Update 3:
I'm completely confused now. Even when letting the App Pool run under the same user credentials as the user that can sucessfully run the console application via UNC path, the IIS website still reports the same error that it cannot load the native DLL.
The App Pool user is now a domain admin user and still no change in behavior.
I am really clueless on where to look next. IIS must do something different in terms of user permissions or .NET Framework restrictions or something like that.
Kaur-Parminder commentedon Jan 21, 2022
@UweKeim I have come across multiple like issue:
Microsoft Community support: https://social.msdn.microsoft.com/Forums/en-US/d358833b-fa67-4cb6-b107-71863ab4ced0/loading-native-dll-in-iis-works-on-iis-express-exception-with-iis-8-badimageformatexception?forum=aspconfiganddeploy
GitHub: grpc/grpc#19666
and some possible workarounds on SO:
https://stackoverflow.com/questions/7996263/how-do-i-get-iis-to-load-a-native-dll-referenced-by-my-wcf-service
It seems not restricted to only M.D.S driver but rather to native dlls for other packages as well.
I can suggest you to try SO workaround if any of this work for you. You can open issue on Microsoft Community portal to get further support. As this does not seem an issue with the SqlClient driver but an IIS issue with UNC path configuration, I am closing this as an external.
UweKeim commentedon Jan 22, 2022
I do think, this one is related, too:
https://github.com/lscorcia/sqlclient.snishadowcopy
(I came there from this SO article)
UweKeim commentedon Jan 22, 2022
SOLVED IT (kind of)
So the Shadow Copy thing was giving me the right hint:
The IIS Shadow Copy feature seems to omit copying native DLLs. Therefore the
DllImportof Microsoft.Data.SqlClient cannot find the native library when being run by IIS in that very shadow copy folder.I then did the following.
Solution/workaround
Use P/Invoke to
SetDllDirectory:(See also this MSDN article about DLL load order)
Then, early in my Global.asax.cs
Application_Startmethod (before any DB calls), call it like this:After that, everything works as expected.
I still to consider this to be kind of a hack/workaround, but at least, it is working now in my scenario.
As I do understand, you can call
SetDllDirectorymultiple times to add additional directories (i.e. not overwrite the existing one).So in case someone reading this might have other assemblies that refer to native DLLs in "x86" or "x64" folders, one might do something like this:
I've also tried serving my test application from a local path (like "C:\Inetpub\wwwroot") and here, the error does not occur, even when not calling
SetDllDirectory.I'm still not sure why the error occurs for UNC paths only, and not for local paths, as I would expect that the shadow copied managed assemblies to fail the
DllImports, too.Kaur-Parminder commentedon Jan 24, 2022
Looks like Shadow Copy causes it, If you do not require shadow copy, one option is to disable it and there are many workarounds for the issue https://stackoverflow.com/questions/344608/unmanaged-dlls-fail-to-load-on-asp-net-server