private void Main(string[] args)
        {
            var folder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            var targetExe = Directory.EnumerateFiles(folder)
                            .FirstOrDefault(x => x.EndsWith(".exe") && x.ToLower() != Assembly.GetEntryAssembly().Location.ToLower() && !Path.GetFileName(x).Contains("EasyHook"));

            if (targetExe == null)
            {
                Console.Error.Write("Target executable not found");
                return;
            }

            var context   = new InjectorContext(args, HarmonyInstance.Create("bootstrapper"));
            var injectors = new Injector[]
            {
                new ScimControllerInjector(context),
                new EventLogInjector(context)
            };

            foreach (var injector in injectors)
            {
                injector.Install();
            }

            Console.WriteLine("Injectors applied");

            var serviceAsm = Assembly.LoadFile(targetExe);
            var entryPoint = serviceAsm.EntryPoint;

            Console.WriteLine("Starting service Main method...");
//            entryPoint.Invoke(null, new[] {new string[0]});
            if (entryPoint.GetParameters().Any())
            {
                Task.Run(() => entryPoint.Invoke(null, new[] { new string[0] }));
            }
            else
            {
                Task.Run(() => entryPoint.Invoke(null, null));
            }
            Console.WriteLine("Press CTRL+C to Shutdown...");
            ApplicationLifecycle.ShutdownCompleteHandle.WaitOne();
        }
        public void Run(string[] args)
        {
            var    folder    = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string targetExe = Environment.GetEnvironmentVariable("WINDOWS_SERVICE_EXE");

            if (targetExe == null)
            {
                targetExe = Directory.EnumerateFiles(folder)
                            .FirstOrDefault(x =>
                                            x.EndsWith(".exe") &&
                                            // x.ToLower() != Assembly.GetEntryAssembly().Location.ToLower() &&
                                            !Path.GetFileName(x).Contains("EasyHook") &&
                                            !Path.GetFileName(x).ToLower().Equals("buildpack.exe") &&
                                            !Path.GetFileName(x).ToLower().Equals("detect.exe") &&
                                            !Path.GetFileName(x).ToLower().Equals("supply.exe") &&
                                            !Path.GetFileName(x).ToLower().Equals("finalize.exe") &&
                                            !Path.GetFileName(x).ToLower().Equals("release.exe") &&
                                            !Path.GetFileName(x).ToLower().Equals("launch.exe")
                                            );
            }
            else
            {
                if (!Path.IsPathRooted(targetExe))
                {
                    targetExe = Path.Combine(Directory.GetCurrentDirectory(), targetExe);
                }
            }
            Console.WriteLine($"Identified {targetExe} as the service entry point executable");
            if (targetExe == null || !File.Exists(targetExe))
            {
                Console.Error.Write("Target executable not found");
                return;
            }
            var context   = new InjectorContext(args, new Harmony("bootstrapper"));
            var injectors = new Injector[]
            {
                new ScimControllerInjector(context),
                new EventLogInjector(context)
            };

            foreach (var injector in injectors)
            {
                injector.Install();
            }

            Console.WriteLine("Injectors applied");

            var serviceAsm = Assembly.LoadFile(targetExe);
            var entryPoint = serviceAsm.EntryPoint;

            Console.WriteLine("Starting service Main method...");
            if (entryPoint.GetParameters().Any())
            {
                Task.Run(() => entryPoint.Invoke(null, new[] { new string[0] }));
            }
            else
            {
                Task.Run(() => entryPoint.Invoke(null, null));
            }
            Console.WriteLine("Press CTRL+C to Shutdown...");
            // if (Environment.GetEnvironmentVariable("IsSubjectUnderTest") != null)
            // {
            //     ApplicationLifecycle.Shutdown();
            // }
            ApplicationLifecycle.ShutdownCompleteHandle.WaitOne();
        }
 public EventLogInjector(InjectorContext context)
     : base(context)
 {
 }
 protected Injector(InjectorContext context)
 {
     Context = context;
 }
Exemple #5
0
 public ScimControllerInjector(InjectorContext context) :
     base(context)
 {
     Args = context.Args;
 }