Skip to content

Error regarding "Microsoft.Data.SqlClient.SNI.x64.dll" when running from UNC network path #1416

Closed

Description

@UweKeim

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.

Further information

In this MSDN Forums thread someone wrote:

...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.

Update 1

I've now created a Group Policy to ensure the Intranet Zone is trusted and ensured that the Group Policy is applied to the user under which the App Pool runs. Unfortunately this did not help.

Activity

  1. JRahnama commented on Dec 3, 2021

    @JRahnama
    Contributor

    @UweKeim we are looking into the issue. Will update here shortly.

  2. Kaur-Parminder commented on Dec 21, 2021

    @Kaur-Parminder
    Contributor

    @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

  3. UweKeim commented on Dec 22, 2021

    @UweKeim
    Author

    Thank a lot. I've quickly stitched together this .NET Full Framework 4.8 console app:

    using Microsoft.Data.SqlClient;
    using System;
    using static System.Console;
    
    internal static class Program
    {
        private static void Main(string[] args)
        {
            try
            {
                if (args.Length < 2)
                {
                    Error.WriteLine("Wrong number of arguments.");
                    Error.WriteLine("Argument 1 = Connection string.");
                    Error.WriteLine("Argument 2 = SQL query.");
                    return;
                }
    
                var cs = args[0];
                var sql = args[1];
                WriteLine($"Connection string: '{cs}'.");
                WriteLine($"SQL: '{sql}'.");
    
                // --
    
                using (var connection = new SqlConnection(cs))
                {
                    var command = new SqlCommand(sql, connection);
    
                    command.Connection.Open();
                    var c = command.ExecuteNonQuery();
    
                    WriteLine($"ExecuteNonQuery returned {c}.");
                    WriteLine("Success. Finished.");
                }
            }
            catch (Exception ex)
            {
                Error.WriteLine("Error:");
                Error.WriteLine(ex.Message);
                Error.WriteLine(ex);
            }
        }
    }

    And just for reference purposes, I also stitched together this .NET Core 6 console app:

    using Microsoft.Data.SqlClient;
    using static System.Console;
    
    try
    {
        if (args.Length < 2)
        {
            Error.WriteLine("Wrong number of arguments.");
            Error.WriteLine("Argument 1 = Connection string.");
            Error.WriteLine("Argument 2 = SQL query.");
            return;
        }
    
        var cs = args[0];
        var sql = args[1];
        WriteLine($"Connection string: '{cs}'.");
        WriteLine($"SQL: '{sql}'.");
    
        // --
    
        using var connection = new SqlConnection(cs);
    
        var command = new SqlCommand(sql, connection);
    
        command.Connection.Open();
        var c = command.ExecuteNonQuery();
    
        WriteLine($"ExecuteNonQuery returned {c}.");
        WriteLine("Success. Finished.");
    }
    catch (Exception ex)
    {
        Error.WriteLine("Error:");
        Error.WriteLine(ex.Message);
        Error.WriteLine(ex);
    }

    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.

  4. Kaur-Parminder commented on Jan 20, 2022

    @Kaur-Parminder
    Contributor

    @UweKeim Did you get chance to try to play with IIS application pool user?

  5. UweKeim commented on Jan 21, 2022

    @UweKeim
    Author

    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 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.

  6. Kaur-Parminder commented on Jan 21, 2022

    @Kaur-Parminder
    Contributor

    @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.

  7. added
    External 🔗
    and removed on Jan 21, 2022
  8. UweKeim commented on Jan 22, 2022

    @UweKeim
    Author
  9. UweKeim commented on Jan 22, 2022

    @UweKeim
    Author

    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 DllImport of 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:

    [DllImport(@"kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern bool SetDllDirectory(string lpPathName);

    (See also this MSDN article about DLL load order)

    Then, early in my Global.asax.cs Application_Start method (before any DB calls), call it like this:

    SetDllDirectory(Server.MapPath(@"~/bin"))

    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 SetDllDirectory multiple 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:

    var bitness = Environment.Is64BitProcess ? @"x64" : @"x86";
    SetDllDirectory(Server.MapPath($@"~/bin/{bitness}"));

    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.

  10. Kaur-Parminder commented on Jan 24, 2022

    @Kaur-Parminder
    Contributor

    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.

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    External 🔗

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions