5

我有一个托管在 consol 应用程序中的 WCF 服务(也充当 Windows 服务安装程序),请在此处查看更多信息:http: //msdn.microsoft.com/en-us/library/ms733069.aspx

这是 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 服务中运行而设计的,我该如何做才能在调试模式下(在开发期间)将其作为常规 selfhost 运行?还是我真的必须启动一个特殊项目才能在运行时调试此服务?

编辑:

我决定使用现有的 windows 服务项目,但将 main 更改为如下内容:

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());
        }
4

1 回答 1

6

这就是我所做的

解决方案 A

  • InstallUtil使用我的Debug\bin文件夹安装 Windows 服务
  • sc start使用或停止和启动服务sc stop
  • 服务启动后,请执行Debug > Attach to Process...并将 VS 附加到服务

解决方案 B

在OnStart方法的第一行调用Debugger.Break

解决方案 C

添加一个临时单独的控制台应用程序,该应用程序与您的服务执行相同的工作。

于 2012-07-13T12:26:35.843 回答