从WCF服务启动控制台应用程序

时间:2012-09-29 20:47:20

标签: c#

我有一个Console项目和WCFService项目的解决方案 在WCF我添加了对Console的引用。

如何运行控制台from the service?
Do I need to create a new
进程using System.Diagnostics;`?

WCF内我有以下方法,我想运行控制台:

public String WelComeMessage(String name) {                      
    Process myConsole = new Process(); ///<<<maybe not required?
    //<want run the console here
    return String.Format("{0}, Welcome to http://blah.com", name);
}

所以在VS中,它现在看起来像这样:

enter image description here

3 个答案:

答案 0 :(得分:1)

如果您真正需要做的就是在控制台应用程序中使用类,那么您可以直接使用该类,而不是将其作为控制台应用程序运行,例如。

Program.Main(args);

答案 1 :(得分:0)

你可以做Process.Start("consoleapp.exe"),但你在做什么?

答案 2 :(得分:0)

如果您想从控制台应用程序中读取结果,这样的内容会很有用:

    /// <summary>
    /// Starts and returns result from console application. Execution is done without window.
    /// </summary>
    /// <param name="exePath">Full path to console exe file</param>
    /// <param name="arguments">console exe parameters</param>
    /// <returns></returns>
    static string ReadConsoleResult(string exePath, string arguments)
    {
        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(exePath, arguments);
        startInfo.UseShellExecute = false;
        startInfo.RedirectStandardOutput = true;
        startInfo.CreateNoWindow = true;
        Process p = Process.Start(startInfo);
        string output = p.StandardOutput.ReadToEnd();
        p.WaitForExit();
        return output;
    }