ASP.NET Webform如何使用Ninject.Web.Common.WebHost道具注入

时间:2018-10-15 08:22:23

标签: c# asp.net webforms ninject ninject.web

我已将我的所有服务绑定到App_Start Ninject.Web.Common中,并尝试在我的aspx页面中进行[Inject]属性注入。

如何使用属性注入?我在Webform项目中例外,我所有注入的道具都为空。

Exception Stacktrace:

  

System.NullReferenceException未通过用户代码HResult = -2147467261处理。Message = Nesnebaşvurusubir nesneninörneğineayarlanmadı。    来源= VDershane.Base StackTrace:编号:VDershane.Base.WebForms.DRecoveryPageLite.LoadLang()c:\ Users \ Alican Kablan \ Documents \ Yeniklasör\ WebForms \ DRecoveryPageLite.cs目录:158个Webnum:VDershane.Base。 DRecoveryPageLite.OnLoad(EventArgs e)c:\ Users \ Alican Kablan \ Documents \ Yeniklasör\ WebForms \ DRecoveryPageLite.cs目录:191编号:System.Web.UI.Control.LoadRecursive()编号:System.Web.UI。 Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)InnerException:

我的网络表单项目参考

框架4.5

注入3.3.3

Ninject.Web.Common 3.3.1

Ninject.Web.Common.WebHost 3.3.1


在我的 Mvc 项目中(我正在这样工作):

 private static void RegisterServices(IKernel kernel)
    {
        DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
    }



public class NinjectDependencyResolver : IDependencyResolver
{
    private readonly IKernel _kernel;

    public NinjectDependencyResolver()
    {
    }

    public NinjectDependencyResolver(IKernel kernel)
    {
        _kernel = kernel;
        AddBinding();
    }

    private void AddBinding()
    {
        _kernel.Bind<IBlogServices>().To<BlogServices>();
        _kernel.Bind<ILanguageServices>().To<LanguageServices>();
        _kernel.Bind<IFrUserServices>().To<FrUserServices>();
        _kernel.Bind<IBlogRepository>().To<BlogRepository>();
        _kernel.Bind<ILanguageRepository>().To<LanguageRepository>();
        _kernel.Bind<IFrUserRepository>().To<FrUserRepository>();
    }

    public object GetService(Type serviceType)
    {
        return _kernel.TryGet(serviceType);
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        return _kernel.GetAll(serviceType);
    }
}

0 个答案:

没有答案