CmdService fix

master
Tudor Stanciu 2022-01-18 18:23:53 +02:00
parent 46f47dfa9f
commit 9d3ca45427
4 changed files with 18 additions and 8 deletions

View File

@ -4,6 +4,7 @@ dotnet publish --configuration Release --runtime win7-x64
Create windows service: Create windows service:
sc create NetworkResurrector.Server binPath= "<path_to_the_service_executable>" sc create NetworkResurrector.Server binPath= "<path_to_the_service_executable>"
sc create NetworkResurrector.Agent binPath= "<path_to_the_service_executable>"
####################################################################################################################################################### #######################################################################################################################################################
Resources: https://github.com/nikeee/wake-on-lan Resources: https://github.com/nikeee/wake-on-lan

View File

@ -7,10 +7,10 @@ namespace NetworkResurrector.Agent.Application.Services.Windows
{ {
internal class CmdService : ICliService internal class CmdService : ICliService
{ {
public (bool success, string message) Execute(string command) => Run("cmd.exe", command); public (bool success, string message) Execute(string command) => Run("cmd.exe", command, null);
public (bool success, string message) Shutdown(string arguments) => Run("shutdown.exe", arguments); public (bool success, string message) Shutdown(string arguments) => Run("shutdown.exe", null, arguments);
private (bool success, string message) Run(string fileName, string arguments) private (bool success, string message) Run(string fileName, string command, string arguments)
{ {
var commandOutput = new StringBuilder(); var commandOutput = new StringBuilder();
var commandErrors = new StringBuilder(); var commandErrors = new StringBuilder();
@ -23,6 +23,9 @@ namespace NetworkResurrector.Agent.Application.Services.Windows
process.StartInfo.UseShellExecute = false; process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = fileName; process.StartInfo.FileName = fileName;
if (arguments != null)
process.StartInfo.Arguments = arguments;
commandOutput.Clear(); commandOutput.Clear();
commandErrors.Clear(); commandErrors.Clear();
@ -39,9 +42,13 @@ namespace NetworkResurrector.Agent.Application.Services.Windows
}); });
process.Start(); process.Start();
process.StandardInput.WriteLine(arguments);
if (command != null)
{
process.StandardInput.WriteLine(command);
process.StandardInput.Flush(); process.StandardInput.Flush();
process.StandardInput.Close(); process.StandardInput.Close();
}
process.BeginOutputReadLine(); process.BeginOutputReadLine();
process.BeginErrorReadLine(); process.BeginErrorReadLine();

View File

@ -11,7 +11,7 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="$(MicrosoftExtensionsPackageVersion)" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="$(MicrosoftExtensionsPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="$(MicrosoftExtensionsPackageVersion)" /> <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="$(MicrosoftExtensionsPackageVersion)" />
<PackageReference Include="NDB.Application.DataContracts" Version="$(NDBApplicationPackageVersion)" /> <PackageReference Include="NDB.Application.DataContracts" Version="$(NDBApplicationPackageVersion)" />
<PackageReference Include="NetworkResurrector.Agent.Wrapper" Version="1.0.3" /> <PackageReference Include="NetworkResurrector.Agent.Wrapper" Version="1.0.3.1" />
<PackageReference Include="NetworkResurrector.Server.Wrapper" Version="1.0.3.3" /> <PackageReference Include="NetworkResurrector.Server.Wrapper" Version="1.0.3.3" />
</ItemGroup> </ItemGroup>

View File

@ -24,7 +24,9 @@ namespace NetworkResurrector.Api.Domain.Data.Repositories
public async Task<Machine> GetMachine(int machineId) public async Task<Machine> GetMachine(int machineId)
{ {
var machine = await _dbContext.Machines.FirstOrDefaultAsync(z => z.MachineId == machineId); var machine = await _dbContext.Machines
.Include(z => z.Agent)
.FirstOrDefaultAsync(z => z.MachineId == machineId);
if (machine == null) if (machine == null)
throw new Exception($"Machine with id {machineId} not found"); throw new Exception($"Machine with id {machineId} not found");