Program.cs 2.5 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 int 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().Bind(kernel),
  36. };
  37. var parser = new CommandLineBuilder(rootCommand)
  38. //.UseMiddleware((invocationContext) =>
  39. //{
  40. // var cmdArgs = new HelloCommand.HelloArgs();
  41. // var binder = new ModelBinder<HelloCommand.HelloArgs>();
  42. // binder.UpdateInstance(cmdArgs, invocationContext.BindingContext);
  43. // kernel.Bind<HelloCommand.HelloArgs>().ToConstant(cmdArgs).InSingletonScope();
  44. // //invocationContext.BindingContext.AddService(typeof(HelloCommand.HelloArgs), () => cmdArgs);
  45. // invocationContext.BindingContext.AddService(typeof(HelloCommand.DefaultHandler), () =>
  46. // {
  47. // var handler = kernel.Get<HelloCommand.DefaultHandler>();
  48. // return handler;
  49. // });
  50. //})
  51. .UseDefaults()
  52. .Build();
  53. return parser.InvokeAsync(args).Result;
  54. }
  55. }
  56. }