简体   繁体   中英

Ninject dependency resolution for webAPI & MVC

Motive: I am trying to have a class in singleton scope and have defined it to be singleton like:

kernel.Bind<IClass>().To<Class>().InSingletonScope();

Idea is that I need to share this between mvc and webapi.

In MVC the class gets singleton. So this work fine in MVC project.

Question : I have implemented my own IDependencyResolver for WebAPI. And it uses same kernel settings.

So I understand that we can have Class to be singleton in webapi calls too. But can I have this singleton for both webapi and mvc in common. Get is persisted for both mvc or webpi call.

Probably I am missing some concepts here. Please explain me how we can achieve this. Or is this not possible. Yes, probably the design is weird and not right, but if I have to do this for some reason.

Having both an MVC and a Web Api means that you have 2 separate applications which have their own separate memory space, processes, etc.

I would imagine that you could try something with AppDomain, but you may have problems with that.

You could try with session, but the web api is REST and should not have that, but if you really need the same instance, you could try to bend the rules a bit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM