ServiceModelReg.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 / ServiceModelReg.cs / 4 / ServiceModelReg.cs

                            //------------------------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------
namespace Microsoft.Tools.ServiceModel
{ 
    using System;
    using System.Collections; 
    using System.Collections.Generic; 
    using System.Globalization;
    using System.IO; 
    using System.Reflection;
    using System.Runtime.InteropServices;
    using System.Security;
    using System.Security.Permissions; 
    using System.Security.Principal;
    using System.ServiceModel.Install; 
    using System.ServiceModel.Install.Configuration; 
    using System.Text;
    using System.Threading; 

    class ServiceModelReg
    {
        const string MutexName = "ServiceModelReg_f685339e-b522-4d07-8921-1d75962f1905"; 
        const string webEngineDllName = @"webengine.dll";
        static Mutex SingleInstance; 
        static bool passedLocalSecurityChecks; 
        string[] protocols = new string[] { "net.tcp", "net.pipe", "net.msmq", "msmq.formatname" };
        CommandArguments commandArguments; 

        //
        internal static bool ShouldInstallToWebConfig
        { 
            get
            { 
                return File.Exists(Path.Combine(InstallHelper.GetFrameworkInstallPath(), webEngineDllName)); 
            }
        } 

        static ServiceModelReg()
        {
            ServiceModelReg.passedLocalSecurityChecks = ServiceModelReg.InitializeSecurity(); 
        }
 
        static bool InitializeSecurity() 
        {
            bool initializedSecurity = false; 

            try
            {
                AppDomain myDomain = Thread.GetDomain(); 
                myDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
 
                // Demand FullTrust permissionset 
                new NamedPermissionSet("FullTrust").Demand();
 
                // Demand Administrator role
                SecurityIdentifier builtInAdministrators = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null);

                foreach (IdentityReference id in WindowsIdentity.GetCurrent().Groups) 
                {
                    if (builtInAdministrators.Value.Equals(id.Value, StringComparison.Ordinal)) 
                    { 
                        initializedSecurity = true;
                        break; 
                    }
                }

                if (!initializedSecurity) 
                {
                    ServiceModelReg.LogError(SR2.GetString(SR2.AccessDeniedLocalAdmin)); 
                } 
            }
            catch (SecurityException) 
            {
                ServiceModelReg.LogError(SR2.GetString(SR2.AccessDeniedFullTrust));
            }
 
            return initializedSecurity;
        } 
 
        Queue BuildActionQueue()
        { 
            Queue actionQueue = new Queue();
            if (this.commandArguments.ApplyToConfig)
            {
                if (InstallHelper.Is64BitMachine() && !String.IsNullOrEmpty(InstallHelper.Wow64MachineConfigFileName)) 
                {
                    if (File.Exists(InstallHelper.Wow64MachineConfigFileName)) 
                    { 
                        actionQueue.Enqueue(new ActionItem(ConfigurationHandlersInstallComponent.CreateWow64ConfigurationHandlersInstallComponent(),
                            this.commandArguments.InstallAction, 
                            this.commandArguments.OutputLevel));
                    }
                    else
                    { 
                        EventLogger.LogWarning(SR2.GetString(SR2.Wow64NotInstalled,
                            System.ServiceModel.Install.SR.GetString(System.ServiceModel.Install.SR.CommunicationSectionNameWow64)), 
                            (OutputLevel.Verbose == this.commandArguments.OutputLevel)); 
                    }
                } 

                actionQueue.Enqueue(new ActionItem(ConfigurationHandlersInstallComponent.CreateNativeConfigurationHandlersInstallComponent(),
                    this.commandArguments.InstallAction,
                    this.commandArguments.OutputLevel)); 

                if (ShouldInstallToWebConfig) 
                { 
                    if (InstallHelper.Is64BitMachine() && !String.IsNullOrEmpty(InstallHelper.Wow64WebConfigFileName))
                    { 
                        if (File.Exists(InstallHelper.Wow64WebConfigFileName))
                        {
                            actionQueue.Enqueue(new ActionItem(BuildProviderInstallComponent.CreateWow64BuildProviderInstallComponent(),
                                this.commandArguments.InstallAction, 
                                this.commandArguments.OutputLevel));
 
                            actionQueue.Enqueue(new ActionItem(CompilationAssemblyInstallComponent.CreateWow64CompilationAssemblyInstallComponent(), 
                                this.commandArguments.InstallAction,
                                this.commandArguments.OutputLevel)); 

                            actionQueue.Enqueue(new ActionItem(HttpHandlersInstallComponent.CreateWow64HttpHandlersInstallComponent(),
                                this.commandArguments.InstallAction,
                                this.commandArguments.OutputLevel)); 

                            actionQueue.Enqueue(new ActionItem(HttpModulesInstallComponent.CreateWow64HttpModulesInstallComponent(), 
                                this.commandArguments.InstallAction, 
                                this.commandArguments.OutputLevel));
                        } 
                        else
                        {
                            EventLogger.LogWarning(SR2.GetString(SR2.Wow64NotInstalled,
                                System.ServiceModel.Install.SR.GetString(System.ServiceModel.Install.SR.BuildProviderNameWow64)), 
                                (OutputLevel.Verbose == this.commandArguments.OutputLevel));
 
                            EventLogger.LogWarning(SR2.GetString(SR2.Wow64NotInstalled, 
                                System.ServiceModel.Install.SR.GetString(System.ServiceModel.Install.SR.CompilationAssemblyNameWow64)),
                                (OutputLevel.Verbose == this.commandArguments.OutputLevel)); 

                            EventLogger.LogWarning(SR2.GetString(SR2.Wow64NotInstalled,
                                System.ServiceModel.Install.SR.GetString(System.ServiceModel.Install.SR.HttpHandlersComponentNameWow64)),
                                (OutputLevel.Verbose == this.commandArguments.OutputLevel)); 

                            EventLogger.LogWarning(SR2.GetString(SR2.Wow64NotInstalled, 
                                System.ServiceModel.Install.SR.GetString(System.ServiceModel.Install.SR.HttpModulesComponentNameWow64)), 
                                (OutputLevel.Verbose == this.commandArguments.OutputLevel));
                        } 
                    }

                    actionQueue.Enqueue(new ActionItem(BuildProviderInstallComponent.CreateNativeBuildProviderInstallComponent(),
                        this.commandArguments.InstallAction, 
                        this.commandArguments.OutputLevel));
 
                    actionQueue.Enqueue(new ActionItem(CompilationAssemblyInstallComponent.CreateNativeCompilationAssemblyInstallComponent(), 
                        this.commandArguments.InstallAction,
                        this.commandArguments.OutputLevel)); 

                    actionQueue.Enqueue(new ActionItem(HttpHandlersInstallComponent.CreateNativeHttpHandlersInstallComponent(),
                        this.commandArguments.InstallAction,
                        this.commandArguments.OutputLevel)); 

                    actionQueue.Enqueue(new ActionItem(HttpModulesInstallComponent.CreateNativeHttpModulesInstallComponent(), 
                        this.commandArguments.InstallAction, 
                        this.commandArguments.OutputLevel));
 
                    if (IisHelper.ShouldInstallWas)
                    {
                        BuildActionQueueIis7(actionQueue);
                    } 
                    else if (this.commandArguments.ApplyToConfig &&
                        InstallAction.Install != this.commandArguments.InstallAction) 
                    { 
                        if (OSEnvironmentHelper.IsVistaOrGreater)
                        { 
                            foreach (string protocol in this.protocols)
                            {
                                if (InstallHelper.Is64BitMachine() && !String.IsNullOrEmpty(InstallHelper.Wow64WebConfigFileName))
                                { 
                                    if (File.Exists(InstallHelper.Wow64WebConfigFileName))
                                    { 
                                        actionQueue.Enqueue(new ActionItem(ProtocolsInstallComponent.CreateWow64ProtocolsInstallComponent(protocol), 
                                            InstallAction.Uninstall,
                                            this.commandArguments.OutputLevel)); 

                                        actionQueue.Enqueue(new ActionItem(TransportConfigurationTypeInstallComponent.CreateWow64TransportConfigurationTypeInstallComponent(protocol),
                                            InstallAction.Uninstall,
                                            this.commandArguments.OutputLevel)); 
                                    }
                                } 
 
                                actionQueue.Enqueue(new ActionItem(ProtocolsInstallComponent.CreateNativeProtocolsInstallComponent(protocol),
                                    InstallAction.Uninstall, 
                                    this.commandArguments.OutputLevel));

                                actionQueue.Enqueue(new ActionItem(TransportConfigurationTypeInstallComponent.CreateNativeTransportConfigurationTypeInstallComponent(protocol),
                                    InstallAction.Uninstall, 
                                    this.commandArguments.OutputLevel));
                            } 
                        } 
                    }
                } 
            }

            if (this.commandArguments.ApplyToScriptMaps && ShouldInstallToWebConfig)
            { 
                // We do not need to install ScriptMaps for IIS7.
                if (!IisHelper.ShouldInstallWas) 
                { 
                    if (IisHelper.ShouldInstallIis && IisHelper.ShouldEnableIISAdmin)
                    { 
                        try
                        {
                            actionQueue.Enqueue(new ActionItem(new WebHostScriptMappingsInstallComponent(this.commandArguments.ScriptMapPath, this.commandArguments.Recursive, this.commandArguments.UpdateScriptMaps),
                                this.commandArguments.InstallAction, 
                                this.commandArguments.OutputLevel));
                        } 
                        catch (Exception exception) 
                        {
                            // If we are running as part of framework install, we should tolerate failures in script map installation 
                            if (IsFatal(exception) || !this.commandArguments.RunInstallScript)
                            {
                                throw;
                            } 
                            ServiceModelReg.LogException(exception);
                        } 
                    } 
                    else if (!IisHelper.ShouldInstallIis)
                    { 
                        EventLogger.LogWarning(SR2.GetString(SR2.IisNotInstalled, System.ServiceModel.Install.SR.GetString(System.ServiceModel.Install.SR.WebHostScriptMappingsName)), (OutputLevel.Verbose == this.commandArguments.OutputLevel));
                    }
                    else
                    { 
                        EventLogger.LogWarning(SR2.GetString(SR2.IISAdminNotEnabled, System.ServiceModel.Install.SR.GetString(System.ServiceModel.Install.SR.WebHostScriptMappingsName)), (OutputLevel.Verbose == this.commandArguments.OutputLevel));
                    } 
                } 
                else
                { 
                    EventLogger.LogWarning(SR2.GetString(SR2.ServiceModelRegScriptMapsNotSupported), (OutputLevel.Verbose == this.commandArguments.OutputLevel));
                }
            }
 
            if (this.commandArguments.RunInstallScript)
            { 
                actionQueue.Enqueue(new ActionItem(new WmiInstallComponent(), 
                    this.commandArguments.InstallAction,
                    this.commandArguments.OutputLevel)); 

                if (this.commandArguments.InstallServices)
                {
                    actionQueue.Enqueue(new ActionItem(new InfoCardServiceInstallComponent(), 
                        this.commandArguments.InstallAction,
                        this.commandArguments.OutputLevel)); 
 
                    actionQueue.Enqueue(new ActionItem(ListenerServiceInstallComponent.CreateTcpPortSharingServiceInstallComponent(),
                        this.commandArguments.InstallAction, 
                        this.commandArguments.OutputLevel));

                    if (OSEnvironmentHelper.IsVistaOrGreater)
                    { 
                        Nullable activationServiceInstallAction = null;
                        if (IisHelper.ShouldInstallWas) 
                        { 
                            activationServiceInstallAction = this.commandArguments.InstallAction;
                        } 
                        else if (InstallAction.Install != this.commandArguments.InstallAction)
                        {
                            activationServiceInstallAction = InstallAction.Uninstall;
                        } 

                        if (activationServiceInstallAction.HasValue) 
                        { 
                            actionQueue.Enqueue(new ActionItem(ListenerServiceInstallComponent.CreateTcpActivationServiceInstallComponent(),
                                activationServiceInstallAction.Value, 
                                this.commandArguments.OutputLevel));

                            actionQueue.Enqueue(new ActionItem(ListenerServiceInstallComponent.CreateNamedPipeActivationServiceInstallComponent(),
                                activationServiceInstallAction.Value, 
                                this.commandArguments.OutputLevel));
 
                            actionQueue.Enqueue(new ActionItem(ListenerServiceInstallComponent.CreateMsmqActivationServiceInstallComponent(), 
                                activationServiceInstallAction.Value,
                                this.commandArguments.OutputLevel)); 
                        }
                    }
                }
 
                actionQueue.Enqueue(new ActionItem(new HttpNamespaceReservationInstallComponent(),
                    this.commandArguments.InstallAction, 
                    this.commandArguments.OutputLevel)); 
            }
 
            return actionQueue;
        }

