I've run into a problem where calling a method on a decoupled WMI provider (developed using the WMI Provider Extensions in .NET 3.5) throws an ExecutionEngineException when running more than one instance of my application. Has anyone run into this limitation before or am I doing something wrong?
I created a (simple) WMI provider and publish it the ctor of a (WinForms) form. If you run one instance of the application everything works great; however, if you run multiple instances of the application, one will crash when calling a WMI method.
Here's the WMI provider class (installer class, and form that publishes the WMI object) It's roughly based on this article.
[assembly: WmiConfiguration(@"root\foo", HostingModel = ManagementHostingModel.Decoupled)]
[ManagementEntity(Singleton = false)]
[ManagementQualifier("Description", Value = "Obtain processor information.")]
public class Test
{
private readonly int _id;
private int _value;
[ManagementBind]
public Test([ManagementName("Id")] int id)
{
_id = id;
}
[ManagementKey]
public int Id
{
get { return _id; }
}
[ManagementProbe]
[ManagementQualifier("Description", Value = "The number of processors.")]
public int Value
{
get { return _value; }
}
[ManagementTask]
public void Increment()
{
_value++;
}
public static Test Create()
{
int id = Process.GetCurrentProcess().Id;
var wmi = new Test(id);
return wmi;
}
}
/// <summary>
/// This installer is for the WMI Provider Extensions for .NET 3.5 support.
/// </summary>
public class LearningWmiManagementInstaller : DefaultManagementInstaller
{}
public partial class Form1 : Form
{
private Test _pi;
public Form1()
{
InitializeComponent();
_pi = Test.Create();
InstrumentationManager.Publish(_pi);
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
InstrumentationManager.Revoke(_pi);
}
}
Of course, I use installutil to run the WMI installer class. Next, startup two instances of the application. If you call the "Increment" WMI [ManagementTask] method using the wmic tool on one of the processes then the other will crash (oddly, not the one you invoke).
wmic /namespace:\\root\foo path Test.Id=<process-id> call increment
Any suggestions would be greatly appreciated.