如何在.net中发布wmi类?

时间:2021-12-14 21:43:52

I've created a seperate assembly with a class that is intended to be published through wmi. Then I've created a windows forms app that references that assembly and attempts to publish the class. When I try to publish the class, I get an exception of type System.Management.Instrumentation.WmiProviderInstallationException. The message of the exception says "Exception of type 'System.Management.Instrumentation.WMIInfraException' was thrown.". I have no idea what this means. I've tried .Net2.0 and .Net3.5 (sp1 too) and get the same results.

我创建了一个单独的程序集,其中包含一个旨在通过wmi发布的类。然后我创建了一个引用该程序集的Windows窗体应用程序并尝试发布该类。当我尝试发布该类时,我得到System.Management.Instrumentation.WmiProviderInstallationException类型的异常。异常的消息说“抛出类型'System.Management.Instrumentation.WMIInfraException'的异常。”。我不知道这是什么意思。我已经尝试过.Net2.0和.Net3.5(也是sp1)并得到了相同的结果。

Below is my wmi class, followed by the code I used to publish it.

下面是我的wmi类,后面是我用来发布它的代码。

//Interface.cs in assembly WMI.Interface.dll

using System;
using System.Collections.Generic;
using System.Text;

[assembly: System.Management.Instrumentation.WmiConfiguration(@"root\Test",
    HostingModel = 
System.Management.Instrumentation.ManagementHostingModel.Decoupled)]

namespace WMI
{
    [System.ComponentModel.RunInstaller(true)]
    public class MyApplicationManagementInstaller :
        System.Management.Instrumentation.DefaultManagementInstaller { }

    [System.Management.Instrumentation.ManagementEntity(Singleton = true)]
    [System.Management.Instrumentation.ManagementQualifier("Description",
        Value = "Obtain processor information.")]
    public class Interface
    {
        [System.Management.Instrumentation.ManagementBind]
        public Interface()
        {
        }

        [System.Management.Instrumentation.ManagementProbe]
        [System.Management.Instrumentation.ManagementQualifier("Descriiption",
            Value="The number of processors.")]
        public int ProcessorCount
        {
            get { return Environment.ProcessorCount; }
        }
    }
}


//Button click in windows forms application to publish class
try
{
    System.Management.Instrumentation.InstrumentationManager.Publish(new 
WMI.Interface());
}
catch (System.Management.Instrumentation.InstrumentationException 
exInstrumentation)
{
    MessageBox.Show(exInstrumentation.ToString());
}
catch (System.Management.Instrumentation.WmiProviderInstallationException 
exProvider)
{
    MessageBox.Show(exProvider.ToString());
}
catch (Exception exPublish)
{
    MessageBox.Show(exPublish.ToString());
}

2 个解决方案

#1


0  

I used gacutil - installutil to to test your class (as a dll). The gacutil part worked, but installutil (actually mofcomp) complained about a syntax error:

我使用gacutil - installutil来测试你的类(作为一个DLL)。 gacutil部分工作,但installutil(实际上是mofcomp)抱怨语法错误:

...

error SYNTAX 0X80044014: Unexpected character in class name (must be an identifier)

错误语法0X80044014:类名中的意外字符(必须是标识符)

Compiler returned error 0x80044014

编译器返回错误0x80044014

...

So I changed the class name to 'MyInterface' the installutil part worked, but the class didn't return any instances. Finally I changed the hosting model to Network Service and got it to work.

所以我将类名更改为'MyInterface',即installutil部分工作,但是类没有返回任何实例。最后,我将托管模型更改为网络服务并使其工作。

#2


3  

To summarize, this is the final code that works:

总而言之,这是最终的代码:

Provider class, in it's own assembly:

Provider类,在它自己的程序集中:

// the namespace used for publishing the WMI classes and object instances 
[assembly: Instrumented("root/mytest")]

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Management.Instrumentation;
using System.Configuration.Install;
using System.ComponentModel;

namespace WMITest
{

    [InstrumentationClass(System.Management.Instrumentation.InstrumentationType.Instance)] 
    //[ManagementEntity()]
    //[ManagementQualifier("Description",Value = "Obtain processor information.")]
    public class MyWMIInterface
    {
        //[System.Management.Instrumentation.ManagementBind]
        public MyWMIInterface()
        {
        }

