private static void Main(string[] args) { bool debugMode = false; try { const int argumentCount = 4; RunnerMode runnerMode; int parentPid; CommunicationProtocol protocol; string[] assemblyPaths; if (args.Length < argumentCount || !Enum.TryParse(args[0], true, out runnerMode) || !int.TryParse(args[1], out parentPid) || !Enum.TryParse(args[2], true, out protocol) || !bool.TryParse(args[3], out debugMode) || !args.Skip(argumentCount).All(p => File.Exists(p))) { throw new Exception("Arguments are invalid."); } assemblyPaths = args.Skip(argumentCount).ToArray(); if (debugMode) { AppDomain.CurrentDomain.FirstChanceException += (o, e) => Console.WriteLine(e.Exception.GetDescription().PadLinesLeft(" ")); } RunTestService(runnerMode, parentPid, protocol, assemblyPaths); } catch (Exception e) { if (debugMode) { if (Debugger.IsAttached) { Debugger.Break(); } else { Debugger.Launch(); } } var crashFilePath = Path.GetTempFileName(); var crashDetails = JsonConvert.SerializeObject(new SerializableException(e)); File.WriteAllText(crashFilePath, crashDetails); ServiceProcess.PrintServiceFailed(crashFilePath); } }
private static void Main(string[] args) { try { #if DEBUG AppDomain.CurrentDomain.FirstChanceException += (o, e) => Console.WriteLine(e.Exception.GetDescription().PadLinesLeft(" ")); #endif RunnerMode runnerMode; int parentPid; if (args.Length < 2 || !Enum.TryParse(args[0], true, out runnerMode) || !int.TryParse(args[1], out parentPid) || !args.Skip(2).All(p => File.Exists(p))) { throw new Exception("Arguments are invalid."); } AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve; foreach (var assemblyPath in args.Skip(2)) { Console.Write($"Loading {assemblyPath}... "); Assembly.LoadFrom(assemblyPath); Console.WriteLine("Done."); } Process parentProcess = null; try { parentProcess = Process.GetProcessById(parentPid); parentProcess.EnableRaisingEvents = true; parentProcess.Exited += OnParentProcessExited; } catch (Exception e) { throw new Exception("Cannot open parent process.", e); } Type serviceInterface; Type serviceImplementation; GetService(runnerMode, out serviceInterface, out serviceImplementation); Console.WriteLine("AxoCover.Runner"); Console.WriteLine("Copyright (c) 2016-2017 Péter Major"); Console.WriteLine(); Console.WriteLine($"Starting {args[0]} service..."); var serviceAddress = NetworkingExtensions.GetServiceAddress(); var serviceBinding = NetworkingExtensions.GetServiceBinding(); var serviceHost = new ServiceHost(serviceImplementation, new[] { serviceAddress }); serviceHost.AddServiceEndpoint(serviceInterface, serviceBinding, serviceAddress); serviceHost.Open(); ServiceProcess.PrintServiceStarted(serviceAddress); _isFinished.WaitOne(); Console.WriteLine("Exiting..."); try { serviceHost.Close(_closeTimeout); } catch { } //Make sure to kill leftover non-background threads started by tests Environment.Exit(0); } catch (Exception e) { #if DEBUG Debugger.Launch(); #endif var crashFilePath = Path.GetTempFileName(); var crashDetails = JsonConvert.SerializeObject(new SerializableException(e)); File.WriteAllText(crashFilePath, crashDetails); ServiceProcess.PrintServiceFailed(crashFilePath); } }