ListenerServiceInstallComponent.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 / Tools / xws_reg / System / ServiceModel / Install / ListenerServiceInstallComponent.cs / 1 / ListenerServiceInstallComponent.cs

                            //------------------------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------
namespace System.ServiceModel.Install
{ 
    using System.ComponentModel;
    using System.Diagnostics; 
    using System.IO; 
    using System.Security.AccessControl;
    using System.Security.Principal; 
    using System.ServiceProcess;

    class ListenerServiceInstallComponent : ServiceInstallComponent
    { 
        static SecurityIdentifier adminSid;
 
        ListenerServiceInstallComponent(string serviceName, string legacyServiceName, string displayName, 
            string serviceArguments, string[] dependsOn, string accountName, string description)
            : base(serviceName, legacyServiceName, displayName, ServiceStartMode.Disabled, 
                   ServiceModelInstallStrings.ListenerServiceExeName, serviceArguments, dependsOn, accountName,
                   description, ServiceModelInstallStrings.ListenerServiceSecurityDescriptor)
        {
            // empty 
        }
 
        static SecurityIdentifier AdminSid 
        {
            get 
            {
                if (adminSid == null)
                {
                    adminSid = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null); 
                }
                return adminSid; 
            } 
        }
 
        public static ServiceModelInstallComponent CreateTcpPortSharingServiceInstallComponent()
        {
            return new ListenerServiceInstallComponent(
                ServiceModelInstallStrings.TcpPortSharingServiceName, 
                ServiceModelInstallStrings.LegacyTcpPortSharingServiceName,
                SR.GetString(SR.TcpPortSharingServiceDisplayName), 
                null, 
                ServiceModelInstallStrings.TcpPortSharingServiceDependencies,
                ServiceModelInstallStrings.LocalServiceAccountName, 
                SR.GetString(SR.TcpPortSharingServiceDescription));
        }

        public static ServiceModelInstallComponent CreateTcpActivationServiceInstallComponent() 
        {
            return new ListenerServiceInstallComponent( 
                ServiceModelInstallStrings.TcpActivationServiceName, 
                ServiceModelInstallStrings.LegacyTcpActivationServiceName,
                SR.GetString(SR.TcpActivationServiceDisplayName), 
                null,
                ServiceModelInstallStrings.TcpActivationServiceDependencies,
                ServiceModelInstallStrings.LocalServiceAccountName,
                SR.GetString(SR.TcpActivationServiceDescription)); 
        }
 
        public static ServiceModelInstallComponent CreateNamedPipeActivationServiceInstallComponent() 
        {
            return new ListenerServiceInstallComponent( 
                ServiceModelInstallStrings.NamedPipeActivationServiceName,
                ServiceModelInstallStrings.LegacyNamedPipeActivationServiceName,
                SR.GetString(SR.NamedPipeActivationServiceDisplayName),
                null, 
                ServiceModelInstallStrings.NamedPipeActivationServiceDependencies,
                ServiceModelInstallStrings.LocalServiceAccountName, 
                SR.GetString(SR.NamedPipeActivationServiceDescription)); 
        }
 
        public static ServiceModelInstallComponent CreateMsmqActivationServiceInstallComponent()
        {
            return new ListenerServiceInstallComponent(
                ServiceModelInstallStrings.MsmqActivationServiceName, 
                ServiceModelInstallStrings.LegacyMsmqActivationServiceName,
                SR.GetString(SR.MsmqActivationServiceDisplayName), 
                "-" + ServiceModelInstallStrings.MsmqActivationServiceName, 
                ServiceModelInstallStrings.MsmqActivationServiceDependencies,
                ServiceModelInstallStrings.NetworkServiceAccountName, 
                SR.GetString(SR.MsmqActivationServiceDescription));
        }

        protected override void OnInstall(OutputLevel outputLevel) 
        {
            base.OnInstall(outputLevel); 
            this.SetConfigSecurity(outputLevel); 
            this.SetExtendedProperties();
        } 

        protected override void OnReinstall(OutputLevel outputLevel)
        {
            base.OnReinstall(outputLevel); 
            this.SetConfigSecurity(outputLevel);
            this.SetExtendedProperties(); 
        } 

        void SetConfigSecurity(OutputLevel outputLevel) 
        {
            string listenerConfigFile =
                Path.Combine(InstallHelper.GetWcfRuntimeInstallPath(), ServiceModelInstallStrings.ListenerServiceExeName + ".config");
 
            FileSecurity fileSecurity = null;
            try 
            { 
                fileSecurity = File.GetAccessControl(listenerConfigFile);
            } 
            catch (FileNotFoundException)
            {
                // acceptable
            } 

            if (fileSecurity != null) 
            { 
                fileSecurity.AddAccessRule(new FileSystemAccessRule(AdminSid, FileSystemRights.FullControl,
                    AccessControlType.Allow)); 
                fileSecurity.AddAccessRule(new FileSystemAccessRule(ServiceModelInstallStrings.LocalServiceAccountName, FileSystemRights.Read,
                    AccessControlType.Allow));
                fileSecurity.AddAccessRule(new FileSystemAccessRule(ServiceModelInstallStrings.NetworkServiceAccountName, FileSystemRights.Read,
                    AccessControlType.Allow)); 

                try 
                { 
                    File.SetAccessControl(listenerConfigFile, fileSecurity);
                } 
                catch (FileNotFoundException)
                {
                    // acceptable
                } 
            }
        } 
 
        void SetExtendedProperties()
        { 
            if (OSEnvironmentHelper.IsVistaOrGreater)
            {
                ExecuteSC("sidtype " + this.ServiceName + " restricted");
                ExecuteSC("privs " + this.ServiceName + " SeCreateGlobalPrivilege"); 
            }
        } 
    } 
}

// 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