How To: Create a Basic Decoupled Provider

Decoupled WMI providers are hosted by an application. The WMI functionality they provide is only available while that application is running. This example is a console application that continues to run until you press ENTER. While it is running, it exposes WMI functionality. This simple environment is a great starting point when learning how to create decoupled providers by using WMI.NET provider extensions.

The following steps were taken to create and compile the code sample.

To create the sample

  1. Create a new Windows Console Application Visual Studio project.

  2. Use the WmiConfigurationAttribute assembly-level attribute to specify that the provider publishes classes to the root\ConsoleDecoupled namespace and uses a decoupled hosting model.

  3. Create a public class that derives from the installer class DefaultManagementInstaller. Use the RunInstallerAttribute attribute to specify that the Installer Tool (Installutil.exe) should call the Install method of the installer class.

  4. Create a public class, RuntimeConfigSettings, that includes a string member, ReadMe, that represents a read-only property of a WMI class.

  5. Use the ManagementEntityAttribute attribute to specify that the RuntimeConfigSettings class represents and implements a WMI class. In this case, the Singleton property is used to specify that the WMI class can only have a single corresponding instance.

  6. Use the ManagementProbeAttribute attribute to specify that the string member ReadMe is a read-only property of the WMI class.

  7. Create a constructor for RuntimeConfigSettings that sets the single ReadMe property of the class. Use the ManagementBindAttribute attribute to specify that the constructor binds a request for a WMI class instance to the appropriate corresponding instance. In the case of a singleton WMI class, the binding is as simple as possible. There can only ever be one instance. So, the constructor sets the ReadMe property to the value we want it to have in that single instance.

  8. In the Main method, use the RegisterType method of the InstrumentationManager class to begin hosting the WMI provider, exposing the RunTimeConfigSettings WMI class.

  9. Use ReadLine to keep the hosting program running until the user presses ENTER.

  10. Use the UnregisterType method of the InstrumentationManager class to stop hosting the WMI provider after the user has pressed ENTER.

Deploying the Decoupled WMI Provider

A decoupled provider is implemented within and hosted by your application. Beyond building and running your application, you still have to get information about the provider registered in the WMI repository. To do that, you run installutil against your program. Take the following steps to deploy a decoupled WMI provider.

To deploy a decoupled WMI provider

  1. Compile the hosting application.

  2. Run installutil against the application executable.

  3. Run the application.

Example

The following sample illustrates how to create a minimal, decoupled WMI provider by using the WMI.NET Provider Extensions framework.

using System;
using System.Management.Instrumentation;

[assembly: WmiConfiguration("root\\ConsoleDecoupled", HostingModel = ManagementHostingModel.Decoupled)]

[System.ComponentModel.RunInstaller(true)]
public class TheInstaller : DefaultManagementInstaller
{
}

namespace ConsoleDecoupled
{
    public class Program
    {
        public static void Main(string[] args)
        {
    InstrumentationManager.RegisterType(typeof(RuntimeConfigSettings));
            Console.WriteLine("Press enter to exit");
            Console.ReadLine();
  InstrumentationManager.UnregisterType(typeof(RuntimeConfigSettings));
        }
    }

    [ManagementEntity(Singleton = true)]
    public class RuntimeConfigSettings
    {
        [ManagementProbe]
        public string ReadMe;

        [ManagementBind]
        public RuntimeConfigSettings()
        {
            ReadMe = "StartValue";
        }
    }
}

You can extend this basic sample by including additional properties and methods in the RuntimeConfigSettings class. Any properties you add can be exposed as WMI properties by indicating whether they are read-only properties or read-write properties. You do this by using WMI.NET provider extension attributes. Use the ManagementProbeAttribute attribute for read-only properties and the ManagementConfigurationAttribute attribute for read-write properties. Any methods you add can be exposed as WMI methods. Use the ManagementTaskAttribute attribute to indicate that a method should be registered and implemented as a WMI method.

Compiling the Code

To compile this code, you must include references to System.Management.Instrumentation and System.Configuration.Install.

See Also

Reference

System.Management.Instrumentation

Send comments about this topic to Microsoft.

Copyright © 2007 by Microsoft Corporation. All rights reserved.