繁体   English   中英

IIS 影子复制 dll 从 bin 位置访问依赖 dll

[英]IIS Shadow Copied dll accessing dependency dlls from bin location

我正在尝试使用 IIS 中的 EFCore 3.1.16 解决 ASP.Net Framework 4.8 站点的问题。 Microsoft.Data.SqlClient 在 SNI.dll 上具有进程锁定,这会导致 IIS 中的 xcopy 部署出现问题。

我尝试了一种将 SNI.dll 复制到与 Microsoft.Data.SqlClient 相同的卷影副本位置的策略,因此它不必尝试访问 bin 文件夹中的 DLL,如Z5E056://github://github.com/github.com/github.com/github.com/github lscorcia/sqlclient.snishadowcopy

// Look for the main Microsoft.Data.SqlClient assembly in the 
// shadow copy path
var sqlClientShadowAssembly = Directory.GetFiles(
currentShadowPath, "Microsoft.Data.SqlClient.dll",
SearchOption.AllDirectories).FirstOrDefault();

// Extract the directory information from the shadow assembly path
var sqlClientShadowPath = 
Path.GetDirectoryName(sqlClientShadowAssembly);

// Find out the process bitness and choose the appropriate native 
// assembly
var moduleName = Environment.Is64BitProcess ? "x86\\SNI.dll"
        : "x64\\SNI.dll";
// Compute the source and target paths for the native assembly
var sourceFile = Path.Combine(currentPrivatePath, moduleName);
var targetFile = Path.Combine(sqlClientShadowPath, "SNI.dll");
File.Copy(sourceFile, targetFile);

但是,它仍然会首先尝试访问 bin 位置,而不是位于同一文件夹位置的 sni.dll。

我已通过删除 dll 并确认引发了 FileNotFound 异常来检查影子位置中的 Microsoft.Data.SqlClient 是否正确使用。我还尝试直接复制到同一个文件夹并复制到阴影位置。

这是在我的场景中有效的解决方法。

使用 P/Invoke 到SetDllDirectory

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

(另请参阅有关 DLL 加载顺序的 MSDN 文章

然后,在我的 Global.asax.cs Application_Start方法的早期(在任何数据库调用之前),这样调用它:

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

之后,一切都按预期工作。


我仍然认为这是一种黑客/解决方法,但至少,它现在在我的场景中工作。

据我了解,您可以多次调用SetDllDirectory添加其他目录(即不覆盖现有目录)。

因此,如果有人阅读本文可能有其他程序集引用“x86”或“x64”文件夹中的本机 DLL,则可能会执行以下操作:

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

(我也在这个 GitHub 问题中发布了上述内容)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM