Exemple #1
0
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            ServiceHost host = new ServiceHost();

            bool runAsService;
            bool runAsApplication;

            Arguments args = new Arguments(Environment.CommandLine, true);

            if (args.Count == 0)
            {
#if DEBUG
                runAsService     = false;
                runAsApplication = true;
#else
                runAsService     = true;
                runAsApplication = false;
#endif
            }
            else
            {
                runAsService     = args.Exists("RunAsService");
                runAsApplication = args.Exists("RunAsApplication");

                if (!runAsService && !runAsApplication && !args.Exists("RunAsConsole"))
                {
                    MessageBox.Show("Invalid argument. If specified, argument must be one of: -RunAsService, -RunAsApplication or -RunAsConsole.");
                    Environment.Exit(1);
                }
            }

            if (runAsService)
            {
                // Run as Windows Service.
                ServiceBase.Run(new ServiceBase[] { host });
            }
            else if (runAsApplication)
            {
                // Run as Windows Application.
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new DebugHost(host));
            }
            else
            {
                string shellHostedServiceName           = host.ServiceName + "Shell.exe";
                string shellHostedServiceFileName       = FilePath.GetAbsolutePath(shellHostedServiceName);
                string shellHostedServiceConfigFileName = FilePath.GetAbsolutePath(shellHostedServiceName + ".config");
                string serviceConfigFileName            = FilePath.GetAbsolutePath(host.ServiceName + ".exe.config");

                try
                {
                    File.Copy(serviceConfigFileName, shellHostedServiceConfigFileName, true);
#if MONO
                    Process hostedServiceSession = Process.Start("mono", shellHostedServiceFileName);
#else
                    Process hostedServiceSession = Process.Start(shellHostedServiceFileName);
#endif
                    if ((object)hostedServiceSession != null)
                    {
                        hostedServiceSession.WaitForExit();

                        try
                        {
                            File.Copy(shellHostedServiceConfigFileName, serviceConfigFileName, true);
                        }
                        catch
                        {
                            // Do not report exception if config file could not be updated
                        }

                        Environment.Exit(hostedServiceSession.ExitCode);
                    }
                    else
                    {
                        MessageBox.Show(string.Format("Failed to start \"{0}\" as a shell hosted service.", host.ServiceName));
                        Environment.Exit(1);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(string.Format("Failed to start \"{0}\" as a shell hosted service: {1}", host.ServiceName, ex.Message));
                    Environment.Exit(1);
                }
            }
        }
Exemple #2
0
 public DebugHost(ServiceHost host)
 {
     this.ServiceHost = host;
 }
Exemple #3
0
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            ServiceHost host = new ServiceHost();

            bool runAsService;
            bool runAsApplication;

            Arguments args = new Arguments(Environment.CommandLine, true);

            if (args.Count == 0)
            {
            #if DEBUG
                runAsService = false;
                runAsApplication = true;
            #else
                runAsService = true;
                runAsApplication = false;
            #endif
            }
            else
            {
                runAsService = args.Exists("RunAsService");
                runAsApplication = args.Exists("RunAsApplication");

                if (!runAsService && !runAsApplication && !args.Exists("RunAsConsole"))
                {
                    MessageBox.Show("Invalid argument. If specified, argument must be one of: -RunAsService, -RunAsApplication or -RunAsConsole.");
                    Environment.Exit(1);
                }
            }

            if (runAsService)
            {
                // Run as Windows Service.
                ServiceBase.Run(new ServiceBase[] { host });
            }
            else if (runAsApplication)
            {
                // Run as Windows Application.
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new DebugHost(host));
            }
            else
            {
                string shellHostedServiceName = host.ServiceName + "Shell.exe";
                string shellHostedServiceFileName = FilePath.GetAbsolutePath(shellHostedServiceName);
                string shellHostedServiceConfigFileName = FilePath.GetAbsolutePath(shellHostedServiceName + ".config");
                string serviceConfigFileName = FilePath.GetAbsolutePath(host.ServiceName + ".exe.config");

                try
                {
                    File.Copy(serviceConfigFileName, shellHostedServiceConfigFileName, true);
            #if MONO
                    Process hostedServiceSession = Process.Start("mono", shellHostedServiceFileName);
            #else
                    Process hostedServiceSession = Process.Start(shellHostedServiceFileName);
            #endif
                    if ((object)hostedServiceSession != null)
                    {
                        hostedServiceSession.WaitForExit();

                        try
                        {
                            File.Copy(shellHostedServiceConfigFileName, serviceConfigFileName, true);
                        }
                        catch
                        {
                            // Do not report exception if config file could not be updated
                        }

                        Environment.Exit(hostedServiceSession.ExitCode);
                    }
                    else
                    {
                        MessageBox.Show(string.Format("Failed to start \"{0}\" as a shell hosted service.", host.ServiceName));
                        Environment.Exit(1);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(string.Format("Failed to start \"{0}\" as a shell hosted service: {1}", host.ServiceName, ex.Message));
                    Environment.Exit(1);
                }
            }
        }
Exemple #4
0
 public DebugHost(ServiceHost host)
 {
     this.ServiceHost = host;
 }
 public DebugHost(ServiceHost host)
 {
     this.ServiceHost = host;
     InitializeComponent();
 }