Program.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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.Invocation;
  11. using System.CommandLine.Parsing;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Reflection;
  15. namespace CustomHostingDemo
  16. {
  17. class Program
  18. {
  19. static int Main(string[] args)
  20. {
  21. var kernel = new StandardKernel();
  22. var config = new ConfigurationBuilder()
  23. .SetBasePath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location))
  24. .AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
  25. .AddJsonFile(Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "custom-demo.json"), optional: true, reloadOnChange: false)
  26. .Build();
  27. kernel.Bind<IConfiguration>().ToConstant(config).InSingletonScope();
  28. kernel.Bind<ILoggerFactory>().To<LoggerFactory>().InSingletonScope();
  29. kernel.Bind(typeof(Logger<>)).ToSelf().InSingletonScope();
  30. kernel.Bind<ILoggerProvider>().To<LoggerProvider>().InSingletonScope();
  31. kernel.Bind<ILog>().To<Log>().InTransientScope();
  32. kernel.Bind<InvocationContext>().ToMethod(ctx =>
  33. {
  34. var parameter = ctx.Parameters.FirstOrDefault(p => p.Name == nameof(InvocationContext));
  35. if (parameter == null)
  36. {
  37. throw new Exception("Cannot resolve InvocationContext outside of an actual NinjectCommandHandler invocation");
  38. }
  39. return (InvocationContext)parameter.GetValue(ctx, null);
  40. });
  41. Console.WriteLine($"Hello {config["Hello"]}");
  42. var logger = kernel.Get<Logger<Program>>();
  43. logger.LogInformation("Informational Message");
  44. logger.LogWarning("Ooopsie!");
  45. var rootCommand = new RootCommand()
  46. {
  47. new HelloCommand().Bind(kernel),
  48. };
  49. var parser = new CommandLineBuilder(rootCommand)
  50. //.UseMiddleware((invocationContext) =>
  51. //{
  52. // var cmdArgs = new HelloCommand.HelloArgs();
  53. // var binder = new ModelBinder<HelloCommand.HelloArgs>();
  54. // binder.UpdateInstance(cmdArgs, invocationContext.BindingContext);
  55. // kernel.Bind<HelloCommand.HelloArgs>().ToConstant(cmdArgs).InSingletonScope();
  56. // //invocationContext.BindingContext.AddService(typeof(HelloCommand.HelloArgs), () => cmdArgs);
  57. // invocationContext.BindingContext.AddService(typeof(HelloCommand.DefaultHandler), () =>
  58. // {
  59. // var handler = kernel.Get<HelloCommand.DefaultHandler>();
  60. // return handler;
  61. // });
  62. //})
  63. .UseDefaults()
  64. .Build();
  65. return parser.InvokeAsync(args).Result;
  66. }
  67. }
  68. }