        void BuildActionQueueIis7(Queue actionQueue) 
        {
            foreach (string protocol in this.protocols) 
            { 
                if (InstallHelper.Is64BitMachine() && !String.IsNullOrEmpty(InstallHelper.Wow64WebConfigFileName))
                { 
                    if (File.Exists(InstallHelper.Wow64WebConfigFileName))
                    {
                        actionQueue.Enqueue(new ActionItem(ProtocolsInstallComponent.CreateWow64ProtocolsInstallComponent(protocol),
                            this.commandArguments.InstallAction, 
                            this.commandArguments.OutputLevel));
 
                        actionQueue.Enqueue(new ActionItem(TransportConfigurationTypeInstallComponent.CreateWow64TransportConfigurationTypeInstallComponent(protocol), 
                            this.commandArguments.InstallAction,
                            this.commandArguments.OutputLevel)); 
                    }
                    else
                    {
                        EventLogger.LogWarning(SR2.GetString(SR2.Wow64NotInstalled, 
                            System.ServiceModel.Install.SR.GetString(System.ServiceModel.Install.SR.ProtocolsNameWow64, protocol)),
                            (OutputLevel.Verbose == this.commandArguments.OutputLevel)); 
 
                        EventLogger.LogWarning(SR2.GetString(SR2.Wow64NotInstalled,
                            System.ServiceModel.Install.SR.GetString(System.ServiceModel.Install.SR.TransportConfigurationNameWow64, protocol)), 
                            (OutputLevel.Verbose == this.commandArguments.OutputLevel));
                    }
                }
 
                if (IisHelper.ShouldInstallApplicationHost)
                { 
                    actionQueue.Enqueue(new ActionItem(ListenerAdaptersInstallComponent.CreateNativeListenerAdaptersInstallComponent(protocol), 
                        this.commandArguments.InstallAction,
                        this.commandArguments.OutputLevel)); 
                }
                else
                {
                    EventLogger.LogWarning(SR2.GetString(SR2.ApplicationHostConfigNotInstalled, 
                        System.ServiceModel.Install.SR.GetString(System.ServiceModel.Install.SR.ListenerAdaptersName, protocol)),
                        (OutputLevel.Verbose == this.commandArguments.OutputLevel)); 
                } 

                actionQueue.Enqueue(new ActionItem(ProtocolsInstallComponent.CreateNativeProtocolsInstallComponent(protocol), 
                    this.commandArguments.InstallAction,
                    this.commandArguments.OutputLevel));

                actionQueue.Enqueue(new ActionItem(TransportConfigurationTypeInstallComponent.CreateNativeTransportConfigurationTypeInstallComponent(protocol), 
                    this.commandArguments.InstallAction,
                    this.commandArguments.OutputLevel)); 
            } 

            if (IisHelper.ShouldInstallIis) 
            {
                if (IisHelper.ShouldInstallApplicationHost)
                {
                    actionQueue.Enqueue(new ActionItem(WasHttpModulesInstallComponent.CreateNativeWasHttpModulesInstallComponent(), 
                        this.commandArguments.InstallAction,
                        this.commandArguments.OutputLevel)); 
 
                    actionQueue.Enqueue(new ActionItem(WasHttpHandlersInstallComponent.CreateNativeWasHttpHandlersInstallComponent(),
                        this.commandArguments.InstallAction, 
                        this.commandArguments.OutputLevel));
                }
                else
                { 
                    EventLogger.LogWarning(SR2.GetString(SR2.ApplicationHostConfigNotInstalled,
                        System.ServiceModel.Install.SR.GetString(System.ServiceModel.Install.SR.HttpModulesComponentNameWAS)), 
                        (OutputLevel.Verbose == this.commandArguments.OutputLevel)); 

                    EventLogger.LogWarning(SR2.GetString(SR2.ApplicationHostConfigNotInstalled, 
                        System.ServiceModel.Install.SR.GetString(System.ServiceModel.Install.SR.HttpHandlersComponentNameWAS)),
                        (OutputLevel.Verbose == this.commandArguments.OutputLevel));
                }
            } 
        }
 
