다음을 통해 공유


WMI Provider

Download sample

This sample demonstrates how to gather data from Windows Communication Foundation (WCF) services at runtime by using the Windows Management Instrumentation (WMI) provider that is built into WCF. Also, this sample demonstrates how to add a user-defined WMI object to a service. The sample activates the WMI provider for the Getting Started Sample and demonstrates how to gather data from the ICalculator service at runtime.

WMI is Microsoft's implementation of the Web-Based Enterprise Management (WBEM) standard. For more information about the WMI SDK, see the MSDN Library. (https://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/wmi\_start\_page.asp). WBEM is an industry standard for how applications expose management instrumentation to external management tools.

WCF implements a WMI provider, a component that exposes instrumentation at runtime through a WBEM-compatible interface. Management tools can connect to the services through the interface at runtime. WCF exposes attributes of services such as addresses, bindings, behaviors, and listeners.

The built-in WMI provider is activated in the configuration file of the application. This is done through the wmiProviderEnabled attribute of the <diagnostics> Element in the system.ServiceModel element section, as shown in the following sample configuration:

<system.serviceModel>
    ...
    <diagnostics wmiProviderEnabled="true" />
    ...
</system.serviceModel>

This configuration entry exposes a WMI interface. Management applications can now connect through this interface and access the management instrumentation of the application.

Custom WMI Object

Adding WMI objects to a service makes it possible to reveal user-defined information along with the built-in WMI provider information. This is accomplished by publishing the schema of the service to WMI by using the Installutil.exe application. Instructions to accomplish this, along with more details can be found in the setup instructions at the end of the topic.

Accessing WMI Information

