Ninject InRequestScope fallback to InThreadScope

后端 未结 3 1641
抹茶落季
抹茶落季 2021-01-11 14:28

In my MVC3 project I\'ve setup my kernel to Ninject the Entityframework context on a InRequestScope basis, this works perfect. But I have a ba

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-11 15:10

    There's an InScope method, with which you can specify a custom scoping rule.

    The implementation of InRequestScope is currently undergoing change from 2.2-2.4.

    So go to the source of your version, look at the impl of InRequestScope and InThreadScope and create an amalgam (which you can then slot in as an extension method alongside the other InXXXScope methods.

    It'll look (before you extract it into an extension method) something like:

    Bind.To().InScope( ctx => ScopeContext.Request(ctx) ?? ScopeContext.Thread(ctx))
    

    The other way is to create two Bindings, with an appropriate constraint such as WhenInjectedInto to customize the scoping based on the contextual binding.

提交回复
热议问题