Running console application in debug mode with self-WCF?

I have a WCF service hosted in a console application (which also acts as a Windows Service installer), please see here: http://msdn.microsoft.com/en-us/library/ms733069.aspx

Here's what the class looks like in a console application:

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

    }

This is done to work in the Windows service, how can I do this to run it as a regular selfhost in debug mode (during development)? or do I really need to run a special project in order to be able to debug this servlet at runtime?

Edit:

I decided to use an existing Windows service project, but changed the main thing to something like this:

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());
        }
+5
source share
1 answer

,

A

  • Windows InstallUtil Debug\bin
  • sc start sc stop
  • Debug > Attach to Process... VS

B

Debugger.Break OnStart.

C

temp, , .

+6

All Articles