| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- using CustomHostingDemo.Logging;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.Logging;
- using Ninject;
- using Ninject.Syntax;
- using System;
- using System.CommandLine;
- using System.CommandLine.Binding;
- using System.CommandLine.Builder;
- using System.CommandLine.Parsing;
- using System.IO;
- using System.Reflection;
- namespace CustomHostingDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- var kernel = new StandardKernel();
- var config = new ConfigurationBuilder()
- .SetBasePath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location))
- .AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
- .AddJsonFile(Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "custom-demo.json"), optional: true, reloadOnChange: false)
- .Build();
- kernel.Bind<IConfiguration>().ToConstant(config).InSingletonScope();
- kernel.Bind<ILoggerFactory>().To<LoggerFactory>().InSingletonScope();
- kernel.Bind(typeof(Logger<>)).ToSelf().InSingletonScope();
- kernel.Bind<ILoggerProvider>().To<LoggerProvider>().InSingletonScope();
- Console.WriteLine($"Hello {config["Hello"]}");
- var logger = kernel.Get<Logger<Program>>();
- logger.LogInformation("Informational Message");
- logger.LogWarning("Ooopsie!");
- var rootCommand = new RootCommand()
- {
- new HelloCommand(),
- };
- var parser = new CommandLineBuilder(rootCommand)
- .UseMiddleware((invocationContext) =>
- {
- //invocationContext.BindingContext.ParseResult.
- //invocationContext.BindingContext.AddService(typeof(IResolutionRoot), () => kernel);
- invocationContext.BindingContext.AddService(typeof(HelloCommand.DefaultHandler), () =>
- {
- var handler = kernel.Get<HelloCommand.DefaultHandler>();
- var binder = new ModelBinder<HelloCommand.DefaultHandler>();
- binder.UpdateInstance(handler, invocationContext.BindingContext);
- return handler;
- });
- //invocationContext.BindingContext.AddService(typeof(HelloCommand), () => {
- // return kernel.Get<HelloCommand>();
- //});
- })
- .Build();
- parser.InvokeAsync(args);
- //rootCommand.Invoke(args);
- }
- }
- }
|