Exemple #1
0
        public Credentials(ServiceAccount account)
        {
            if (account == ServiceAccount.User)
            {
                throw new ArgumentException("You must use the constructor that takes username and password when passing a User account.");
            }

            Username = account == ServiceAccount.LocalSystem
                                ? "NT AUTHORITY\\SYSTEM"
                                : $"NT AUTHORITY\\{Regex.Replace(account.ToString(), "[a-z][A-Z]", m => $"{m.Value[0]} {m.Value[1]}")}";

            Account = account;
        }
Exemple #2
0
        public ServiceWindow(WindowsApplicationService windowsApplicationService)
        {
            InitializeComponent();
            TextBoxServiceName.Text = windowsApplicationService.DisplayName;
            TextBoxDescription.Text = windowsApplicationService.Description;

            int index = -1;
            ServiceStartMode serviceStartMode;

            if (!Enum.TryParse(windowsApplicationService.StartType, out serviceStartMode))
            {
                MessageBox.Show("Unable to parse the Startup Type", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else
            {
                for (int x = 0; x < ComboBoxStartupType.Items.Count; x++)
                {
                    var item    = ComboBoxStartupType.Items[x] as ComboBoxItem;
                    var content = item.Content as string;
                    if (content == serviceStartMode.ToString())
                    {
                        index = x;
                        break;
                    }
                }
            }

            index = -1;
            if (!string.IsNullOrEmpty(windowsApplicationService.ServiceAccountName))
            {
                ServiceAccount serviceAccount = windowsApplicationService.ServiceAccount;
                for (int x = 0; x < ComboBoxServiceAccount.Items.Count; x++)
                {
                    var item    = ComboBoxServiceAccount.Items[x] as ComboBoxItem;
                    var content = item.Content as string;
                    if (content == serviceAccount.ToString())
                    {
                        index = x;
                        break;
                    }
                }
            }
            ComboBoxServiceAccount.SelectedIndex = index;

            ButtonInstallUpdate.Content = "Update Service";
            ButtonInstallUpdate.Click  += Button_Click_UpdateService;
        }
        /// <summary>
        ///     Installs the service(s) in the specified assembly.
        /// </summary>
        /// <param name="path">
        ///     A string containing the path to the service assembly.
        /// </param>
        /// <param name="startMode">
        ///     An enumeration that describes how and when this service is started.
        /// </param>
        /// <param name="account">
        ///     An enumeration that describes the type of account under which the service will run.
        /// </param>
        /// <param name="credentials">
        ///     The user credentials of the account under which the service will run.
        /// </param>
        /// <param name="parameters">
        ///     A dictionary of parameters passed to the service's installer.
        /// </param>
        public static void InstallService(string path, ServiceStartMode startMode, ServiceAccount account, NetworkCredential credentials, StringDictionary parameters)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentException("The specified path parameter is invalid.");
            }

            string filename = Path.GetFileNameWithoutExtension(path);

            try
            {
                // Initialize the service installer
                Assembly serviceAssembly  = Assembly.LoadFrom(path);
                var      serviceInstaller = new AssemblyInstaller(serviceAssembly, null);

                var commandLine = new ArrayList
                {
                    string.Format("StartMode={0}", startMode.ToString("g"))
                };

                // Set the service start mode

                // Set the service account
                switch (account)
                {
                case ServiceAccount.LocalService:
                case ServiceAccount.NetworkService:
                case ServiceAccount.LocalSystem:
                {
                    commandLine.Add(string.Format("Account={0}", account.ToString("g")));
                    break;
                }

                case ServiceAccount.User:
                {
                    commandLine.Add(string.Format("Account={0}", CredentialHelper.GetFullyQualifiedName(credentials)));
                    commandLine.Add(string.Format("Password={0}", credentials.Password));
                    break;
                }
                }

                // Set any parameters
                if (parameters != null)
                {
                    foreach (string key in parameters.Keys)
                    {
                        commandLine.Add(string.Format("{0}={1}", key, parameters[key]));
                    }
                }

                // Initialize the service installer
                serviceInstaller.CommandLine = ( string[] )commandLine.ToArray(typeof(string));

                // Initialize the base installer
                var transactedInstaller = new TransactedInstaller( );
                transactedInstaller.Installers.Add(serviceInstaller);
                transactedInstaller.Context = new InstallContext(string.Format("{0}.log", filename), ( string[] )commandLine.ToArray(typeof(string)));

                // Install the service
                var savedState = new Hashtable( );
                transactedInstaller.Install(savedState);
            }
            catch (Exception exception)
            {
                throw new Exception("Unable to install the specified service.", exception);
            }
        }
Exemple #4
0
 public Credentials(ServiceAccount account)
 {
     if (account == ServiceAccount.User)
     {
         throw new ArgumentException("You must use the constructor that takes username and password when passing a User account.");
     }
     this.Username = (account == ServiceAccount.LocalSystem ? "NT AUTHORITY\\SYSTEM" : string.Format("NT AUTHORITY\\{0}", Regex.Replace(account.ToString(), "[a-z][A-Z]", (Match m) => string.Format("{0} {1}", m.Value[0], m.Value[1]))));
     this.Account  = account;
 }