-
-
Notifications
You must be signed in to change notification settings - Fork 840
Nested ContainerBuilder created with BeginLifetimeScope shares BuildCallback list with its parent ContainerBuilder #912
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
Comments
This seems... complicated. What's the problem you're trying to solve by doing this nested series of callbacks? |
The outer builder comes from asp.net app. The module wants to build a complex component. For this it needs some services from user code that have to be resolved. But we can't resolve them in ContainerBuilder. So we have to create a new LifetimeScope. But ContainerBuilder doesn't have BeginLifetimeScope so we're postponing that via RegisterBuildCallback. In real case code looks a bit more complex, I just simplified it :) So actually instead of that nested RegisterBuildCallback I'm using some builder/factory stuff to build my component. containerBuilder.RegisterBuildCallback(container =>
{
container.TryResolve(out IXApplicationFactory appFactory);
m_appScope = container.BeginLifetimeScope(nestedBuilder =>
{
var appBuilder = new XApplicationBuilder(nestedBuilder);
appFactory.Build(appBuilder);
});
} That XApplicationBuilder doesn't know whether supplied ContainerBuidler is nested or not. It's just a ContainerBuilder for him and he wants to know when that ContainerBuilder is built. |
We can definitely look into this but from a general standpoint I'd recommend separating your app startup code - the creation of an application level scope and the execution of your app factory stuff - from the population and building of the container. I can see how it might be easy to try tying it all together but I can also see high potential for getting into challenging and complicated situations like this. Like I said, though, we will look into it. Thanks for the report! |
PR accepted so we won't regress this. |
Inside MyModule I create a nested scope and register a BuildCallback for the nested container builder:
But it seems that these two builders (the outer and the nested) share the same callbacks list. As I'm gettting an exception "Collection was modified; enumeration operation may not execute" on
Build()
call for the outer builder:Autofac 2.8.0
The text was updated successfully, but these errors were encountered: