Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running consol application in debug mode with WCF selfhosting?

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

This is how the class in the consol application looks like :

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 made for running in a Windows Service, how do I make so I can run this as a regular selfhost in debug mode(during development)? or do I really have to start a special project to be able to debug this servuce during runtime?

Edit:

I decided to use the existing windows service project but change the main 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());
        }
like image 976
Banshee Avatar asked Jul 13 '12 12:07

Banshee


1 Answers

This is what I do

Solution A

  • Install a Windows Service using InstallUtil from my Debug\bin folder
  • Stop and start service using sc start or sc stop
  • Once service started do Debug > Attach to Process... and attach VS to the service

Solution B

Have a Debugger.Break call on the first line of the OnStart method.

Solution C

Add a temp separate console application that does the same job as your service.

like image 194
oleksii Avatar answered Nov 15 '22 08:11

oleksii