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.

Allow controlling of Invocation lifetime/scope

See original GitHub issue

I’m using the SDK with Autofac dependency injection, and a very important scenario is to control lifetime scope of dependency injection and disposal when invoking a new function.

I used the IJobActivator interface to plug-in my Autofac container, however I cannot activate my types within a lifetime scope because there are no callbacks to dispose of the lifetime when the function invocation is finished (succeeded or not).

What I’m doing now is this:

        public T CreateInstance<T>()
        {
            var scope = _container.BeginLifetimeScope();
            return scope.Resolve<T>();
        }

The code above is bad because the lifetimescope is IDisposable, and will not be disposed automatically for me, and there is no way for me to determine when the activated instance has been disposed in order to dispose my lifetime scope.

I propose adding an ActivationContext parameter that is passed per activation (ideally invocation), that has the function invocation unique id. Then adding a Deactivate() method that takes the same context, to allow me to dispose of the lifetime I created earlier.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:10
  • Comments:16 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
drub0ycommented, Mar 23, 2017

So this seems to have stagnated, so I just wanted to chime in and say that I started down this path and found IJobActivator and was like 😀, but then quickly realized there was no ability to control scope and was like 😢.

I spent a little time looking at the SDK current design in hopes of offering some advice on how to maybe offer scoping, but unfortunately I’m not quite sure how to get out of this corner it’s current painted into right now. If backwards compatibility is a concern then the best option may actually be leaving IJobActivator the way it is, maybe even obsoleting it at some point, and instead revisiting the solution to DI from the top and offering it as a sibling option to the job activator approach. Then the upstream runtime logic could basically check if this “new” DI approach was configured and, if so, it would just ignore any existing activator concepts (maybe even throw and exception if both are used) and prefer the new DI approach instead.

4reactions
BishoyDemiancommented, May 17, 2016

Well, in theory, yes this alternative solution might work, however when the IJobActivator resolves the function class it should also resolve and inject multiple levels of dependencies that have several other lifetime/scope configurations (singletons, per invocation, per thread, etc.).

I personally don’t like my business function (or its class) to worry about or (even worse) control the scope/lifetime of its dependencies directly, as this goes against SRP. And my container already comes with lifetime management facility that I can use.

I’d be happy to undertake the work required here in a fork and submit a PR, however I wanted to kick start the discussion here first to gather as much information and feedback as possible before I start refactoring any code?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Autofac instance per lifetime scope only on child scope, not ...
1 Answer 1 · Needs to control the creation and disposal of its own DbContext instances · Is generally a singleton or otherwise...
Read more >
Controlling Scope and Lifetime - Autofac's documentation!
Lifetime scopes are nestable and they control how components are shared. For example, a “singleton” service might be resolved from a root lifetime...
Read more >
LifetimeScope error when running as a control
When I place the YAF control, I receive the following error. System.ObjectDisposedException: Instances cannot be resolved and nested lifetimes cannot be created ...
Read more >
Dependency injection guidelines - .NET
Register the instance with a scoped lifetime. Use IServiceScopeFactory.CreateScope to create a new IServiceScope. Use the scope's ...
Read more >
Dependency Injection Lifetimes in ASP.NET Core
Let's run the application and invoke the GET endpoint and make the request two times. This is important because we want to see...
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