Ninject Transient Scope with Dispose

Michael

I have a console app that uses kernel.Get<SomeClass>(); However, SomeClass has a dependency on SomeDisposableClass. How can I set up my binding to dispose of SomeDisposableClass when SomeClass is garbage collected? My MVC app uses InRequestScope and that works great, but there doesn't seem to be an analogous scope for console apps.

Example here:

public class SomeClass {
    public SomeClass(SomeDisposableClass c) {
        this.C = c;
    }

    private SomeDisposableClass C { get; set; }

    // ... Business Methods ... //
}

My module

kernel.Bind<ISomeClass>().To<SomeClass>().In???Scope()

My console app

public static void Main() {
    SomeFunc();
    SomeFunc();
    Console.ReadLine();
}

public static void SomeFunc() {
    ISomeClass someClass = kernel.Get<ISomeClass>();
    // work
}

I'd like for SomeDisposableClass to be disposed when SomeFunc is finished (or when the garbage collector is called). But I'm not sure of which binding scope to use. InTransientScope doesn't ever call dispose. Do I just have to make SomeClass disposable and implement Dispose() and wrap all my usages in the console app with a using statement?

qujck

In Ninject2, you can do this by:

Bind<IService>().To<ServiceImpl>().InScope(ctx => ...);

For example, the callback used for InRequestScope() is:

ctx => HttpContext.Current

Since HttpContext.Current is set to a new instance of HttpContext on each web request, only a single instance of the service will be activated for each request, and when the request ends and the HttpContext is (eventually) collected, the instances will be deactivated.

You can have a static variable within your console to reference an object that will control lifetime.

public static object LifetimeController = new object();

You can register this as your lifetime control object

Bind<IService>().To<ServiceImpl>().InScope(ctx => LifetimeController);

And each time you want to refresh the objects you can have a method like this

public static void StartNewLifetime()
{
    LifetimeController = new object();
}

See here and here for more information

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can't get Ninject to dispose object in Request Scope

From Dev

Console app with MVC, Ninject and WCF Service (Dispose issue?)

From Dev

Ninject Owin Request Scope Silently Fails

From Dev

How to change the scope of an existing binding in Ninject

From Dev

Set Scope for Ninject Modules in Onion Architecture

From Dev

How to change the scope of an existing binding in Ninject

From Dev

In Ninject, is this where I would use a child scope?

From Dev

Do I need to call .Dispose() on both of these XmlDataSource objects? "CA2000 Dispose objects before losing scope" tells me to .Dispose() it

From Dev

Will Ninject Dispose of an Object that doesn't have IDisposable on it's Interface, but does on it's implementation?

From Dev

How to configure Ninject in call scope binding within Azure Web Job?

From Dev

C# CA2000 Dispose Object Before Losing Scope

From Dev

Ninject Conditional Self bind to change scope (For Task-scheduler) not working properly?

From Dev

Can't figure out why Ninject Named Scope isnt working as expected

From Dev

Can't figure out why Ninject Named Scope isnt working as expected

From Dev

How to dispose/release/"finalize" unmanaged resources when a shared value gets out of scope

From Dev

"CA2000: Dispose object before losing scope" building Unity container

From Dev

"CA2000: Dispose object before losing scope" building Unity container

From Dev

Hangfire with Ninject

From Dev

Catel with Ninject

From Dev

RepositoryFactory with Ninject

From Dev

Hangfire with Ninject

From Dev

CacheManager and Dispose

From Dev

CacheManager and Dispose

From Dev

Dispose FileInfo()

From Dev

Dispose will dispose objects that are still referenced?

From Dev

Not in scope: <*>

From Dev

Not in scope: <*>

From Java

What are transient dependencies?

From Dev

Java Externalization vs Transient

Related Related

  1. 1

    Can't get Ninject to dispose object in Request Scope

  2. 2

    Console app with MVC, Ninject and WCF Service (Dispose issue?)

  3. 3

    Ninject Owin Request Scope Silently Fails

  4. 4

    How to change the scope of an existing binding in Ninject

  5. 5

    Set Scope for Ninject Modules in Onion Architecture

  6. 6

    How to change the scope of an existing binding in Ninject

  7. 7

    In Ninject, is this where I would use a child scope?

  8. 8

    Do I need to call .Dispose() on both of these XmlDataSource objects? "CA2000 Dispose objects before losing scope" tells me to .Dispose() it

  9. 9

    Will Ninject Dispose of an Object that doesn't have IDisposable on it's Interface, but does on it's implementation?

  10. 10

    How to configure Ninject in call scope binding within Azure Web Job?

  11. 11

    C# CA2000 Dispose Object Before Losing Scope

  12. 12

    Ninject Conditional Self bind to change scope (For Task-scheduler) not working properly?

  13. 13

    Can't figure out why Ninject Named Scope isnt working as expected

  14. 14

    Can't figure out why Ninject Named Scope isnt working as expected

  15. 15

    How to dispose/release/"finalize" unmanaged resources when a shared value gets out of scope

  16. 16

    "CA2000: Dispose object before losing scope" building Unity container

  17. 17

    "CA2000: Dispose object before losing scope" building Unity container

  18. 18

    Hangfire with Ninject

  19. 19

    Catel with Ninject

  20. 20

    RepositoryFactory with Ninject

  21. 21

    Hangfire with Ninject

  22. 22

    CacheManager and Dispose

  23. 23

    CacheManager and Dispose

  24. 24

    Dispose FileInfo()

  25. 25

    Dispose will dispose objects that are still referenced?

  26. 26

    Not in scope: <*>

  27. 27

    Not in scope: <*>

  28. 28

    What are transient dependencies?

  29. 29

    Java Externalization vs Transient

HotTag

Archive