我已经继续并实施了这个,它似乎工作正常。部署时没有锁定问题。
示例代码如下。您可能需要根据您的环境对其进行修改,它是为 OWIN 中托管的 Web API 2 编写的。
private void LoadSqlServerTypes()
{
var shadowCopyDirectoryPath = new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName;
var nativeDllDirectory = new DirectoryInfo(HostingEnvironment.MapPath("~/bin/SqlServerTypes"));
var nativeDllShadowCopyDirectory = Path.Combine(shadowCopyDirectoryPath, "SqlServerTypes");
if (Directory.Exists(nativeDllShadowCopyDirectory) == false)
{
nativeDllDirectory.CopyTo(nativeDllShadowCopyDirectory, true);
}
SqlServerTypes.Utilities.LoadNativeAssemblies(shadowCopyDirectoryPath);
}
该CopyTo
方法是https://docs.microsoft.com/en-us/dotnet/standard/io/how-to-copy-directories上代码的修改版本
public static partial class DirectoryInfoExtensions
{
/*
* Source adapted from https://docs.microsoft.com/en-us/dotnet/standard/io/how-to-copy-directories
*/
/// <summary>
/// Copy a directory and its contents to a specified location.
/// </summary>
/// <param name="sourceDirectory">The directory to copy.</param>
/// <param name="destinationDirectoryPath">The path of the new directory to create.</param>
/// <param name="recursive">True if subdirectories must be copied; otherwise, false.</param>
public static void CopyTo(this DirectoryInfo sourceDirectory, string destinationDirectoryPath, bool recursive)
{
if (Directory.Exists(destinationDirectoryPath) == false)
{
Directory.CreateDirectory(destinationDirectoryPath);
}
var files = sourceDirectory.GetFiles();
foreach (var file in files)
{
string destinationFilePath = Path.Combine(destinationDirectoryPath, file.Name);
file.CopyTo(destinationFilePath, false);
}
if (recursive == true)
{
var subdirectories = sourceDirectory.GetDirectories();
foreach (DirectoryInfo directory in subdirectories)
{
string destinationSubdirectoryPath = Path.Combine(destinationDirectoryPath, directory.Name);
CopyTo(new DirectoryInfo(directory.FullName), destinationSubdirectoryPath, recursive);
}
}
}
}