I have a controller and a PythonJobRepository where I call a python scrypt. This is the repository
public class PythonJobRepository
{
private Process process { get; set; }
private readonly DatabaseContext _context;
public bool IsJobRunning
{
get
{
return process.HasExited;
}
}
public PythonJobRepository(DatabaseContext context)
{
_context = context;
}
public async Task<string> StartJobAsync()
{
List<SearchTerms> searchTerms = await _context.SearchTerms.ToListAsync();
var json = JsonConvert.SerializeObject(searchTerms);
json = json.Replace("\"", "'");
ProcessStartInfo processInfo = new ProcessStartInfo();
string scriptPath = Path.Combine(System.IO.Directory.GetCurrentDirectory(), "Python\\scrapeGoogle.py");
processInfo.FileName = "python";
processInfo.Arguments = string.Format("{0} \"{1}\"", scriptPath, json);
processInfo.UseShellExecute = false;
processInfo.CreateNoWindow = true;
processInfo.RedirectStandardOutput = true;
processInfo.RedirectStandardError = true;
process = Process.Start(processInfo);
using (StreamReader reader = process.StandardOutput)
{
string stderr = await process.StandardError.ReadToEndAsync();
var result = await reader.ReadToEndAsync();
process.WaitForExit();
process.Close();
if (stderr == "")
{
return result;
}
else
{
return stderr;
}
};
}
public void StopJob()
{
if (!process.HasExited)
{
process.Kill();
}
}
}
And I register it as a Transient
services.AddTransient<PythonJobRepository,PythonJobRepository>();
I inject this repository in my controller and I run the StartJobAsync() method. The problem is when I call StopJob(), the process instance is null. I am pretty new to c# and asp, hope someone can help me