10

我在这里有点头疼,我想知道是否有人知道答案。

设置基本上是这样的:

//in Visual Studio plug-in application
SpinUpProgramWithDebuggerAttached();

//in spun up program
void Start()
{
    StaticClass.StaticVariable = "I want to use this.";
    XmlSerializer.Deserialize(typeof(MyThingie), "xml");
}

class MyThingie : IXmlSerializable
{
     ReadXml()
     {
         //why the heck is this null?!?
         var thingIWantToUse = StaticClass.StaticVariable;
     }
}

让我大吃一惊的问题是,在 IXmlSerializable.ReadXml() 方法中,StaticClass.StaticVariable 为空,即使在设置变量后立即调用它。

值得注意的是,没有命中断点,并且 Debugger.Launch() 在问题发生的确切位置被忽略。

神秘的是,我通过引发异常确定 AppDomain.CurrentDomain.FriendlyName 属性对于填充静态变量的位置与 null 是相同的!

为什么静态变量超出范围?!?这是怎么回事?!?如何共享我的变量?

编辑:

根据响应中的建议,我添加了一个静态构造函数,并让它执行 Debug.WriteLine。我注意到它被调用了两次,即使所有代码似乎都在同一个 AppDomain 中运行。这是我在输出窗口中看到的内容,我希望这将是一个有用的线索:

静态构造函数调用于:2015-01-26T13:18:03.2852782-07:00

...加载 'C:...\GAC_MSIL\System.Numerics\v4.0_4.0.0.0__b77a5c561934e089\System.Numerics.dll'...

...加载“Microsoft.GeneratedCode”...

...加载 'C:...\GAC_MSIL\System.Xml.Linq\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.Linq.dll'....

...加载 'C:\USERS...\APPDATA\LOCAL\MICROSOFT\VISUALSTUDIO\12.0EXP\EXTENSIONS...SharePointAdapter.dll'。已加载符号。

...加载“Microsoft.GeneratedCode”。

静态构造函数调用于:2015-01-26T13:18:03.5196524-07:00

附加细节:

这是实际代码,因为一些评论者认为它可能会有所帮助:

//this starts a process called "Emulator.exe"
var testDebugInfo = new VsDebugTargetInfo4
{
    fSendToOutputWindow = 1,
    dlo = (uint)DEBUG_LAUNCH_OPERATION.DLO_CreateProcess,
    bstrArg = "\"" + paramPath + "\"",
    bstrExe = EmulatorPath, 
    LaunchFlags = grfLaunch | (uint)__VSDBGLAUNCHFLAGS.DBGLAUNCH_StopDebuggingOnEnd | (uint)__VSDBGLAUNCHFLAGS.DBGLAUNCH_WaitForAttachComplete,
    dwDebugEngineCount = 0,
    guidLaunchDebugEngine = VSConstants.CLSID_ComPlusOnlyDebugEngine,
};

var debugger = Project.GetService(typeof(SVsShellDebugger)) as IVsDebugger4;
var targets = new[] { testDebugInfo };
var processInfos = new[] { new VsDebugTargetProcessInfo() };

debugger.LaunchDebugTargets4(1, targets, processInfos);

//this is in the emulator program that spins up
public partial class App : Application
{
    //***NOTE***: static constructors added to static classes.
    //Problem still occurs and output is as follows (with some load messages in between):
    //
    //MefInitializer static constructor called at: 2015-01-26T15:34:19.8696427-07:00
    //ContainerSingleton static constructor called at: 2015-01-26T15:34:21.0609845-07:00. Type: SystemTypes.ContainerSingleton, SystemTypes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=...
    //ContainerSingleton static constructor called at: 2015-01-26T15:34:21.3399330-07:00. Type: SystemTypes.ContainerSingleton, SystemTypes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=...

    protected override void OnStartup(StartupEventArgs e)
    {
         //...

         //initializes a MEF container singleton (stored as static variable)
         MefInitilizer.Run(); 

         //here's where it blows up. the important details are that 
         //FullSelection implements IXmlSerializable, and its implemention
         //ends up referencing the MEF container singleton, which ends up
         //null, even though it was initialized in the previous line.
         //NOTE: the approach works perfectly under a different context
         //so the problem is not the code itself, per se, but a problem
         //with the code in the environment it's running in.
         var systems = XmlSerialization.FromXml<List<FullSelection>>(systemsXml);
    }
 }

