Common usage of static array in two different console applications - c#

I've got a problem with a .NET Core 2.2 console application.
I want to start an console app with an static array. This one connects to an TCP/IP partner.
On start up, I start an second console application, which should use the same static array, and the same connection as the first console application.
I want to split both apps, cause it is an 'option package'. Both apps are running as background services with timed async functions.
Both apps share the same namespace and are configured in the same project.
The child app have an dependency of the parent app.
Until now, I can start both apps in the same console (as child-process), but the child-console can't use the static array.
How can I solve it?
Here is my Program.cs Main Method:
static async Task Main(string[] args) {
Portal_Connect.PLC = new Class_PLC[11];
if ((Configuration.Diagnostic))
{
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "dotnet ",
Arguments = Directory.GetCurrentDirectory() + \\Diagnostic.dll",
WorkingDirectory = Directory.GetCurrentDirectory(),
UseShellExecute = false,
RedirectStandardOutput = false,
RedirectStandardError = false,
CreateNoWindow = false
}
};
process.Start();
process.PriorityClass = ProcessPriorityClass.High;
process.ProcessorAffinity = (IntPtr)6;
}
}

Related

Trying to automatically rebuild and restart .NET console application on linux

I'm making a small webserver .NET console application on my windows machine that will run on a linux server (amazon os), and I'd like to make it automatically update itself when I push new code to github.
Automatically pulling from github already works by waiting for a webhook and then creating a Process which, as I understand it, practically lets me run a command line from the code.
using System.Diagnostics;
public static class GitApi
{
public static void Pull(string path)
{
Process process = new Process()
{
StartInfo = new ProcessStartInfo()
{
FileName = "git",
Arguments = "pull",
UseShellExecute = false,
RedirectStandardOutput = true,
WorkingDirectory = path
}
};
process.Start();
}
}
What I'd then like to do is to automatically rebuild and restart after pulling from git, by using the dotnet run command in a similar way.
using System.Diagnostics;
public static class Restarter
{
public static void Restart(string path)
{
Process process = new Process()
{
StartInfo = new ProcessStartInfo()
{
FileName = "dotnet",
Arguments = "run",
UseShellExecute = false,
WorkingDirectory = path
}
};
process.Start();
Environment.Exit(0);
}
}
While this seems to work fine on windows, when I try on linux it seems to exit the main process and then start the new one in the background somehow. It still writes in the same screen, but I can't kill it with Ctrl + C. Instead I have to find the process by checking the port that it's using and then use the kill command.
Could I fix this somehow or is there another simple way to rebuild and restart a .NET console application?

not able to call .net core process from .net core web application

