コード例 #1
0
        /// <summary>
        /// Clears and test related state.
        /// </summary>
        private void ClearState()
        {
            // Put Hyper-V into a known state by ensuring that any test related assets
            // left over from a previous run are removed.

            using (var hyperv = new HyperVClient())
            {
                if (hyperv.GetVm(machineName: TestMachineName1) != null)
                {
                    hyperv.StopVm(TestMachineName1, turnOff: true);
                    hyperv.RemoveVm(TestMachineName1);
                }

                if (hyperv.GetVm(machineName: TestMachineName2) != null)
                {
                    hyperv.StopVm(TestMachineName2, turnOff: true);
                    hyperv.RemoveVm(TestMachineName2);
                }

                if (hyperv.GetSwitch(switchName: TestSwitchName) != null)
                {
                    hyperv.RemoveSwitch(TestSwitchName);
                }

                NeonHelper.DeleteFile(test1VhdxPath);
                NeonHelper.DeleteFile(test2VhdxPath);
            }
        }
コード例 #2
0
        /// <summary>
        /// Stops the named virtual machine.
        /// </summary>
        /// <param name="machineName">The machine name.</param>
        /// <param name="turnOff">
        /// <para>
        /// Optionally just turns the VM off without performing a graceful shutdown first.
        /// </para>
        /// <note>
        /// <b>WARNING!</b> This could result in corruption or the the loss of unsaved data.
        /// </note>
        /// </param>
        public void StopVm(string machineName, bool turnOff = false)
        {
            if (isAdmin)
            {
                hypervClient.StopVm(machineName: machineName, turnOff: turnOff);
            }
            else
            {
                var request = new GrpcStopVmRequest(machineName: machineName, turnOff: turnOff);
                var reply   = desktopService.StopVmAsync(request).Result;

                reply.Error.EnsureSuccess();
            }
        }
コード例 #3
0
        /// <inheritdoc/>
        public async Task <GrpcBaseReply> StopVmAsync(GrpcStopVmRequest request, CallContext context = default)
        {
            await SyncContext.Clear;

            try
            {
                hyperv.StopVm(machineName: request.MachineName, turnOff: request.TurnOff);

                return(new GrpcBaseReply());
            }
            catch (Exception e)
            {
                return(new GrpcBaseReply(e));
            }
        }