        //[ManagementProbe]
        //[ManagementQualifier("Descriiption", Value="The number of processors.")]
        public int ProcessorCount
        {
            get { return Environment.ProcessorCount; }
        }
    }

    /// <summary>
    /// This class provides static methods to publish messages to WMI
    /// </summary>
    public static class InstrumentationProvider
    {
        /// <summary>
        /// publishes a message to the WMI repository
        /// </summary>
        /// <param name="MessageText">the message text</param>
        /// <param name="Type">the message type</param>
        public static MyWMIInterface Publish()
        {
            // create a new message
            MyWMIInterface pInterface = new MyWMIInterface();

            Instrumentation.Publish(pInterface);

            return pInterface;
        }

        /// <summary>
        /// revoke a previously published message from the WMI repository
        /// </summary>
        /// <param name="Message">the message to revoke</param>
        public static void Revoke(MyWMIInterface pInterface)
        {
            Instrumentation.Revoke(pInterface);
        }        
    }

    /// <summary>
    /// Installer class which will publish the InfoMessage to the WMI schema
    /// (the assembly attribute Instrumented defines the namespace this
    /// class gets published too
    /// </summary>
    [RunInstaller(true)]
    public class WMITestManagementInstaller :
        DefaultManagementProjectInstaller
    {
    }
}

Windows forms application main form, publishes provider class:

Windows表单应用程序主窗体,发布提供程序类:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Management;
using System.Management.Instrumentation;

namespace WMI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        WMITest.MyWMIInterface pIntf_m;

        private void btnPublish_Click(object sender, EventArgs e)
        {
            try
            {
                pIntf_m = WMITest.InstrumentationProvider.Publish();
            }
            catch (ManagementException exManagement)
            {
                MessageBox.Show(exManagement.ToString());
            }
            catch (Exception exPublish)
            {
                MessageBox.Show(exPublish.ToString());
            }
        }
    }
}

Test web application, consumer:

测试Web应用程序,消费者:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Management.Instrumentation;
using System.Management;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ManagementClass pWMIClass = null;

            pWMIClass = new ManagementClass(@"root\interiorhealth:MyWMIInterface");

            lblOutput.Text = "ClassName: " + pWMIClass.ClassPath.ClassName + "<BR/>" +
                 "IsClass: " + pWMIClass.ClassPath.IsClass + "<BR/>" +
                 "IsInstance: " + pWMIClass.ClassPath.IsInstance + "<BR/>" +
                 "IsSingleton: " + pWMIClass.ClassPath.IsSingleton + "<BR/>" +
                 "Namespace Path: " + pWMIClass.ClassPath.NamespacePath + "<BR/>" +
                 "Path: " + pWMIClass.ClassPath.Path + "<BR/>" +
                 "Relative Path: " + pWMIClass.ClassPath.RelativePath + "<BR/>" +
                 "Server: " + pWMIClass.ClassPath.Server + "<BR/>";

            //GridView control
            this.gvProperties.DataSource = pWMIClass.Properties;
            this.gvProperties.DataBind();

            //GridView control
            this.gvSystemProperties.DataSource = pWMIClass.SystemProperties;
            this.gvSystemProperties.DataBind();

            //GridView control
            this.gvDerivation.DataSource = pWMIClass.Derivation;
            this.gvDerivation.DataBind();

            //GridView control
            this.gvMethods.DataSource = pWMIClass.Methods;
            this.gvMethods.DataBind();

            //GridView control
            this.gvQualifiers.DataSource = pWMIClass.Qualifiers;
            this.gvQualifiers.DataBind();
        }
    }
}

#1


0  

I used gacutil - installutil to to test your class (as a dll). The gacutil part worked, but installutil (actually mofcomp) complained about a syntax error:

我使用gacutil - installutil来测试你的类(作为一个DLL)。 gacutil部分工作,但installutil(实际上是mofcomp)抱怨语法错误:

...

error SYNTAX 0X80044014: Unexpected character in class name (must be an identifier)

错误语法0X80044014:类名中的意外字符(必须是标识符)

Compiler returned error 0x80044014

编译器返回错误0x80044014

...

So I changed the class name to 'MyInterface' the installutil part worked, but the class didn't return any instances. Finally I changed the hosting model to Network Service and got it to work.

所以我将类名更改为'MyInterface',即installutil部分工作,但是类没有返回任何实例。最后,我将托管模型更改为网络服务并使其工作。

