2012-07-13 85 views
5

我有一個統一公債應用託管WCF服務(也作爲Windows服務安裝程序),懇求多看這裏:http://msdn.microsoft.com/en-us/library/ms733069.aspx使用WCF自託管在調試模式下運行CONSOL應用程序?

這是康壽應用程序的類的樣子:

public class MyAppWindowsService : ServiceBase 
    { 
     public ServiceHost _MyAppClientServiceHost = null; 
     public ServiceHost _MyAppIntegrationServiceHost = null; 
     public ServiceHost _MyAppserviceHost = null; 

     public MyAppWindowsService() 
     { 
      // Name the Windows Service 
      ServiceName = "MyApp Service"; 
     } 

     public static void Main() 
     { 
      ServiceBase.Run(new MyAppWindowsService()); 
     } 

     private void StopService(ServiceHost serviceHost) 
     { 
      if (serviceHost != null) 
      { 
        serviceHost.Close(); 
        serviceHost = null; 
      } 
     } 
     private ServiceHost StartService(Type serviceType) 
     { 
      ServiceHost serviceHost = null; 

      // Create a ServiceHost for the CalculatorService type and 
      // provide the base address. 
      serviceHost = new ServiceHost(serviceType); 

      // Open the ServiceHostBase to create listeners and start 
      // listening for messages. 
      serviceHost.Open(); 

      return serviceHost; 
     } 
     private void StartServices() 
     { 
      StopService(_MyAppClientServiceHost); 
      StopService(_MyAppIntegrationServiceHost); 
      StopService(_MyAppServiceHost); 

      _MyAppClientServiceHost = StartService(typeof(MyApp.ServiceImplementation.MyAppClientService)); 
      _MyAppIntegrationServiceHost = StartService(typeof(MyApp.ServiceImplementation.MyAppIntegration)); 
      _MyAppServiceHost = StartService(typeof(MyApp.ServiceImplementation.HL7Service)); 
     } 
     private void StopServices() 
     { 
      StopService(_MyAppClientServiceHost); 
      StopService(_MyAppIntegrationServiceHost); 
      StopService(_MyAppHl7ServiceHost); 
     } 

     // Start the Windows service. 
     protected override void OnStart(string[] args) 
     { 
      StartServices(); 
     } 

     protected override void OnStop() 
     { 
      StopServices(); 
     } 

    } 

這是爲了在Windows服務中運行而製作的,我該如何製作,因此我可以在調試模式下(開發過程中)將其作爲常規自身主機運行?還是我真的必須啓動一個特殊項目才能在運行時調試這個servuce?

編輯:

我決定利用現有的Windows服務項目,但改變的主要的東西是這樣的:

public static void Main() 
     { 
      if (Debugger.IsAttached) 
      { 
       Console.WriteLine("--- MyApp Services ---"); 
       Console.WriteLine("Starting services..."); 
       Instance.StartServices(); 
       Console.WriteLine("--Finished--"); 
       Console.WriteLine("Press any key to exit"); 
       Console.ReadKey(); 
       Instance.StopServices(); 
      } 
      else 
       ServiceBase.Run(new MyAppWindowsService()); 
     } 

回答

6

這是我做的

解決方案A

  • 安裝Windows服務使用InstallUtil從我調試\ BIN文件夾
  • 停止使用sc startsc stop
  • 啓動服務一旦服務開始做調試>附加到進程...並附VS到服務

溶液B

對的第一行一個Debugger.Break呼叫OnStart方法。

C液

添加一個臨時的單獨的控制檯應用程序,做同樣的工作爲您服務。

+0

當服務在II7中運行時,我可以聲明網站主機應該在調試時啓動並且它將從本地主機運行。這是一個很好的解決方案。但是現在,假設我已經將我的WCF服務安裝爲Windows服務,然後啓動我的CONSOL應用程序進行調試,那麼這個CONSOL應用程序將嘗試在Windows服務已經使用的相同通道上啓動WCF服務?這意味着我必須在調試我的consol App(selfhost)之前停止Windows服務?如果我只能在visual studio中進行調試,那就太好了。 – Banshee 2012-07-13 12:49:01

+0

@SnowJim我明白你的意思,我完全同意這是一種痛苦。但到目前爲止,這是我想出的。我不知道有任何更簡單的解決方案。我將使用選項C(客戶機和服務器是兩個控制檯應用程序,您可以配置爲在F5上啓動它們)。一旦您不需要進行多次調試,我將切換選項A或B. – oleksii 2012-07-13 12:57:21

+0

謝謝!我的解決方案是使用現有的項目,而不是ServiceBase.Run(new(MyAppWindowsService()在主要我添加了一個檢查Dubugger.IsAttached,如果是這樣手動啓動服務,並在CONSOL顯示一些信息。 – Banshee 2012-07-13 13:46:00

相關問題