The only equivalent for Ninject Bind. ToMethod of IPrincipal, IIdentity

I am trying to replicate the following Ninject syntax in Unity, but with no luck:

Bind<IIdentity>().ToMethod(c => HttpContext.Current.User.Identity); 

I think it should look something like this:

 IUnityContainer container; ... container.RegisterType<IIdentity>(HttpContext.Current.User.Identity); 

How should it be?

+6
source share
3 answers

While neontapir's answer may work, this extension method is deprecated. The correct way to do this will now use an InjectionFactory :

 container.RegisterType<IIdentity>(new InjectionFactory(u => HttpContext.Current.User.Identity)); 
+11
source
 container.RegisterInstance<IIdentity>(...); 
+1
source

I suppose a static factory extension will do this. I'm rusting on Unity. Seeman Dependency Injection in .NET is a good resource for such situations.

0
source

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


All Articles