Program.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using CustomHostingDemo.Logging;
  2. using Microsoft.Extensions.Configuration;
  3. using Microsoft.Extensions.Logging;
  4. using Ninject;
  5. using Ninject.Syntax;
  6. using System;
  7. using System.CommandLine;
  8. using System.CommandLine.Binding;
  9. using System.CommandLine.Builder;
  10. using System.CommandLine.Parsing;
  11. using System.IO;
  12. using System.Reflection;
  13. namespace CustomHostingDemo
  14. {
  15. class Program
  16. {
  17. static void Main(string[] args)
  18. {
  19. var kernel = new StandardKernel();
  20. var config = new ConfigurationBuilder()
  21. .SetBasePath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location))
  22. .AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
  23. .AddJsonFile(Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "custom-demo.json"), optional: true, reloadOnChange: false)
  24. .Build();
  25. kernel.Bind<IConfiguration>().ToConstant(config).InSingletonScope();
  26. kernel.Bind<ILoggerFactory>().To<LoggerFactory>().InSingletonScope();
  27. kernel.Bind(typeof(Logger<>)).ToSelf().InSingletonScope();
  28. kernel.Bind<ILoggerProvider>().To<LoggerProvider>().InSingletonScope();
  29. Console.WriteLine($"Hello {config["Hello"]}");
  30. var logger = kernel.Get<Logger<Program>>();
  31. logger.LogInformation("Informational Message");
  32. logger.LogWarning("Ooopsie!");
  33. var rootCommand = new RootCommand()
  34. {
  35. new HelloCommand(),
  36. };
  37. var parser = new CommandLineBuilder(rootCommand)
  38. .UseMiddleware((invocationContext) =>
  39. {
  40. //invocationContext.BindingContext.ParseResult.
  41. //invocationContext.BindingContext.AddService(typeof(IResolutionRoot), () => kernel);
  42. invocationContext.BindingContext.AddService(typeof(HelloCommand.DefaultHandler), () =>
  43. {
  44. var handler = kernel.Get<HelloCommand.DefaultHandler>();
  45. var binder = new ModelBinder<HelloCommand.DefaultHandler>();
  46. binder.UpdateInstance(handler, invocationContext.BindingContext);
  47. return handler;
  48. });
  49. //invocationContext.BindingContext.AddService(typeof(HelloCommand), () => {
  50. // return kernel.Get<HelloCommand>();
  51. //});
  52. })
  53. .Build();
  54. parser.InvokeAsync(args);
  55. //rootCommand.Invoke(args);
  56. }
  57. }
  58. }