WMI data can be accessed many different ways. Microsoft provides WMI APIs for scripts, Visual Basic applications, C++ applications, and the .NET Framework (https://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/using\_wmi.asp).

This sample uses two Java scripts: one to enumerate services running on the machine along with some of their properties and the second to view user-defined WMI data. The script opens a connection to the WMI provider, parses data, and displays the data gathered.

Start the sample to create a running instance of a WCF service. While the service is running, run each Java script by using the following command at the command prompt:

cscript EnumerateServices.js

The script accesses the instrumentation contained in the service and produces the following output:

Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

1 service(s) found.
|-PID:           1444
|-DistinguishedName:  CalculatorService@https://localhost/servicemodelsamples/service.svc
|-Endpoints:     2 endpoints
  |-CalculatorService.ICalculator@https://localhost/servicemodelsamples/service.svc
    |-Address:                        https://localhost/servicemodelsamples/service.svc
    |-CounterInstanceName:
    |-AddressHeaders:                 0
    |-ContractType:                   Contract.Name='ICalculator'
    |-BindingElements:                4 bindings
      |-BindingElements[0]
        |-Type:                       TransactionFlowBindingElement
      |-BindingElements[1]
        |-Type:                       SymmetricSecurityBindingElement
      |-BindingElements[2]
        |-Type:                       TextMessageEncodingBindingElement
        |-MaxReadPoolSize:            64
        |-MaxWritePoolSize:           16
      |-BindingElements[3]
        |-Type:                       HttpTransportBindingElement
        |-ManualAddressing:           false
        |-MaxBufferSize:              65536
        |-AllowCookies:               false
        |-AuthenticationScheme:       Anonymous
        |-BypassProxyOnLocal:         false
        |-HostNameComparisonMode:     StrongWildcard
        |-ProxyAddress:               null
        |-ProxyAuthenticationScheme:  Anonymous
        |-Realm:
        |-TransferMode:               Buffered
        |-UseDefaultWebProxy:         true
  |-CalculatorService.IMetadataExchange@https://localhost/servicemodelsamples/service.svc/mex
    |-Address:                        https://localhost/servicemodelsamples/service.svc/mex
    |-CounterInstanceName:
    |-AddressHeaders:                 0
    |-ContractType:                   Contract.Name='IMetadataExchange'
    |-BindingElements:                3 bindings
      |-BindingElements[0]
        |-Type:                       TransactionFlowBindingElement
      |-BindingElements[1]
        |-Type:                       TextMessageEncodingBindingElement
        |-MaxReadPoolSize:            64
        |-MaxWritePoolSize:           16
      |-BindingElements[2]
        |-Type:                       HttpTransportBindingElement
        |-ManualAddressing:           false
        |-MaxBufferSize:              65536
        |-AllowCookies:               false
        |-AuthenticationScheme:       Anonymous
        |-BypassProxyOnLocal:         false
        |-HostNameComparisonMode:     StrongWildcard
        |-ProxyAddress:               null
        |-ProxyAuthenticationScheme:  Anonymous
        |-Realm:
        |-TransferMode:               Buffered
        |-UseDefaultWebProxy:         true
|-Behaviors:     6 behaviors
      |-Behavior[0]
      |-Type:                       ServiceBehaviorAttribute
        |-AddressFilterMode:               Exact
        |-AutomaticSessionShutdown:        true
        |-ConcurrencyMode:                 Single
        |-IncludeExceptionDetailInFaults:  false
        |-InstanceContextMode:             PerSession
        |-TransactionIsolationLevel:       Unspecified
        |-TransactionTimeout:              null
        |-ValidateMustUnderstand:          true
      |-Behavior[1]
      |-Type:                       AspNetCompatibilityRequirementsAttribute
      |-Behavior[2]
      |-Type:                       ServiceMetadataBehavior
        |-ExternalMetadataLocation:       null
        |-EnableGetEnabled:               undefined
        |-HttpGetUrl:                     null
      |-Behavior[3]
      |-Type:                       ServiceDebugBehavior
      |-Behavior[4]
      |-Type:                       ServiceAuthorizationBehavior
      |-Behavior[5]
      |-Type:                       Behavior

Next, run the second Java Script to display the user-defined WMI data:

cscript EnumerateCustomObjects.js

The script accesses the user-defined instrumentation contained in the services and produces the following output:

1 WMIObject(s) found.
|-PID:           30285bfd-9d66-4c4e-9be2-310499c5cef5
|-InstanceId:    3839
|-WMIInfo:       User Defined WMI Information.

The output shows that there is a single service running on the machine. The service exposes one endpoint that implements the ICalculator contract. The settings of the behavior and binding that are implemented by the endpoint are listed as the sum of individual elements of the messaging stack.

WMI is not limited to exposing the management instrumentation of the WCF infrastructure. The application can expose its own domain-specific data items through the same mechanism. WMI is a unified mechanism for inspection and control of a Web service.

To set up, build, and run the sample

  1. Ensure you have performed the One-Time Setup Procedure for the Windows Communication Foundation Samples.

  2. To build the C# or Visual Basic .NET edition of the solution, follow the instructions in Building the Windows Communication Foundation Samples.

  3. Publish the services schema to WMI by running the InstallUtil.exe (the default locations for InstallUtil.exe is "%WINDIR%\Microsoft.NET\Framework\v2.0.50727") on the service.dll file in the hosting directory. This step only needs to be executed when changes have been made to the service.dll file. For more information, see Providing Management Information by Instrumenting Applications at: https://msdn2.microsoft.com/en-us/library/ms186147.aspx in the "How To: Publish the Scheme to WMI for an Instrumented Application" section.

  4. To run the sample in a single- or cross-machine configuration, follow the instructions in Running the Windows Communication Foundation Samples.

    NoteNote:

    If you installed WCF after installing ASP.NET, you may need to run "%WINDIR%\ Microsoft.Net\Framework\v3.0\Windows Communication Foundation\servicemodelreg.exe " -r -x to give the ASPNET account permission to publish WMI objects.

  5. View data from the sample surfaced through WMI by using the commands: cscript EnumerateServices.js or cscript EnumerateCustomObjects.js.

Footer image

Send comments about this topic to Microsoft.
© Microsoft Corporation. All rights reserved.