        void DisplayLogo() 
        {
            // ThisAssembly conflicts with one imported from System.ServiceModel.Install.dll (friend assembly) 
            // build uses the correct one (local)...\
            // Using CommonResStrings.WcfTrademarkForCmdLine for the trademark: the proper resource for command line tools.
#pragma warning disable 436
            Console.WriteLine(SR2.GetString(SR2.HelpLogo, CommonResStrings.WcfTrademarkForCmdLine, ThisAssembly.InformationalVersion, CommonResStrings.CopyrightForCmdLine)); 
#pragma warning restore 436
        } 
 
        static void LogException(Exception e)
        { 
            //  If passed security checks, System.ServiceModel.Install.dll
            //  log error should be available (which also uses NT Event Log).
            //  Otherwise, event log may not be available due to security
            //  restrictions, so only log to console. 
            if (ServiceModelReg.passedLocalSecurityChecks)
            { 
                // Wrap method to prevent LinkDemand in this method 
                ServiceModelReg.InstallHelperLogException(e);
            } 
            else
            {
                Console.WriteLine();
#if DEBUG 
                Console.WriteLine(String.Format(CultureInfo.CurrentUICulture,SR2.GetString(SR2.Error, e.ToString())));
#else 
                Console.WriteLine(String.Format(CultureInfo.CurrentUICulture,SR2.GetString(SR2.Error, e.Message))); 
#endif
            } 
        }