#2


3  

To summarize, this is the final code that works:

总而言之,这是最终的代码:

Provider class, in it's own assembly:

Provider类,在它自己的程序集中:

// the namespace used for publishing the WMI classes and object instances 
[assembly: Instrumented("root/mytest")]

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Management.Instrumentation;
using System.Configuration.Install;
using System.ComponentModel;

namespace WMITest
{

    [InstrumentationClass(System.Management.Instrumentation.InstrumentationType.Instance)] 
    //[ManagementEntity()]
    //[ManagementQualifier("Description",Value = "Obtain processor information.")]
    public class MyWMIInterface
    {
        //[System.Management.Instrumentation.ManagementBind]
        public MyWMIInterface()
        {
        }

        //[ManagementProbe]
        //[ManagementQualifier("Descriiption", Value="The number of processors.")]
        public int ProcessorCount
        {
            get { return Environment.ProcessorCount; }
        }
    }

    /// <summary>
    /// This class provides static methods to publish messages to WMI
    /// </summary>
    public static class InstrumentationProvider
    {
        /// <summary>
        /// publishes a message to the WMI repository
        /// </summary>
        /// <param name="MessageText">the message text</param>
        /// <param name="Type">the message type</param>
        public static MyWMIInterface Publish()
        {
            // create a new message
            MyWMIInterface pInterface = new MyWMIInterface();

            Instrumentation.Publish(pInterface);

            return pInterface;
        }

        /// <summary>
        /// revoke a previously published message from the WMI repository
        /// </summary>
        /// <param name="Message">the message to revoke</param>
        public static void Revoke(MyWMIInterface pInterface)
        {
            Instrumentation.Revoke(pInterface);
        }        
    }

    /// <summary>
    /// Installer class which will publish the InfoMessage to the WMI schema
    /// (the assembly attribute Instrumented defines the namespace this
    /// class gets published too
    /// </summary>
    [RunInstaller(true)]
    public class WMITestManagementInstaller :
        DefaultManagementProjectInstaller
    {
    }
}

Windows forms application main form, publishes provider class:

Windows表单应用程序主窗体,发布提供程序类:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Management;
using System.Management.Instrumentation;

namespace WMI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        WMITest.MyWMIInterface pIntf_m;

        private void btnPublish_Click(object sender, EventArgs e)
        {
            try
            {
                pIntf_m = WMITest.InstrumentationProvider.Publish();
            }
            catch (ManagementException exManagement)
            {
                MessageBox.Show(exManagement.ToString());
            }
            catch (Exception exPublish)
            {
                MessageBox.Show(exPublish.ToString());
            }
        }
    }
}

Test web application, consumer:

测试Web应用程序,消费者:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Management.Instrumentation;
using System.Management;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ManagementClass pWMIClass = null;

            pWMIClass = new ManagementClass(@"root\interiorhealth:MyWMIInterface");

            lblOutput.Text = "ClassName: " + pWMIClass.ClassPath.ClassName + "<BR/>" +
                 "IsClass: " + pWMIClass.ClassPath.IsClass + "<BR/>" +
                 "IsInstance: " + pWMIClass.ClassPath.IsInstance + "<BR/>" +
                 "IsSingleton: " + pWMIClass.ClassPath.IsSingleton + "<BR/>" +
                 "Namespace Path: " + pWMIClass.ClassPath.NamespacePath + "<BR/>" +
                 "Path: " + pWMIClass.ClassPath.Path + "<BR/>" +
                 "Relative Path: " + pWMIClass.ClassPath.RelativePath + "<BR/>" +
                 "Server: " + pWMIClass.ClassPath.Server + "<BR/>";

            //GridView control
            this.gvProperties.DataSource = pWMIClass.Properties;
            this.gvProperties.DataBind();

            //GridView control
            this.gvSystemProperties.DataSource = pWMIClass.SystemProperties;
            this.gvSystemProperties.DataBind();

            //GridView control
            this.gvDerivation.DataSource = pWMIClass.Derivation;
            this.gvDerivation.DataBind();

            //GridView control
            this.gvMethods.DataSource = pWMIClass.Methods;
            this.gvMethods.DataBind();

            //GridView control
            this.gvQualifiers.DataSource = pWMIClass.Qualifiers;
            this.gvQualifiers.DataBind();
        }
    }
}