c# - Ninject field inject not work in class with constructor -
in c# mvc project, used ninject ioc pattern, when using constructor injection works fine without problem when use field injection, null reference exception occurred _ordernotification.
public class shopmanager { [inject] private iordernotification _ordernotification; public shopmanager(string shopname) { //do somethings } public int getnotifycount() { return _ordernotification.count(); } }
also registered services in ninjectwebcommon,
kernel.bind<iordernotification>().to<ordernotification>();
ninject version 3.0
try this:
public iordernotification _ordernotification; public iordernotification ordernotification { { return _ordernotification ?? (_ordernotification = dependencyresolver.current.getservice<iordernotification>()); } set { _ordernotification = value; } }
also can use without constructor:
public class shopmanager { [inject] public iordernotification ordernotification { get; set; } public int getnotifycount() { return ordernotification.count(); } }
Comments
Post a Comment