Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual studio 未从app.config运行时元素正确加载UseLegacyPathHandling_Visual Studio_Asp.net 4.5_.net 4.6.2 - Fatal编程技术网

Visual studio 未从app.config运行时元素正确加载UseLegacyPathHandling

Visual studio 未从app.config运行时元素正确加载UseLegacyPathHandling,visual-studio,asp.net-4.5,.net-4.6.2,Visual Studio,Asp.net 4.5,.net 4.6.2,我正在尝试在我的应用程序中使用新的长路径支持。为了使用它,在不强制客户机在其机器上安装最新的.net 4.6.2版本的情况下,只应将这些元素添加到app.config(有关更多信息,请参阅链接): 当我在执行项目中使用它时,它工作得非常好。问题在于我的测试项目(使用Nunit)。我将app.config添加到测试项目中的方式与将其添加到执行项目中的方式相同 使用ConfigurationManager类,我已经设法确保确实加载了app config(简而言之:使用我能够在单元测试中检索到的a

我正在尝试在我的应用程序中使用新的长路径支持。为了使用它,在不强制客户机在其机器上安装最新的.net 4.6.2版本的情况下,只应将这些元素添加到app.config(有关更多信息,请参阅链接):


当我在执行项目中使用它时,它工作得非常好。问题在于我的测试项目(使用Nunit)。我将app.config添加到测试项目中的方式与将其添加到执行项目中的方式相同

使用ConfigurationManager类,我已经设法确保确实加载了app config(简而言之:使用我能够在单元测试中检索到的app设置)

使用ConfigurationManager.GetSection(“runtime”),我甚至设法确保runtime元素已正确加载(_rawXml值与app.config中的值相同)

但是(!)由于某些原因,app config runtime元素不影响UseLegacyPathHandling变量,因此我所有的长路径调用都会失败

我想问题在于,测试项目变成了使用Nunit引擎加载的dll,Nunit引擎是执行入口点

我在另一个项目中面临着完全相同的问题,这是一个由Office Word应用程序加载的dll。我相信这两种情况下的问题是相同的,并且源于这样一个事实,即项目并不意味着是一个执行入口点

重要的是要了解,我无法访问他们自己(Word Office或Nunit)的应用程序,因此我无法自己配置它们


是否有一个选项可以使AppContextSwitchOverrides从零开始动态加载?其他想法将是最受欢迎的。谢谢。

我也遇到了同样的问题,并注意到该特定设置同样缺少加载

到目前为止,我得到的是,设置缓存至少是部分原因。 如果您检查它是如何实现的,那么禁用缓存对以后的值调用没有影响(即,如果启用了缓存并且在此期间访问了某些内容,那么它将始终被缓存)

对于大多数设置来说,这似乎不是一个问题,但由于某种原因,当我第一步进入代码时,UseLegacyPathHandling和BlockLongPaths设置正在被缓存

现在,我没有一个好的答案,但是如果你在这期间需要一些东西,我有一个非常可疑的临时解决方案。使用反射,可以在程序集初始化中修复该设置。它按名称写入私有变量,并使用特定值0使缓存无效,因此这是一个非常微妙的修复方法,不适合长期解决方案

这就是说,如果您需要暂时“正常工作”的东西,您可以检查设置,并根据需要应用黑客。 下面是一个简单的示例代码。这将是您在测试类中需要的方法

    [AssemblyInitialize]
    public static void AssemblyInit(TestContext context)
    {
        // Check to see if we're using legacy paths
        bool stillUsingLegacyPaths;
        if (AppContext.TryGetSwitch("Switch.System.IO.UseLegacyPathHandling", out stillUsingLegacyPaths) && stillUsingLegacyPaths)
        {
            // Here's where we trash the private cached field to get this to ACTUALLY work.
            var switchType = Type.GetType("System.AppContextSwitches"); // <- internal class, bad idea.
            if (switchType != null)
            {
                AppContext.SetSwitch("Switch.System.IO.UseLegacyPathHandling", false);   // <- Should disable legacy path handling

                // Get the private field that is used for caching the path handling value (bad idea).
                var legacyField = switchType.GetField("_useLegacyPathHandling", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
                legacyField?.SetValue(null, (Int32)0); // <- caching uses 0 to indicate no value, -1 for false, 1 for true.

                // Ensure the value is set.  This changes the backing field, but we're stuck with the cached value for now.
                AppContext.TryGetSwitch("Switch.System.IO.UseLegacyPathHandling", out stillUsingLegacyPaths);
                TestAssert.False(stillUsingLegacyPaths, "Testing will fail if we are using legacy path handling.");
            }
        }
    }
[AssemblyInitialize]
公共静态void AssemblyInit(TestContext上下文)
{
//检查是否使用传统路径
布尔仍然使用单通道;
if(AppContext.TryGetSwitch(“Switch.System.IO.UseLegacyPathHandling”,out StillusingleGacPaths)和&StillusingleGacPaths)
{
//在这里,我们丢弃私有缓存字段以使其实际工作。

var switchType=Type.GetType(“System.AppContextSwitches”);//同样值得注意的是,如果您签出用于app.config的样式表,rutime部分的processContents=“skip”属性。我想知道这是否会阻止处理此部分。进一步的调查显示,还有更多问题。AppContextDefault是在执行任何用户代码之前设置的,因此如果无法正确加载配置,则无法在不进行反射的情况下重新配置这些选项。此外,显然测试引擎es将使用测试引擎的.config文件(即单元测试会话窗口中的vstest.executionengine.x86.exe.config),而不是为程序集指定的文件。这解释了为什么忽略运行时部分(不需要,因为已经加载了一个)。不幸的是,我还没有找到解决方法。此外,如果您阅读了我的上一条评论,并想知道为什么我不只是在运行时加载配置,请查看AppContextSwitchs中如何实现缓存(一旦设置,如果您的配置指定为.NET4.6.1或更早版本,则无法修改缓存)。因此,如果您要设置AppContextDefaultValues.Defaults.cs中默认的5个“怪癖”中的任何一个,您将无法针对这些更改编写测试。
    [AssemblyInitialize]
    public static void AssemblyInit(TestContext context)
    {
        // Check to see if we're using legacy paths
        bool stillUsingLegacyPaths;
        if (AppContext.TryGetSwitch("Switch.System.IO.UseLegacyPathHandling", out stillUsingLegacyPaths) && stillUsingLegacyPaths)
        {
            // Here's where we trash the private cached field to get this to ACTUALLY work.
            var switchType = Type.GetType("System.AppContextSwitches"); // <- internal class, bad idea.
            if (switchType != null)
            {
                AppContext.SetSwitch("Switch.System.IO.UseLegacyPathHandling", false);   // <- Should disable legacy path handling

                // Get the private field that is used for caching the path handling value (bad idea).
                var legacyField = switchType.GetField("_useLegacyPathHandling", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
                legacyField?.SetValue(null, (Int32)0); // <- caching uses 0 to indicate no value, -1 for false, 1 for true.

                // Ensure the value is set.  This changes the backing field, but we're stuck with the cached value for now.
                AppContext.TryGetSwitch("Switch.System.IO.UseLegacyPathHandling", out stillUsingLegacyPaths);
                TestAssert.False(stillUsingLegacyPaths, "Testing will fail if we are using legacy path handling.");
            }
        }
    }