2016-12-20 193 views
-2

非常奇怪的问题...我在我的解决方案中有两个控制台应用程序,控制台应用程序1,确实显示控制台,当我运行它并将其设置为启动项目时。 。控制台应用程序2,运行...执行...但不显示控制台。为什么我的控制台应用程序没有显示控制台

下面是代码:

public class Program 
    { 
     static void Main() 
     { 
      string baseAddress = "http://localhost:8080/"; 

      using (WebApp.Start<StartUp>(url: baseAddress)) 
      { 
       Console.Write("Service Listening at : " + baseAddress); 
       System.Threading.Thread.Sleep(-1); 
      } 
     } 
    } 

这里是类时启动Main方法返回,除非一个子线程标记为前台线程

public class StartUp 
    { 
     public void Configuration(IAppBuilder appBuilder) 
     { 
      HttpConfiguration config = new HttpConfiguration(); 
      config.EnableCors(); 

      config.Routes.MapHttpRoute(
       name: "DefaultApi", 
       routeTemplate: "api/{controller}/{id}", 
       defaults: new { id = RouteParameter.Optional }); 
      config.Formatters.Clear(); 
      config.Formatters.Add(new JsonMediaTypeFormatter()); 
      config.Formatters.JsonFormatter.SerializerSettings = 
      new JsonSerializerSettings 
      { 
       ContractResolver = new CamelCasePropertyNamesContractResolver() 
      }; 
      appBuilder.UseWebApi(config); 
     } 
    } 
+3

你是否已经在调试器中检查了应用程序,看它是否命中了Console.Write? – mrogers

+0

是的,我有,它确实有效,然后我改变了启动并重建它,然后 –

+0

告诉我们两个**控制台应用程序的代码 – MickyD

回答

0

控制台应用程序将终止。您的程序运行速度可能非常快,您永远无法看到显示的控制台窗口。

WebApp.Start不是阻止方法,您的Main方法中也不会有任何后续调用。

只需在“服务监听”消息后添加一个Console.ReadLine()呼叫。不需要Thread.Sleep(-1),因为您的代码不在协同多任务环境中运行。

+0

我试过了,但没有奏效。谢谢 –

+0

它是如何“不起作用”的?你有没有观察到同样的行为? – Dai

+0

我把Console.ReadLine();拿出Thread.Sleep(-1); ...按CMD 5,它直接进入调试模式...但没有窗口 –

相关问题