public static class MefInitilizer
{
    static MefInitilizer() { Debug.WriteLine("MefInitializer static constructor called at: " + DateTime.Now.ToString("o")); }

    public static void Run()
    {
        var catalog = new AggregateCatalog();
        //this directory should have all the defaults
        var dirCatalog = new DirectoryCatalog(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
        //add system type plug-ins, too
        catalog.Catalogs.Add(dirCatalog);

        var container = new CompositionContainer(catalog);
        ContainerSingleton.Initialize(container);
    }
}

public class ContainerSingleton
{
    static ContainerSingleton() 
    {
        Debug.WriteLine("ContainerSingleton static constructor called at: " + DateTime.Now.ToString("o") + ". Type: " + typeof(ContainerSingleton).AssemblyQualifiedName);
    }
    private static CompositionContainer compositionContainer;

    public static CompositionContainer ContainerInstance
    {
        get 
        {
            if (compositionContainer == null)
            {
                var appDomainName = AppDomain.CurrentDomain.FriendlyName;
                throw new Exception("Composition container is null and must be initialized through the ContainerSingleton.Initialize()" + appDomainName);
            }
            return compositionContainer; 
        }
    }

    public static void Initialize(CompositionContainer container)
    {
        compositionContainer = container;
    }
}
4

4 回答 4

2

请记住,我刚刚复制了您的代码以尝试复制您的问题。运行此代码时,我在 Debug.Write 上收到 NullReferenceException,AnotherClass 在调用解决之前没有正确初始化。

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            MefInitilizer.Run();
            Debug.Write(AnotherClass.Test);
        }
    }

    public class AnotherClass
    {
        public static String Test = ContainerSingleton.ContainerInstance;
    }

    public static class MefInitilizer
    {
        public static void Run()
        {
            ContainerSingleton.Initialize("A string");
        }
    }

    public class ContainerSingleton
    {
        private static String compositionContainer;

        public static String ContainerInstance
        {
            get
            {
                if (compositionContainer != null) return compositionContainer;

                var appDomainName = AppDomain.CurrentDomain.FriendlyName;
                throw new Exception("Composition container is null and must be initialized through the ContainerSingleton.Initialize()" + appDomainName);
            }
        }

        public static void Initialize(String container)
        {
            compositionContainer = container;
        }
    }


}

但是,当我将静态构造函数添加到所有具有静态字段的类时,它会按预期工作:

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            MefInitilizer.Run();

            Debug.Write(AnotherClass.Test);
        }
    }

    public class AnotherClass
    {
        static AnotherClass()
        {

        }

        public static String Test = ContainerSingleton.ContainerInstance;
    }

    public static class MefInitilizer
    {
        static MefInitilizer()
        {

        }
        public static void Run()
        {

            ContainerSingleton.Initialize("A string");
        }
    }

    public class ContainerSingleton
    {
        static ContainerSingleton()
        {

        }
        private static String compositionContainer;

        public static String ContainerInstance
        {
            get
            {
                if (compositionContainer != null) return compositionContainer;

                var appDomainName = AppDomain.CurrentDomain.FriendlyName;
                throw new Exception("Composition container is null and must be initialized through the ContainerSingleton.Initialize()" + appDomainName);
            }
        }

        public static void Initialize(String container)
        {
            compositionContainer = container;
        }
    }


}

我会说这肯定是一个 BeforeFieldInit 问题。

于 2015-01-26T22:12:50.847 回答
2

据我了解,您的代码是 Visual Studio 的插件,您的应用程序的主要问题是您的类被实例化了两次,一次是正常的AppDomain,一次是因为其他原因,您无法真正找出.

首先,我在这里看到sandboxing了 Visual Studio 的潜力——它希望以各种权限测试您的代码,以确保您的代码不会损害 Visual Studio 的任何其他部分或最终用户的工作。在这种情况下,您的代码可能会被加载到另一个 AppDomain 中,而无需某些权限(您可以在 MSDN 上找到一篇好文章),因此您可以理解为什么每个应用程序调用您的代码两次。

其次,我想指出您误解了 staticconstructor和 static的想法method

public static void Initialize(CompositionContainer container)
{
    compositionContainer = container;
}

不一样

public static ContainerSingleton()
{
    compositionContainer = container;
}

因此,我建议您将所有初始化逻辑移动到静态容器中,如下所示:

public class ContainerSingleton
{
    private static CompositionContainer compositionContainer;

    public static CompositionContainer ContainerInstance
    {
        get 
        {
            if (compositionContainer == null)
            {
                var appDomainName = AppDomain.CurrentDomain.FriendlyName;
                throw new Exception("Composition container is null and must be initialized through the ContainerSingleton.Initialize()" + appDomainName);
            }
            return compositionContainer; 
        }
    }

    public static ContainerSingleton()
    {
        var catalog = new AggregateCatalog();
        //this directory should have all the defaults
        var dirCatalog = new DirectoryCatalog(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
        //add system type plug-ins, too
        catalog.Catalogs.Add(dirCatalog);

        compositionContainer = new CompositionContainer(catalog);
    }
}

第二种方法:我想指出您用于获取单例的模式已过时,请尝试使用Lazy<T>该类,如下所示:

public class ContainerSingleton
{
    private static Lazy<CompositionContainer> compositionContainer;

    public static CompositionContainer ContainerInstance
    {
        get 
        {
            return compositionContainer.Value;
        }
    }

    public static ContainerSingleton()
    {
        compositionContainer = new Lazy<CompositionContainer>(() => Initialize());
    }
    public static void Initialize()
    {
         // Full initialization logic here
    }
}

此外,您应该记住,仅添加空的静态构造函数是不够的 - 您应该将所有分配移至它,因此您应该替换这样的代码:

public class AnotherClass
{
    static AnotherClass()
    {

    }

    public static String Test = ContainerSingleton.ContainerInstance;
}

有了这个:

public class AnotherClass
{
    static AnotherClass()
    {
        Test = ContainerSingleton.ContainerInstance;
    }

    public static String Test;
}

更新:

@Colin您甚至可以使用 [ LazyTasktype] [ https://msdn.microsoft.com/en-us/magazine/dn683795.aspx] - 只需将 a 传递Func给您的构造函数,这将是一种线程安全的方法,查看更多在文章中。相同IdAppDomain意思没有任何意义——沙箱可以通过AppDomain.ExecuteAssembly方法运行你的代码(它在 4.5 中已经过时,但仍然可能是一个可能的变体)来查看它在各种权限集中的行为。

.NET 4.5 中可能有另一种技术,但现在找不到相关的文章。

更新 2:

正如我在您的代码中看到的,您正在从磁盘读取一些信息。尝试为此添加代码访问安全规则,以查看您的代码是否在受限权限下运行,如下所示:

FileIOPermission f2 = new FileIOPermission(FileIOPermissionAccess.Read, Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
//f2.AddPathList(FileIOPermissionAccess.Write | FileIOPermissionAccess.Read, "C:\\example\\out.txt");
try
{
    f2.Demand();
}
catch (SecurityException s)
{
    Console.WriteLine(s.Message);
}

更多关于FileIOPermissionMSDN 上的类。

于 2015-01-26T22:51:31.693 回答
1

尝试将静态构造函数添加到ContainerSingleton. 我相信这是BeforeFieldInit再次抬起丑陋的头。

于 2015-01-26T19:26:29.033 回答
0

感谢所有提供建议的人!我从来没有弄清楚到底发生了什么(如果我知道的话,我会继续调查/发布更新),但我最终想出了一个解决方法。

一些人建议将静态类的初始化内部化,但由于实际问题的性质,必须将初始化逻辑外部化(我基本上是在加载一个 DI/服务位置容器,其组成因环境而异)。

另外,我怀疑它没有帮助,因为我可以观察到静态构造函数被调用了两次(因此,无论有什么初始化逻辑都只会被调用两次,这并没有直接解决问题)。

然而,这个建议让我走上了正轨。

就我而言,我加载的所有服务都不需要是有状态的,因此除了性能影响之外,初始化发生两次并不重要。

因此,我只是让静态类检查是否加载了 MEF 容器,如果没有加载,我会读取一个配置文件,其中指定了一个处理初始化的类。

通过这样做,我仍然可以在不同环境中改变 MEF 容器的组成,尽管它不是一个理想的解决方案,但它目前运行良好。

我想在所有提供帮助的人之间分配赏金,但由于这似乎不可能,我可能会奖励 OakNinja,因为他是一位英雄,根据我提供的信息,他吐出了尽可能多的好想法。假如。再次感谢!

于 2015-02-02T23:02:55.033 回答