c# - Nancy create singleton with constructor parameters -


i'm using nancy tinyioc solve dependencies.

one dependency in particular needs application-lifecycle singleton.

if default constructor works:

container.register<ifoo, foo>().assingleton();   // works 

but if try arguments on contructor not:

container.register<ifoo>((c, e) => new foo("value", c.resolve<ilogger>())).assingleton(); // fails error "cannot convert current registration of nancy.tinyioc.tinyioccontainer+delegatefactory singleton" 

whithout .assingleton(), works again, don't singleton:

container.register<ifoo>((c, e) => new foo("value", c.resolve<ilogger>())); // works, foo not singleton 

any ideas? think mistake should obvious can't find it. i've used google-foo.


edit

the code runs here:

public class bootstrapper : defaultnancybootstrapper {     protected override void configureapplicationcontainer(tinyioccontainer container)     {         base.configureapplicationcontainer(container);          // here      } } 

what you're doing there telling tinyioc "every time want 1 of these, call delegate", if want use method have handle singleton aspect yourself.

unless particularly need deferred creation it's easier do:

container.register<ifoo>(new foo("value", c.resolve<ilogger>())); 

that use instance whenever want ifoo.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -