简体   繁体   English

将args传递给Main(string [] args)

[英]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 我试图构建一个控制台应用程序以启动以.dll格式构建的.NetCore Web应用程序

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. 不幸的是,我的应用程序未收到在ProcessStartInfo中输入的Args,但是我的应用程序确实启动了,并且在Console.WriteLine方法中出现了意外的行为。

This code is inside my SocketAPI Project which is a .NetCore 2.2 WebApplication | 这段代码位于我的SocketAPI项目(它是.NetCore 2.2 WebApplication)中。 API Project: API专案:

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: 它由ProcessRunner声明和调用,ProcessRunner是一个保存当前Process的类:

I am also referring to this documentation: dotnet command documentation on microsoft.com 我也参考此文档: microsoft.com上的dotnet命令文档

Which describes: dotnet [command] [arguments] 其中描述: dotnet [command] [arguments]

This Code is inside the ProcessRunner Constructor 该代码在ProcessRunner构造函数中

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(); 稍后,我调用Process.Start(); inside the ProcessRunner to start the Process. 在ProcessRunner中启动流程。

As said I do get Output but no Args... So why do i see "Received :" but nothing on the end? 如前所述,我确实获得了Output却没有Args ...为什么我看不到“ Received:”,但最后却什么也没有?

Do i have to enable my SocketAPI to receive args when starting them as a process? 在将它们作为进程启动时,是否必须使我的SocketAPI能够接收args? 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(); 也许,尝试使用Path.GetFullPath()解析您的工作目录;

Big thank you to @AlexanderHiggins for showing me how dull i should feel now! 非常感谢@AlexanderHiggins向我展示了我现在应该有多沉闷!

In your code you are not adding a placeholder for the argument length. 在您的代码中,您没有为参数长度添加占位符。

Console.WriteLine(string.Format("Received: {0} ",args.Length)); Console.WriteLine(string.Format(“ Received:{0}”,args.Length));

Sorry for bothering! 抱歉打扰了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM