使用WCF自托管在调试模式下运行consol应用程序?

Ban*_*hee 5 .net c# wcf self-hosting

我有一个在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();
        }

    }
Run Code Online (Sandbox Code Playgroud)

这是为了在Windows服务中运行,我该怎么做才能在调试模式下(在开发期间)将其作为常规自主机运行?或者我是否真的必须启动一个特殊项目才能在运行时调试此servuce?

编辑:

我决定使用现有的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());
        }
Run Code Online (Sandbox Code Playgroud)

ole*_*sii 6

这就是我的工作

解决方案A.

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

解决方案B.

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

解决方案C.

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