【问题标题】:How to copy dll file dependency to Temp compilation folder for Azure Function App?如何将 dll 文件依赖项复制到 Azure Function App 的临时编译文件夹?
【发布时间】:2018-06-03 06:54:46
【问题描述】:

我正在使用一个使用第三方 DLL 的 azure 函数应用程序,该应用程序依赖于与当前执行相关的文件夹中存在的 XML 映射文件。当我在 Azure 堆栈上发布和运行我的函数时,我遇到了一个异常,即 dll 无法加载 XML 文件。我的 bin 目录中存在带有 dll 的 XML,但 Azure 似乎正在将已编译的 dll 移动到没有所需 XML 的临时文件夹中,并继续根据以下异常消息查找相对于该临时路径的 XML :

"Could not find a part of the path 'D:\\local\\Temporary ASP.NET Files\\root\\da2a6178\\25f43073\\assembly\\dl3\\28a13679\\d3614284_4078d301\\Resources\\RepresentationSystem.xml'."

有什么方法可以确保这些附加文件也被复制到 Azure 正在运行的临时文件夹中?或者,我可以强制它从 bin 而不是 temp 运行吗?

更新:很遗憾,我不允许分享有关 dll 的任何信息。我能说的是所有内容都发布到我的 wwwroot 文件夹,但是当输出一些调试信息时,我可以看到执行是从“临时 ASP.NET 文件”文件夹中发生的。每个 dll 都被复制到自己的单独文件夹中。 D:\local\Temporary ASP.NET Files\root\da2a6178\25f43073\assembly\dl3\28a13679\d3614284_4078d301\ThirdParty.dll 是该路径是有问题的 dll,并且它与预期 xml 的位置一致.

【问题讨论】:

  • 使用KUDU,你会发现你的资源文件在D:\home\site\wwwroot下。而且你的函数执行目录和你部署到azure的内容目录不一样。它看起来像“D:\Program Files (x86)\SiteExtensions\Functions\1.0.11388”。我建议您更新您的问题并提供有关您的第三方 DLL 的更多详细信息,以便我们找到它如何读取 xml 文件。如果可能,您可以使用绝对路径来读取 xml 文件。
  • 您可以利用ILSpy自己检查第三方DLL中读取xml文件的代码来缩小这个问题。此外,您的 azure 功能在本地是否按预期工作?

标签: c# xml azure dll


【解决方案1】:

虽然这不是问题的真正答案,但解决此问题的方法是在 dll 函数运行之前在代码中添加一个函数,在 Temp ASP.Net 文件夹中搜索有问题的 dll,然后将 xml 文件从已知位置复制到该目录。

// Work Around Begin Here
string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
// Check if we are in temp dir
if (assemblyFolder.Contains("Temporary ASP.NET Files"))
{
    DirectoryInfo dir = new DirectoryInfo(assemblyFolder);
    // Go up 2 dirs
    DirectoryInfo top = dir.Parent.Parent;
    DirectoryInfo[] dirs = top.GetDirectories();
    foreach (DirectoryInfo child in dirs)
    {
        DirectoryInfo[] dirs2 = child.GetDirectories();
        foreach (DirectoryInfo child2 in dirs2)
        {
            // Find out if this is the Rep
            if (File.Exists(child2.FullName + "\\ThirdParty.Representation.dll"))
            {

                // Look to see if resource folder is there
                if (!Directory.Exists(child2.FullName + "\\Resources"))
                {
                        child2.CreateSubdirectory("Resources");
                }

                DirectoryInfo resDir = new DirectoryInfo(child2.FullName + "\\Resources");

                if (File.Exists(resourceDir + "RepresentationSystem.xml"))
                {
                    if(!File.Exists(resDir.FullName + "\\RepresentationSystem.xml"))
                    {

                        File.Copy(resourceDir + "RepresentationSystem.xml", resDir.FullName + "\\RepresentationSystem.xml");
                    }
                }

                if (File.Exists(resourceDir + "UnitSystem.xml"))
                {
                    if (!File.Exists(resDir.FullName + "\\UnitSystem.xml"))
                    {

                        File.Copy(resourceDir + "UnitSystem.xml", resDir.FullName + "\\UnitSystem.xml");
                    }
                }
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    感谢 DoubleHolo 提供此解决方法。它运行良好。 我已更改代码,仅添加 Path.Combine 以简化代码。

    private void CopyResourcesToTemporaryFolder()
    {
            // Work Around Begin Here
            string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
    
            string resourceDir = Path.Combine(FileUtils.WebProjectFolder, "Resources");
    
            // Check if we are in temp dir
            if (assemblyFolder.Contains("Temporary ASP.NET Files"))
            {
                DirectoryInfo dir = new DirectoryInfo(assemblyFolder);
                // Go up 2 dirs
                DirectoryInfo top = dir.Parent.Parent;
                DirectoryInfo[] dirs = top.GetDirectories();
                foreach (DirectoryInfo child in dirs)
                {
                    DirectoryInfo[] dirs2 = child.GetDirectories();
                    foreach (DirectoryInfo child2 in dirs2)
                    {
                        // Find out if this is the Rep
                        if (File.Exists(Path.Combine(child2.FullName, "AgGateway.ADAPT.Representation.DLL")))
                        {
    
                            // Look to see if resource folder is there
                            if (!Directory.Exists(Path.Combine(child2.FullName, "Resources")))
                            {
                                child2.CreateSubdirectory("Resources");
                            }
    
                            DirectoryInfo resDir = new DirectoryInfo(Path.Combine(child2.FullName, "Resources"));
    
                            if (File.Exists(Path.Combine(resourceDir, "RepresentationSystem.xml")))
                            {
                                if (!File.Exists(Path.Combine(resDir.FullName, "RepresentationSystem.xml")))
                                {
    
                                    File.Copy(Path.Combine(resourceDir, "RepresentationSystem.xml"), Path.Combine(resDir.FullName, "RepresentationSystem.xml"));
                                }
                            }
    
                            if (File.Exists(Path.Combine(resourceDir, "UnitSystem.xml")))
                            {
                                if (!File.Exists(Path.Combine(resDir.FullName, "UnitSystem.xml")))
                                {
    
                                    File.Copy(Path.Combine(resourceDir, "UnitSystem.xml"), Path.Combine(resDir.FullName, "UnitSystem.xml"));
                                }
                            }
    
                        }
                    }
                }
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-12
      • 1970-01-01
      • 2021-01-04
      • 2015-08-06
      • 1970-01-01
      • 2017-11-29
      • 1970-01-01
      相关资源
      最近更新 更多