Nancy and / or TinyIoC do not respect AsSingleton ()

Setup: I have an almost ready installation of Nancy + TinyIoC, in which webservice works, which works fine. It depends on the different classes of service (AsSingleton). However, they are not entered as singletones, each time a new instance is created.

I installed the Nancy bootloader as follows:

class MyBootStrapper : DefaultNancyBootstrapper { protected override void ConfigureApplicationContainer(TinyIoCContainer container) { var cp = new CertificateProvider(); container.Register(cp).AsSingleton(); } } 
+4
source share
2 answers

Are you sure your bootloader is being used? It is not publicly available, so itโ€™s quite possible to use the built-in in it, where the default convention is a multiple instance for non-interface dependencies.

As with Daniel, you also don't need AsSingleton, if you are registering an instance, you can also simply:

 container.Register<CertificateProvider>().AsSingleton(); 

Therefore, it is created only when necessary.

+5
source

In your code, even if you delete AsSingleton() , you will still have a singleton, because you are not registering a type or factory, but an instance. TinyIoC cannot create new CertificateProvider instances with this registration.

The only possible thing I can think of is that the bootstrapper itself is executed several times, but this is a completely different problem and has nothing to do with your registration.

+5
source

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


All Articles