        static void LogError(string message)
        { 
            //  If passed security checks, System.ServiceModel.Install.dll
            //  log error should be available (which also uses NT Event Log). 
            //  Otherwise, event log may not be available due to security 
            //  restrictions, so only log to console.
            if (ServiceModelReg.passedLocalSecurityChecks) 
            {
                // Wrap method to prevent LinkDemand in this method
                ServiceModelReg.InstallHelperLogError(message);
            } 
            else
            { 
                Console.WriteLine(); 
                Console.WriteLine(String.Format(CultureInfo.CurrentUICulture, message));
            } 
        }

        static void InstallHelperLogException(Exception e)
        { 
            EventLogger.LogException(e);
        } 
 
        static void InstallHelperLogError(string message)
        { 
            EventLogger.LogError(message);
        }

        static int Main(string[] args) 
        {
            Environment.ExitCode = NativeMethods.ERROR_APP_INIT_FAILURE; 
            ServiceModelReg.ValidateUICulture(); 

            if (!ServiceModelReg.passedLocalSecurityChecks) 
            {
                Environment.ExitCode = NativeMethods.ERROR_ACCESS_DENIED;
            }
            else 
            {
                // Wrap method to prevent LinkDemand in this method 
                Environment.ExitCode = ServiceModelReg.TryRun(args); 
            }
 
            return Environment.ExitCode;
        }