I have a ASP .NET core web application (win service) that needs to call for another .net Core process (console application) to execute.
I have the following code for the call:
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "dotnet",
Arguments = #"C:\CxSASTEngine\CxEngine\Product\netcoreapp2.1\ScanClient.dll" + " blablabla",
UseShellExecute = false,
RedirectStandardOutput = false,
RedirectStandardError = false,
CreateNoWindow = false,
}
};
process.Start();
If I place the same code in a new .Net core console application, the process runs, but in the scope of the win service it fails to run.
The ScanClient.dll code is:
static void Main(string[] args)
{ try
{
Console.WriteLine(args.Length);
if (args.Length > 0)
{
Console.WriteLine(args[0]);
}
Console.ReadLine();
if (File.Exists(#"C:\Users\Mary\Desktop\mary.txt"))
{
File.Delete(#"C:\Users\Mary\Desktop\mary.txt");
}
File.Create(#"C:\Users\Mary\Desktop\mary.txt");
}
catch (Exception)
{
Environment.Exit(-1);
}
}
what can be the reason for the code not to run in the scope of the win service?
Just to be fair, if I create the ScanClient as a .netFramewrok console application that generates an .exe file, the process is being executed.

Pass args to Main(string[] args)

I am trying to build a Console Application to start my .NetCore Web Applications that I've got built as a .dll
Unfortunately the Args entered in the ProcessStartInfo are not being received by my application, however my application does start and i get a unexpected behaviour in the Console.WriteLine Method.
This code is inside my SocketAPI Project which is a .NetCore 2.2 WebApplication | API Project:
public static void Main(string[] args)
{
// Outputs -> Received :
// ?? Why is args.Length empty? Not even 0 or null??
Console.WriteLine(string.Format("Received : ",args.Length));
CreateWebHostBuilder(args).Build().Run();
}
It gets declared and called by my ProcessRunner which is a Class that holds the current Process:
I am also referring to this documentation:
dotnet command documentation on microsoft.com
Which describes: dotnet [command] [arguments]
This Code is inside the ProcessRunner Constructor
ProcessStartInfo = new ProcessStartInfo
{
FileName = "dotnet",
Arguments = string.Format("BHR.{0}.dll {1}", Name, args),
WorkingDirectory = ".\\",
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
};
Later on I'm calling Process.Start(); inside the ProcessRunner to start the Process.
As said I do get Output but no Args... So why do i see "Received :" but nothing on the end?
Do i have to enable my SocketAPI to receive args when starting them as a process? I've been trying to deal with the problem since 2 days now and I'm completely clueless...
Kind Regards
In your code you are not adding a placeholder for the argument length.
Console.WriteLine(string.Format("Received: {0} ",args.Length));
FYI, This works for me:
var arguments = "myarg1 myarg2";
var dir = #"C:\somedir\somechilddir";
var info = new System.Diagnostics.ProcessStartInfo("dotnet", $"someproject.dll {arguments}");
info.UseShellExecute = false;
info.CreateNoWindow = true;
info.WorkingDirectory = dir;
var process = new System.Diagnostics.Process();
process.StartInfo = info;
process.Start();
process.WaitForExit();
Perhaps, try resolving your working directory using Path.GetFullPath();
Big thank you to #AlexanderHiggins for showing me how dull i should feel now!
In your code you are not adding a placeholder for the argument length.
Console.WriteLine(string.Format("Received: {0} ",args.Length));
Sorry for bothering!

Open .dll in new console window on Linux .Net Core

I am able to run program.dll in same console window by running:
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "dotnet",
Arguments = "program.dll",
UseShellExecute = true,
RedirectStandardOutput = false,
RedirectStandardError = false,
CreateNoWindow = false
}
};
process.Start();
flag CreateNoWindow = false do not open new console as on Windows system.
How to open program.dll in new console window on Linux using .NET Core?
I saw this question but it do not work on linux.
Alternative is this but it involves running program.dll via .sh scripts which I do not want to do.
At the moment it is not possible to open new console window by using that class. Flag CreateNoWindow is ignored on linux in .NETCore 2.0 (issue on .NETCore github)
However there is workaround available - using xterm to run new console with .dll process.
var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = "xterm",
Arguments = $"-e dotnet <pathToDll>",
RedirectStandardOutput = true,
CreateNoWindow = true,
UseShellExecute = false,
},
};
process.Start();

Commands missing on command-line started from IIS 8.5

I've used a IHttpHandler that compiles SASS files with compass since IIS 7.5. Ever since upgrading to windows 8.1 and IIS 8.5, it's not been working and responds with the follow message:
/*-----------------------------------------------------------------------------------------------
//
// compass compile "theme/default/style/custom.sass" --trace --debug-info --css-dir "data/compass"
//
// 'compass' is not recognized as an internal or external command,
// operable program or batch file.
//
//---------------------------------------------------------------------------------------------*/
Here the portion code that executes the process (ref Dado.Compass.SingleFileHandler.cs):
void IHttpHandler.ProcessRequest(HttpContext context)
{
// Gather Compass Configuration Variables
ReadConfiguration(context);
string baseDir = Path.GetDirectoryName(context.Request.Path).Replace(#"\", "/");
string fileName = Path.GetFileName(context.Request.Path);
string command = String.Format(#"compass compile ""{0}"" --trace --debug-info --css-dir ""{1}""",
(baseDir + "/" + fileName).Trim(new char[] { '\\', '/' }),
_cachePath.Trim(new char[] { '\\', '/' })
);
ProcessStartInfo psi = new ProcessStartInfo()
{
FileName = "cmd.exe",
WorkingDirectory = context.Server.MapPath("~/"),
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
Arguments = "/c " + command
};
using (Process process = new Process { StartInfo = psi }) {
bool hasError = false;
...
I'm running under ApplicationPoolIdentify and have given the same permissions. Also, compass is available when I try to execute it the command from cmd.exe.
Why does the command appear from the IIS instance of the cmd.exe?
I don't know if this is a new setting in Window 8, but it seems there is now a User Path variable and this is the variable that was referencing compass. However, the IIS process worker is the ApplicationPoolIdentity, so it will not receive my User's Path specification.
The solution is to move the referencing location from the User Path variable to the System Path variable.

Categories

Resources