Exemple #1
0
        /// <summary>
        /// Applies changes since the most recent apply.
        /// </summary>
        /// <returns> true if changes can be applied. </returns>
        public bool CanApplyChanges()
        {
            ManagementObjectSearcher ServiceQuery = new ManagementObjectSearcher
                                                        ("Select * from Win32_Service Where DisplayName ='" + serviceResultNode.DisplayName + "'");

            object[] parameters = new object[1];
            parameters[0] = ComboBoxStartupType.SelectedItem.ToString();

            foreach (ManagementObject ServiceObject in ServiceQuery.Get())
            {
                ServiceObject.InvokeMethod("ChangeStartMode", parameters);
            }

            serviceResultNode.SubItemDisplayNames[2] = ComboBoxStartupType.SelectedItem.ToString();
            return(true);
        }
        /// <summary>
        /// Handles MessageReceived events of all clients, evaluates each message, finds appropriate
        /// service object and invokes appropriate method.
        /// </summary>
        /// <param name="sender">Source of event</param>
        /// <param name="e">Event arguments</param>
        private void Client_MessageReceived(object sender, MessageEventArgs e)
        {
            // Get RequestReplyMessenger object (sender of event) to get client
            RequestReplyMessenger <IScsServerClient> requestReplyMessenger = (RequestReplyMessenger <IScsServerClient>)sender;

            // Cast message to ScsRemoteInvokeMessage and check it
            if (!(e.Message is ScsRemoteInvokeMessage invokeMessage))
            {
                return;
            }

            try
            {
                // Get client object
                IScsServiceClient client = _serviceClients[requestReplyMessenger.Messenger.ClientId];
                if (client == null)
                {
                    requestReplyMessenger.Messenger.Disconnect();
                    return;
                }

                // Get service object
                ServiceObject serviceObject = _serviceObjects[invokeMessage.ServiceClassName];
                if (serviceObject == null)
                {
                    SendInvokeResponse(requestReplyMessenger, invokeMessage, null, new ScsRemoteException("There is no service with name '" + invokeMessage.ServiceClassName + "'"));
                    return;
                }

                // Invoke method
                try
                {
                    // Set client to service, so user service can get client in service method using
                    // CurrentClient property.
                    object returnValue;
                    serviceObject.Service.CurrentClient = client;
                    try
                    {
                        returnValue = serviceObject.InvokeMethod(invokeMessage.MethodName, invokeMessage.Parameters);
                    }
                    finally
                    {
                        // Set CurrentClient as null since method call completed
                        serviceObject.Service.CurrentClient = null;
                    }

                    // Send method invocation return value to the client
                    SendInvokeResponse(requestReplyMessenger, invokeMessage, returnValue, null, invokeMessage.Parameters);
                }
                catch (TargetInvocationException ex)
                {
                    Exception innerEx = ex.InnerException;
                    if (innerEx != null)
                    {
                        SendInvokeResponse(requestReplyMessenger, invokeMessage, null, new ScsRemoteInvocationException(invokeMessage.ServiceClassName, serviceObject.ServiceAttribute?.Version ?? "", invokeMessage.MethodName, innerEx.Message, innerEx));
                    }
                }
                catch (Exception ex)
                {
                    SendInvokeResponse(requestReplyMessenger, invokeMessage, null, new ScsRemoteInvocationException(invokeMessage.ServiceClassName, serviceObject.ServiceAttribute?.Version ?? "", invokeMessage.MethodName, ex.Message, ex));
                }
            }
            catch (Exception ex)
            {
                SendInvokeResponse(requestReplyMessenger, invokeMessage, null, new ScsRemoteException("An error occured during remote service method call.", ex));
            }
        }
        /// <summary>
        /// OnSelectionAction method handles the execution of a selection-dependent action.
        /// </summary>
        /// <param name="action">The executed action.</param>
        /// <param name="status">The object that holds the status information.</param>
        protected override void OnSelectionAction(Action action, AsyncStatus status)
        {
            ManagementObjectSearcher ServiceQuery = new ManagementObjectSearcher
                                                        ("Select * from Win32_Service Where DisplayName = '" + SelectedNodes[0].DisplayName + "'");

            string actionString = action.Tag as string;

            if (actionString != null)
            {
                switch (actionString)
                {
                case "Start":
                {
                    foreach (ManagementObject ServiceObject in ServiceQuery.Get())
                    {
                        ServiceObject.InvokeMethod("StartService", null);
                    }
                    SelectedNodes[0].SubItemDisplayNames[1]             = "Started";
                    ((Action)SelectionData.ActionsPaneItems[0]).Enabled = false;
                    ((Action)SelectionData.ActionsPaneItems[3]).Enabled = false;
                }
                break;

                case "Stop":
                {
                    foreach (ManagementObject ServiceObject in ServiceQuery.Get())
                    {
                        ServiceObject.InvokeMethod("StopService", null);
                    }
                    SelectedNodes[0].SubItemDisplayNames[1]             = "Stopped";
                    ((Action)SelectionData.ActionsPaneItems[1]).Enabled = false;
                    ((Action)SelectionData.ActionsPaneItems[2]).Enabled = false;
                    ((Action)SelectionData.ActionsPaneItems[3]).Enabled = false;
                }
                break;

                case "Pause":
                {
                    foreach (ManagementObject ServiceObject in ServiceQuery.Get())
                    {
                        ServiceObject.InvokeMethod("PauseService", null);
                    }
                    SelectedNodes[0].SubItemDisplayNames[1]             = "Paused";
                    ((Action)SelectionData.ActionsPaneItems[0]).Enabled = false;
                    ((Action)SelectionData.ActionsPaneItems[2]).Enabled = false;
                }
                break;

                case "Resume":
                {
                    foreach (ManagementObject ServiceObject in ServiceQuery.Get())
                    {
                        ServiceObject.InvokeMethod("ResumeService", null);
                    }
                    SelectedNodes[0].SubItemDisplayNames[1]             = "Started";
                    ((Action)SelectionData.ActionsPaneItems[0]).Enabled = false;
                    ((Action)SelectionData.ActionsPaneItems[3]).Enabled = false;
                }
                break;


                case "Properties":
                {
                    SelectionData.ShowPropertySheet("Service Properties");
                }
                break;
                }
            }
        }