C# 如何将dll文件依赖项复制到Azure函数应用程序的临时编译文件夹?

C# 如何将dll文件依赖项复制到Azure函数应用程序的临时编译文件夹?,c#,xml,azure,dll,C#,Xml,Azure,Dll,我正在使用一个使用第三方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.N

我正在使用一个使用第三方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\d361614284\u 4078d301\ThirdParty.dll是问题中的dll所在的路径,它与xml的预期位置一致。

虽然这不是问题的真正答案,但解决此问题的方法是在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");
                    }
                }
            }
        }
    }
}

感谢DoubleHolo提供此解决方案。它运行良好。 我更改了代码,只添加了路径。合并以简化代码

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"));
                            }
                        }

                    }
                }
            }
        }

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