question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Opt-in to captive dependency detection on a registration

See original GitHub issue

My company is using Autofac extensively and really loving it.

That being said, one of the most difficult to debug issue we have run into with IoC containers is captive dependencies.

I understand that captive dependencies are complex, and there are cases where captive dependencies are acceptable. However, we have cases where a component absolutely must not become a captive dependency (becomes its behaviour would be broken). We would like to be able to annotate these components so it is impossible for us to accidentally widen their lifetime scope without being notified by an error message.

I am imagining something like a LifetimeScopeCannotBeWidened method that can be called when registering. The intention would be that, when this setting is applied to a registration, Autofac would throw an exception rather than inject the component into another component with a wider lifetime scope.

builder
  .RegisterType<Component>()
  .As<IService>()
  .InstancePerLifetimeScope()
  .LifetimeScopeCannotBeWidened();

Does that make sense? Do you think it would it be possible for us to extend Autofac to provide this functionality, or would this need to be built into Autofac?

Thanks for your help!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
bentefaycommented, Feb 27, 2019

Hey @TylerReid, unfortunately I never found the time to investigate further, but a colleague recently pointed out that, at least for us, we could potentially use (or write) a library like the autofac-analysis project to detect issues like captive depedencies with our app as part of our integration tests. If you’re happy detecting issues at test-time, this could present a viable alternative that has the added benefit of not impacting the performance of the app.

1reaction
tilligcommented, Feb 26, 2019

Any sort of analysis like this will be a challenge to implement. Happy to see any PR, but if we’re going to support it, it really needs to be something folks can rely on if they use it. If it only handles a subset of cases, we will absolutely get more issues asking why it isn’t a 100% functional feature and it may become more maintenance burden than it’s worth.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Captive Dependency - ploeh blog
A Captive Dependency is a dependency with an incorrectly configured lifetime. It's a typical and dangerous DI Container configuration error.
Read more >
Dependency injection guidelines - .NET
This article provides general guidelines and best practices for implementing dependency injection in .NET applications.
Read more >
Is it bad to have a captive dependency on DbContext ...
Here is another post from Autofac documentation. They suggest using captive dependencies only by purpose (when you know what you are doing!).
Read more >
Captive Dependency with ASP.NET Core | by Ashish Patel
Captive Dependency is a dependency with an incorrectly configured lifetime. A service should never depend on a service that has a shorter lifetime...
Read more >
The dangers and gotchas of using scoped services in ...
That means that the service, and any dependencies, are resolved from the root container, triggering the captive dependency detection.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found