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. (http://msdn.microsoft.com/library/default.asp?url=/library/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> in the <system.serviceModel> 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.
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.
WMI data can be accessed many different ways. Microsoft provides WMI APIs for scripts, Visual Basic applications, C++ applications, and the .NET Framework (http://msdn.microsoft.com/library/default.asp?url=/library/wmisdk/wmi/using_wmi.asp).
This sample uses two Java scripts: one to enumerate services running on the computer 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:
The script accesses the instrumentation contained in the service and produces the following output:
Microsoft (R) Windows Script Host Version 5.6 Copyright © Microsoft Corporation 1996-2001. All rights reserved. 1 service(s) found. |-PID: 5776 |-DistinguishedName: CalculatorService@http://localhost/ServiceModelSamples/service.svc |-Endpoints: 1 endpoints |-CalculatorService.ICalculator@http://localhost/ServiceModelSamples/service.svc |-Address: http://localhost/ServiceModelSamples/service.svc |-CounterInstanceName: |-AddressHeaders: 0 |-ContractType: Contract.Name='ICalculator' |-BindingElements: 4 bindings |-BindingElements |-Type: TransactionFlowBindingElement |-BindingElements |-Type: SymmetricSecurityBindingElement |-BindingElements |-Type: TextMessageEncodingBindingElement |-MaxReadPoolSize: 64 |-MaxWritePoolSize: 16 |-BindingElements |-Type: HttpTransportBindingElement |-ManualAddressing: false |-MaxBufferSize: 65536 |-AllowCookies: false |-AuthenticationScheme: Anonymous |-BypassProxyOnLocal: false |-HostNameComparisonMode: StrongWildcard |-ProxyAddress: null |-ProxyAuthenticationScheme: Anonymous |-Realm: |-TransferMode: Buffered |-UseDefaultWebProxy: true |-Behaviors: 5 behaviors |-Behavior |-Type: ServiceBehaviorAttribute |-AddressFilterMode: Exact |-AutomaticSessionShutdown: true |-ConcurrencyMode: Single |-IncludeExceptionDetailInFaults: false |-InstanceContextMode: PerSession |-TransactionIsolationLevel: Unspecified |-TransactionTimeout: null |-ValidateMustUnderstand: true |-Behavior |-Type: AspNetCompatibilityRequirementsAttribute |-Behavior |-Type: ServiceDebugBehavior |-Behavior |-Type: ServiceAuthorizationBehavior |-Behavior |-Type: Behavior
Next, run the second Java Script to display the user-defined WMI data:
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 computer. 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
Ensure you have performed the One-Time Setup Procedure for the Windows Communication Foundation Samples.
To build the C# or Visual Basic .NET edition of the solution, follow the instructions in Building the Windows Communication Foundation Samples.
Publish the services schema to WMI by running the InstallUtil.exe (the default locations for InstallUtil.exe is "%WINDIR%\Microsoft.NET\Framework\v4.0.30319") 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: http://msdn2.microsoft.com/library/ms186147.aspx in the "How To: Publish the Scheme to WMI for an Instrumented Application" section.
To run the sample in a single- or cross-computer configuration, follow the instructions in Running the Windows Communication Foundation Samples.
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.
View data from the sample surfaced through WMI by using the commands: cscript EnumerateServices.js or cscript EnumerateCustomObjects.js.
The samples may already be installed on your computer. Check for the following (default) directory before continuing.
If this directory does not exist, go to Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4 to download all Windows Communication Foundation (WCF) and WF samples. This sample is located in the following directory.