문제

나는 의도 된 클래스로 별도의 어셈블리를 만들었습니다. WMI를 통해 게시되었습니다.그런 다음 Windows Forms 응용 프로그램을 만들었습니다. 해당 어셈블리를 참조하고 클래스를 게시하려고 시도합니다.내가 할 때 클래스를 게시하면 유형의 예외가 발생합니다. System.Management.Instrumentation.WmiProviderInstallationException입니다.이 예외 메시지에는 "Exception of type 'System.Management.Instrumentation.WMIInfraException'이 발생했습니다.".나는 가지고있다 이것이 무엇을 의미하는지 전혀 모릅니다.나는 .Net2.0과 .Net3.5(sp1도 마찬가지)를 시도해 보았고 동일한 결과를 얻었습니다.

아래에는 내 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());
}
도움이 되었습니까?

해결책

나는 gacutil - installutil을 사용하여 클래스를 (dll로) 테스트했습니다.gacutil 부분은 작동했지만 installutil(실제로는 mofcomp)이 구문 오류에 대해 불평했습니다.

...

오류 구문 0X80044014:클래스 이름에 예상치 못한 문자가 있습니다(식별자여야 함).

컴파일러에서 오류 0x80044014를 반환했습니다.

...

그래서 클래스 이름을 'MyInterface'로 변경했습니다. installutil 부분은 작동했지만 클래스는 어떤 인스턴스도 반환하지 않았습니다.마지막으로 호스팅 모델을 네트워크 서비스로 변경하고 작동하게 했습니다.

다른 팁

요약하자면, 작동하는 최종 코드는 다음과 같습니다.

자체 어셈블리에 있는 공급자 클래스:

// 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 응용 프로그램 기본 양식은 공급자 클래스를 게시합니다.

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());
            }
        }
    }
}

웹 애플리케이션 테스트, 소비자:

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();
        }
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top