        static int TryRun(string[] args) 
        {
            try 
            { 
                using (ServiceModelReg.SingleInstance = new Mutex(false, ServiceModelReg.MutexName))
                { 
                    if (ServiceModelReg.SingleInstance.WaitOne(1, true))
                    {
                        Environment.ExitCode = NativeMethods.ERROR_UNHANDLED_EXCEPTION;
                        ServiceModelReg app = new ServiceModelReg(); 
                        Environment.ExitCode = app.Run(args);
                    } 
                    else 
                    {
                        Console.WriteLine(SR2.GetString(SR2.ServiceModelRegMultipleInstanceError)); 
                    }
                }
            }
            catch (SecurityException) 
            {
                Environment.ExitCode = NativeMethods.ERROR_ACCESS_DENIED; 
            } 
#pragma warning suppress 56500 // covered by FxCOP
            catch (Exception e) 
            {
                ServiceModelReg.LogException(e);

                if (e is System.ComponentModel.Win32Exception) 
                {
                    System.ComponentModel.Win32Exception win32Exception = (System.ComponentModel.Win32Exception)e; 
                    Environment.ExitCode = win32Exception.NativeErrorCode; 
                }
                else if (null != e.InnerException && e.InnerException is System.ComponentModel.Win32Exception) 
                {
                    System.ComponentModel.Win32Exception win32Exception = (System.ComponentModel.Win32Exception)e.InnerException;
                    Environment.ExitCode = win32Exception.NativeErrorCode;
                } 
                else
                { 
                    Environment.ExitCode = NativeMethods.ERROR_UNHANDLED_EXCEPTION; 
                }
            } 

            EventLogger.TerminateMsiStyleLogging();

            return Environment.ExitCode; 
        }
 
        int Run(string[] args) 
        {
            try 
            {
                this.commandArguments = new CommandArguments(args);
            }
            catch (ApplicationException e) 
            {
                DisplayLogo(); 
                ServiceModelReg.LogException(e); 

                Console.WriteLine(); 
                CommandArguments.DisplayUsage();
                return NativeMethods.ERROR_APP_INIT_FAILURE;
            }
 
            if (this.commandArguments.RunInstallScript)
            { 
                EventLogger.InitializeMsiStyleLogging(); 
            }
 
            if (this.commandArguments.DisplayLogo || this.commandArguments.DisplayHelp)
            {
                DisplayLogo();
            } 

            if (this.commandArguments.DisplayHelp) 
            { 
                CommandArguments.DisplayUsage();
                return NativeMethods.ERROR_SUCCESS; 
            }

            Queue actionQueue = BuildActionQueue();
 
            if (0 == actionQueue.Count)
            { 
                throw new ApplicationException(SR2.GetString(SR2.ServiceModelRegNoValidOptionsError)); 
            }
 
            // If action is Install, Uninstall[All], or Reinstall log start message in event log
            if (InstallAction.Install == this.commandArguments.InstallAction ||
                InstallAction.Uninstall == this.commandArguments.InstallAction ||
                InstallAction.Reinstall == this.commandArguments.InstallAction) 
            {
                StringBuilder eventLogStartMessage = new StringBuilder(); 
                eventLogStartMessage = eventLogStartMessage.Append(SR2.GetString(SR2.EventLogMessageStart)); 

                foreach (ActionItem actionItem in actionQueue) 
                {
                    eventLogStartMessage = eventLogStartMessage.Append(actionItem.Component.GetActionString(actionItem.Action) + "\n");
                }
 
                EventLogger.LogInformation(eventLogStartMessage.ToString(), OutputLevel.Verbose == this.commandArguments.OutputLevel);
            } 
 
            Nullable confirmUninstall = null;
 
            if (InstallAction.Uninstall == this.commandArguments.InstallAction ||
                InstallAction.Reinstall == this.commandArguments.InstallAction)
            {
                if (this.commandArguments.RequireConfirmation) 
                {
                    while (!confirmUninstall.HasValue) 
                    { 

                        Console.WriteLine(); 
                        Console.WriteLine(SR2.GetString(SR2.RequestConfirmation));

                        foreach (ActionItem actionItem in actionQueue)
                        { 
                            Console.WriteLine("\t" + actionItem.Component.DisplayName);
                        } 
 
                        Console.Write(SR2.GetString(SR2.AreYouSure));
 
                        string userConfirmation = Console.ReadLine().Trim();

                        if (userConfirmation.Equals(SR2.GetString(SR2.Yes), StringComparison.OrdinalIgnoreCase))
                        { 
                            confirmUninstall = true;
                        } 
                        else if (userConfirmation.Equals(SR2.GetString(SR2.No), StringComparison.OrdinalIgnoreCase)) 
                        {
                            confirmUninstall = false; 
                        }
                    }
                }
                else 
                {
                    confirmUninstall = true; 
                } 
            }
 
            bool tolerateActionFailures = this.commandArguments.RunInstallScript &&
                (this.commandArguments.InstallAction == InstallAction.Uninstall);
            Exception lastException = null;
            while (0 < actionQueue.Count) 
            {
                ActionItem actionItem = actionQueue.Dequeue(); 
                try 
                {
                    this.PerformAction(actionItem, confirmUninstall); 
                }
                catch (Exception exception)
                {
                    if (IsFatal(exception)) 
                    {
                        throw; 
                    } 

                    bool tolerateWebHostFailure = this.commandArguments.RunInstallScript && 
                        (actionItem.Component is WebHostScriptMappingsInstallComponent);
                    if (tolerateWebHostFailure)
                    {
                        ServiceModelReg.LogException(exception); 
                    }
                    else if (tolerateActionFailures) 
                    { 
                        ServiceModelReg.LogException(exception);
                        lastException = exception; 
                    }
                    else
                    {
                        throw; 
                    }
                } 
            } 

            if (lastException != null) 
            {
                throw lastException;
            }
 
            // If action is Install, Uninstall, or Reinstall log success message in event log
            if (InstallAction.Install == this.commandArguments.InstallAction || 
                InstallAction.Uninstall == this.commandArguments.InstallAction || 
                InstallAction.Reinstall == this.commandArguments.InstallAction)
            { 
                EventLogger.LogInformation(SR2.GetString(SR2.EventLogMessageSuccess), OutputLevel.Verbose == this.commandArguments.OutputLevel);
            }

            return NativeMethods.ERROR_SUCCESS; 
        }
 
