开发者

Managed WMI Event class is not an event class?

I am using directions from here: http://msdn.microsoft.com/en-us/library/ms257351(VS.80).aspx to create a managed event class. Here's the code that I wrote:

[ManagementEntity]
[InstrumentationClass(InstrumentationType.Event)]
public class MyEvent
{
    [ManagementKey]
    public string ID { get; set; }
    [ManagementEnumerator]
    static public IEnumerable<MyEvent> EnumerateInstances()
    {
        var e = new MyEvent() { ID = "9A3C1B7E-8F3E-4C54-8030-B0169DE922C6" };
        return new MyEvent[] { e };
    }
}

class Program
{
    static void Main(string[] args)
    {
        var thisAssembly = typeof(Program).Assembly;
        var wmi_installer = new AssemblyInstaller(thisAssembly, null);
        wmi_installer.Install(null);
        wmi_installer.Commit(null);

        InstrumentationManager.RegisterAssembly(thisAssembly);

        Console.Write("Press Enter...");
        Console.ReadLine();
        var e = new MyEvent() { ID = "A6144A9E-0667-415B-9903-220652AB7334" };
        Instrumentation.Fire(e);

        Console.Write("Press Enter...");
        Console.ReadLine();
        wmi_installer.Uninstall(null);
    }

}

I can run a program, and it properly installs. Using wbemtest.exe I can browse to the event, and "show mof":

[dynamic: ToInstance, provider("WmiTest, 
      Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")]
class MyEvent
{
    [read, key] string ID;
};

Notice, the class does not inherit from __ExtrinsicEvent, which is weird...

I can also run select * from MyEvent, and get the result. Instrumentation.Fire() also returns no error. However, when I'm trying to subscribe to event using "Notification Query" option, I'm getting 0x80041059

Number: 0x80041059

Facility: WMI

Description: Class is not an event cla开发者_运维知识库ss.

What am I doing wrong, and is there a correct way to create managed WMI event?


After some digging, I figured out what happened: apparently in framework 4, there has been a second "branch" of WMI classes and attributes introduced, which interferes with classic ones. All the sample code I found on the internet is written with .NET 2.0 WMI support in mind. I have not found a way with .NET 4 classes to inherit a class from __Event or __ExtrinsicEvent.

It has been very annoying to find that Microsoft introduced two incompatible branches of code into the same namespace, which not only don't work with each other, but they break each other's functionality.

At any rate, what I needed to do in order to fix the issue was essentially making sure my app is using .NET 2 code:

  • completely get rid of DefaultManagementInstaller derived class inside wmi assembly. Use DefaultManagementProjectInstaller.
  • use Instrumentation.RegisterAssembly instead of InstrumentationManager.RegisterAssembly
  • do some manual cleanup of WMI namespace on Uninstall(), since wmi classes are not properly removed from the namespace with .NET 2 api.
  • cope with the fact that it is impossible to make a field to be a [key] with .NET 2 api
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