Tyrrrz / CliFx

Class-first framework for building command-line interfaces
MIT License
1.5k stars 61 forks source link

After upgrading from 0.0.8 to 1.0.0, Dependency injection management using Autofac failed #43

Closed xknife-erian closed 4 years ago

xknife-erian commented 4 years ago

using Autofac;

            var builder = new ContainerBuilder();
            builder.RegisterAssemblyModules(typeof(Logic.Global).Assembly);

            builder.RegisterType<SerialCliCommand>().Named<ICommand>("serial").SingleInstance();
            builder.RegisterType<CareConfigCliCommand>().Named<ICommand>("cc").SingleInstance();
            builder.RegisterType<CareCliCommand>().Named<ICommand>("ci").SingleInstance();
            builder.RegisterType<DataCommand>().Named<ICommand>("d").SingleInstance();

            var container = builder.Build();

            await new CliApplicationBuilder()
                .AddCommandsFromThisAssembly()
                .UseTypeActivator(type => container.ResolveNamed<ICommand>(type.Name))
                .Build()
                .RunAsync(args);
Tyrrrz commented 4 years ago

Hi. By "failed", what do you mean? What exactly happens?

Tyrrrz commented 4 years ago

I think you are resolving a named instance. Previously UseCommandFactory provided a CommandSchema which had a command name, but now it's just a Type. You need to resolve the command by type, not by name.

xknife-erian commented 4 years ago

thank you I come from a non-english speaking country and may not express clearly. I mean, it was normal to use Autofac, but I want to be able to use it after the upgrade, not report errors. According to your reply, I have fixed the problem.Thank you very much. Your project is also a very well designed project, which I have used in many of my projects.

Tyrrrz commented 4 years ago

Don't worry 🙂 I'm glad you've managed to fix the problem and also glad that you like the project!