示例#1
0
        private static void ExecuteDIPlugin()
        {
            string[] pluginPaths = new string[]
            {
                @"DIPlugin\bin\Debug\netstandard2.0\DIPlugin.dll"
            };

            ICommandFactory commandFactory = pluginPaths.SelectMany(pluginPath =>
            {
                Assembly pluginAssembly = LoadPlugin(pluginPath);
                return(CreateCommandFactories(pluginAssembly));
            }).FirstOrDefault();

            var services = new ServiceCollection();

            var builder = new ConfigurationBuilder()
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
            var configuration = builder.Build();

            services.AddSingleton <IConfiguration>(configuration);
            services.AddSingleton <IDependantService, DependantService>();

            services.AddSingleton <ICommandFactory>(serviceProvider =>
            {
                var dependantService = serviceProvider.GetRequiredService <IDependantService>();
                commandFactory.ConfigureServices(dependantService);
                return(commandFactory);
            });

            services.AddTransient <ICommand>(serviceProvider =>
            {
                var factory = serviceProvider.GetRequiredService <ICommandFactory>();
                return(factory.Create());
            });
            var serviceProvider = services.BuildServiceProvider();

            var command = serviceProvider.GetRequiredService <ICommand>();

            command.Execute();
        }