2012-07-13 87 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

相关问题