        static bool IsFatal(Exception exception) 
        {
            while (exception != null) 
            {
                if ((exception is OutOfMemoryException && !(exception is InsufficientMemoryException)) ||
                    exception is ThreadAbortException ||
                    exception is AccessViolationException || 
                    exception is SEHException)
                { 
                    return true; 
                }
 
                // These exceptions aren't themselves fatal, but since the CLR uses them to wrap other exceptions,
                // we want to check to see whether they've been used to wrap a fatal exception.  If so, then they
                // count as fatal.
                if (exception is TypeInitializationException || 
                    exception is TargetInvocationException)
                { 
                    exception = exception.InnerException; 
                }
                else 
                {
                    break;
                }
            } 

            return false; 
        } 

        void PerformAction(ActionItem actionItem, Nullable confirmUninstall) 
        {
            switch (actionItem.Action)
            {
                case (InstallAction.Install): 
                    if (OutputLevel.Quiet != actionItem.OutputLevel)
                    { 
                        EventLogger.LogToConsole(SR2.GetString(SR2.InstallMessage, actionItem.Component.DisplayName)); 
                    }
 
                    EventLogger.WriteMsiStyleLogEntry(SR2.GetString(SR2.InstallMessage, actionItem.Component.DisplayName));
                    actionItem.Component.Install(actionItem.OutputLevel);
                    break;
 
                case (InstallAction.Uninstall):
                    if (confirmUninstall.HasValue && confirmUninstall.Value) 
                    { 
                        if (OutputLevel.Quiet != actionItem.OutputLevel)
                        { 
                            EventLogger.LogToConsole(SR2.GetString(SR2.UninstallMessage, actionItem.Component.DisplayName));
                        }

                        EventLogger.WriteMsiStyleLogEntry(SR2.GetString(SR2.UninstallMessage, actionItem.Component.DisplayName)); 
                        actionItem.Component.Uninstall(actionItem.OutputLevel);
                    } 
                    break; 

                case (InstallAction.Reinstall): 
                    if (confirmUninstall.HasValue && confirmUninstall.Value)
                    {
                        actionItem.Component.Reinstall(actionItem.OutputLevel);
                    } 
                    break;
 
                case (InstallAction.ListVersions): 
                    Console.WriteLine();
                    Console.WriteLine(SR2.GetString(SR2.ServiceModelRegInstalledVersions, actionItem.Component.DisplayName)); 

                    if (actionItem.Component.IsInstalled)
                    {
                        string[] installedVersions = actionItem.Component.InstalledVersions; 
                        if (null == installedVersions || 0 == installedVersions.Length)
                        { 
                            Console.WriteLine("\t" + SR2.GetString(SR2.ServiceModelRegNone)); 
                        }
                        else 
                        {
                            foreach (string version in installedVersions)
                            {
                                Console.WriteLine("\t" + String.Format(CultureInfo.CurrentUICulture, version)); 
                            }
                        } 
                    } 
                    else
                    { 
                        Console.WriteLine("\t" + SR2.GetString(SR2.ServiceModelRegNone));
                    }
                    break;
 
                case (InstallAction.ListScriptMaps):
                    WebHostScriptMappingsInstallComponent webHostHandlersComponent = (WebHostScriptMappingsInstallComponent)actionItem.Component; 
                    string[] installedPaths = webHostHandlersComponent.InstalledPaths; 

                    Console.WriteLine(); 
                    Console.WriteLine(SR2.GetString(SR2.ServiceModelRegInstalledScriptMaps));
                    if (null == installedPaths || 0 == installedPaths.Length)
                    {
                        Console.WriteLine("\t" + SR2.GetString(SR2.ServiceModelRegNone)); 
                    }
                    else 
                    { 
                        foreach (string scriptMapInfo in installedPaths)
                        { 
                            Console.WriteLine(String.Format(CultureInfo.CurrentUICulture, scriptMapInfo));
                        }
                    }
                    break; 

                case (InstallAction.VerifyInstall): 
                    InstallationState installState = actionItem.Component.VerifyInstall(); 

                    string installStateText = SR2.GetString(SR2.InstallationStateUnknown); 

                    switch (installState)
                    {
                        case (InstallationState.Unknown): 
                            installStateText = SR2.GetString(SR2.InstallationStateUnknown);
                            break; 
 
                        case (InstallationState.NotInstalled):
                            installStateText = SR2.GetString(SR2.InstallationStateNotInstalled); 
                            break;

                        case (InstallationState.InstalledDefaults):
                            installStateText = SR2.GetString(SR2.InstallationStateInstalledDefaults); 
                            break;
 
                        case (InstallationState.InstalledCustom): 
                            installStateText = SR2.GetString(SR2.InstallationStateInstalledCustom);
                            break; 
                    }

                    Console.WriteLine(actionItem.Component.DisplayName + "\t:\t" + installStateText);
                    break; 
            }
        } 
 
        // Since we are outputing to the console, force the current UI culture to be the
        // console fallback UI culture since the console cannot support all code pages for 
        // certain cultures (i.e. it will print garbage characters).  Unfortunately, there
        // are some console fallback cultures that still use code pages incompatible with
        // the console.  Catch those cases and fall back to English because ASCII is widely
        // accepted in OEM code pages. 
        static void ValidateUICulture()
        { 
            Thread.CurrentThread.CurrentUICulture = CultureInfo.CurrentUICulture.GetConsoleFallbackUICulture(); 

            if ((System.Console.OutputEncoding.CodePage != Encoding.UTF8.CodePage) && 
                (System.Console.OutputEncoding.CodePage != Thread.CurrentThread.CurrentUICulture.TextInfo.OEMCodePage))
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("en");
            } 
        }
    } 
} 

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