Thanks to visit codestin.com
Credit goes to github.com

Skip to content

StructureMap cann't resolve dependencies when using child container. #618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
zordark opened this issue Oct 31, 2017 · 5 comments
Closed

Comments

@zordark
Copy link

zordark commented Oct 31, 2017

Hello, I use StructureMap as replacement of standard container in asp.net core and get strange behavior when use child containers.
I manually instantiate Startup class and pass pre-configured container to it.
My Startup.cs looks like this:

  public class Startup : IStartup {
        private IContainer _container;
        public Startup(IConfiguration configuration, IContainer container)
        {
            Configuration = configuration;
            _container = container;
       }

        public IConfiguration Configuration { get; }

        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            // Register additional stuff
            // .....

            // populate the container using the service collection.
            _container.Populate(services);

            return _container.GetInstance<IServiceProvider>();
        }
        public void Configure(IApplicationBuilder app)
        {
             //do additional work 
        }
    }

And build host like this:

            var container = new Container();

            var configurationBuilder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json");

            var configuration = configurationBuilder.Build();

            var startup = new Startup(configuration, container);

            var builder = WebHost.CreateDefaultBuilder()
                .UseSetting(WebHostDefaults.ApplicationKey, typeof(Startup).Assembly.GetName().Name)
                .ConfigureServices(services => services.AddSingleton<IStartup>(startup))
                .UseKestrel(options =>
                {
                    options.Listen(IPAddress.Loopback, 1113);
                });

            var host = builder.Build();

It's work perfectly, until I start to use child container for my Strartup class.
Following code will fail:

            var container = new Container();

            var configurationBuilder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json");

            var configuration = configurationBuilder.Build();

            var startup = new Startup(configuration,  container.CreateChildContainer());

            var builder = WebHost.CreateDefaultBuilder()
                .UseSetting(WebHostDefaults.ApplicationKey, typeof(Startup).Assembly.GetName().Name)
                .ConfigureServices(services => services.AddSingleton<IStartup>(startup))
                .UseKestrel(options =>
                {
                    options.Listen(IPAddress.Loopback, 1113);
                });

            var host = builder.Build();

with exception: StructureMap.StructureMapConfigurationException : No default Instance is registered and cannot be automatically determined for type 'IOptions<KestrelServerOptions>'
It's happens inside WebHost class, when it try to resolve IServer instance.

Why is it happens?

@jeremydmiller
Copy link
Contributor

Meh, this has come up a couple times now. The policies that do the automatic closing of open generic candidates only apply in the root container. Your use case isn't something I ever anticipated folks doing.

You could cheat this by first resolving that service in the root container before using the child container. First though, what's the point in using a child container here?

@zordark
Copy link
Author

zordark commented Oct 31, 2017

It's some kind of webhost orchestration system.
Application manage many IWebHost instances, and I can't share the same container between it, because each host have they own configuration of services.
But I have some common configuration for all hosts.
Using child containers exactly what I need, according documentation.
At now, I use workaround: I create individual container for each host and initialize it with common configuration. But it's not best solution. And I can't use solution you provide, because I don't know exactly, what each host registered in child container. Application just offer infrastructure for each host.

@dazinator
Copy link

Wondering why this is closed? Please see structuremap/StructureMap.Microsoft.DependencyInjection#43 for related issue. Is this closed because it's not considered a bug? I'm not really sure what to do to overcome this issue so I have had to switch to autofac where IOptions and child containers works as expected.

@jeremydmiller
Copy link
Contributor

@dazinator StructureMap is no longer actively supported

@dazinator
Copy link

@jeremydmiller Oh right - apologies I hadn't realised. I'll take a look at Lamar, and I'll probably stick with autofac for now. Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants