ManagementExtension.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / Administration / ManagementExtension.cs / 1 / ManagementExtension.cs

                            //------------------------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------

namespace System.ServiceModel.Administration 
{
    using System; 
    using System.ServiceModel; 
    using System.ServiceModel.Description;
    using System.Collections.Generic; 
    using System.IO;
    using System.Text;
    using System.Threading;
    using System.Runtime.Serialization; 
    using System.ServiceModel.Configuration;
    using System.Xml; 
    using System.Security; 

    internal sealed class ManagementExtension 
    {
        static Dictionary services;
        static bool activated = false;
        static object syncRoot = new object(); 
        static bool isEnabled = GetIsWmiProviderEnabled();
 
        internal static bool IsActivated 
        {
            get { return ManagementExtension.activated; } 
        }

        internal static bool IsEnabled
        { 
            get { return ManagementExtension.isEnabled; }
        } 
 
        /// 
        /// Critical - calls critical method DiagnosticSection.UnsafeGetSection 
        /// Safe - doesn't leak config section instance, just returns a bool
        /// 
        [SecurityCritical, SecurityTreatAsSafe]
        static bool GetIsWmiProviderEnabled() 
        {
            return DiagnosticSection.UnsafeGetSection().WmiProviderEnabled; 
        } 

        static Dictionary GetServices() 
        {
            if (ManagementExtension.services == null)
            {
                lock (ManagementExtension.syncRoot) 
                {
                    if (ManagementExtension.services == null) 
                    { 
                        ManagementExtension.services = new Dictionary();
                    } 
                }
            }
            return ManagementExtension.services;
        } 

        internal static ICollection Services 
        { 
            get
            { 
                return GetServices().Keys;
            }
        }
 
        internal static DateTime GetTimeOpened(ServiceHostBase service)
        { 
            return GetServices()[service]; 
        }
 
        public static void OnServiceOpened(ServiceHostBase serviceHostBase)
        {
            EnsureManagementProvider();
            Add(serviceHostBase); 
        }
 
        public static void OnServiceClosing(ServiceHostBase serviceHostBase) 
        {
            Remove(serviceHostBase); 
        }

        static void Add(ServiceHostBase service)
        { 
            Dictionary services = GetServices();
            lock (services) 
            { 
                if (!services.ContainsKey(service))
                { 
                    services.Add(service, DateTime.Now);
                }
            }
        } 

        static void Remove(ServiceHostBase service) 
        { 
            Dictionary services = GetServices();
            lock (services) 
            {
                if (services.ContainsKey(service))
                {
                    services.Remove(service); 
                }
            } 
        } 

        static void EnsureManagementProvider() 
        {
            if (!ManagementExtension.activated)
            {
                lock (ManagementExtension.syncRoot) 
                {
                    if (!ManagementExtension.activated) 
                    { 
                        Activate();
                        ManagementExtension.activated = true; 
                    }
                }
            }
        } 

        static void Activate() 
        { 
            WbemProvider wmi = new WbemProvider(AdministrationStrings.IndigoNamespace, AdministrationStrings.IndigoAppName);
            wmi.Register(AdministrationStrings.AppDomainInfo, new AppDomainInstanceProvider()); 
            wmi.Register(AdministrationStrings.Service, new ServiceInstanceProvider());
            wmi.Register(AdministrationStrings.Contract, new ContractInstanceProvider());
            wmi.Register(AdministrationStrings.Endpoint, new EndpointInstanceProvider());
            wmi.Register(AdministrationStrings.ServiceAppDomain, new ServiceAppDomainAssociationProvider()); 
            wmi.Register(AdministrationStrings.ServiceToEndpointAssociation, new ServiceEndpointAssociationProvider());
        } 
    } 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK