Nancy creates a singleton with constructor options

I am using Nancy with TinyIoC to solve dependencies.

One of the dependencies, in particular, should be single for the application-life cycle.

If I do this with a standard constructor, it works:

container.Register<IFoo, Foo>().AsSingleton();   // WORKS

but if I try this with some arguments on the constructor, it is not:

container.Register<IFoo>((c, e) => new Foo("value", c.Resolve<ILogger>())).AsSingleton();
// FAILS with error "Cannot convert current registration of Nancy.TinyIoc.TinyIoCContainer+DelegateFactory to singleton"

Without .AsSingleton(), it works again, but I don't get singleton:

container.Register<IFoo>((c, e) => new Foo("value", c.Resolve<ILogger>()));
// Works, but Foo is not singleton

Any ideas? I think the error should be obvious, but I cannot find it. I used all my google-foo.


EDIT

The code works here:

public class Bootstrapper : DefaultNancyBootstrapper
{
    protected override void ConfigureApplicationContainer(TinyIoCContainer container)
    {
        base.ConfigureApplicationContainer(container);

        // here 
    }
}
+4
source share
1 answer

, , TinyIOC " , , ", , , singleton.

, :

container.Register<IFoo>(new Foo("value", c.Resolve<ILogger>()));

, IFoo.

+7

Source: https://habr.com/ru/post/1623306/


All Articles