Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ninject:Ninject.web-如何在常规ASP.Net web(!MVC)上应用_Asp.net_Dependency Injection_Webforms_Ninject - Fatal编程技术网

Ninject:Ninject.web-如何在常规ASP.Net web(!MVC)上应用

Ninject:Ninject.web-如何在常规ASP.Net web(!MVC)上应用,asp.net,dependency-injection,webforms,ninject,Asp.net,Dependency Injection,Webforms,Ninject,我看到的是类似于下面()的内容: 自述文件。标记 此扩展允许Ninject核心和ASP.NET MVC项目之间的集成。要使用它,只需使您的HttpApplication(通常在Global.asax.cs中)扩展NinjectHttpApplication: 公共类YourWebApplication: NinjectHttpApplication{public 覆盖Application Started()上的无效内容 { //这仅在MVC1中需要 RegisterAllControllers

我看到的是类似于下面()的内容:

自述文件。标记

此扩展允许Ninject核心和ASP.NET MVC项目之间的集成。要使用它,只需使您的HttpApplication(通常在Global.asax.cs中)扩展NinjectHttpApplication:

公共类YourWebApplication: NinjectHttpApplication{public 覆盖Application Started()上的无效内容
{ //这仅在MVC1中需要 RegisterAllControllersIn(“Some.Assembly.Name”); }

公共覆盖IKernel CreateKernel(){ 返回新的标准内核(new SomeModule(),new SomeOtherModule(), …)


一旦您这样做,您的控制器将通过Ninject激活,这意味着您可以公开对其构造函数(或属性或方法)的依赖关系以请求注入。

由于您在问题中没有排除这一点,我不得不假设您不知道


(链接自)

只想分享我是如何使用VisualStudio2008解决这个问题的

对于那些访问过www.tekpub.com的人来说,下面的代码有点熟悉,是的!下面的正确代码来自于掌握ASP.NET MVC 2.0系列,以及如何使用NLog的演示

所需参考资料:

<%@ Application Language="C#" Inherits ="Ninject.Web.NinjectHttpApplication"  %>
<%@ Import Namespace="App_Code.Infrastructure.Logging"%>
<%@ Import Namespace="Ninject.Modules"%>
<%@ Import Namespace="Ninject"%>

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup

    }

    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown

    }

    void Application_Error(object sender, EventArgs e) 
    { 
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e) 
    {
        // Code that runs when a new session is started
    }

    void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.

    }


    protected override void OnApplicationStarted()
    {
        //base.OnApplicationStarted();

        Container.Get<ILogger>().Info("Application Started");
    }

    protected override IKernel CreateKernel()
    {
        return Container;
    }

    static IKernel Container
    {
        get
        {
            return new StandardKernel(new SiteModule());
        }

    }

    class SiteModule : NinjectModule
    {
        public override void Load()
        {
            Bind<ILogger>().To<NLogger>().InSingletonScope();
        }
    }

</script>
  • Ninject.dll
  • Ninject.Web
  • NLog.dll
Global.asax:

<%@ Application Language="C#" Inherits ="Ninject.Web.NinjectHttpApplication"  %>
<%@ Import Namespace="App_Code.Infrastructure.Logging"%>
<%@ Import Namespace="Ninject.Modules"%>
<%@ Import Namespace="Ninject"%>

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup

    }

    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown

    }

    void Application_Error(object sender, EventArgs e) 
    { 
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e) 
    {
        // Code that runs when a new session is started
    }

    void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.

    }


    protected override void OnApplicationStarted()
    {
        //base.OnApplicationStarted();

        Container.Get<ILogger>().Info("Application Started");
    }

    protected override IKernel CreateKernel()
    {
        return Container;
    }

    static IKernel Container
    {
        get
        {
            return new StandardKernel(new SiteModule());
        }

    }

    class SiteModule : NinjectModule
    {
        public override void Load()
        {
            Bind<ILogger>().To<NLogger>().InSingletonScope();
        }
    }

</script>

无效应用程序\u启动(对象发送方,事件参数e)
{
//在应用程序启动时运行的代码
}
无效应用程序\u结束(对象发送方,事件参数e)
{
//应用程序关闭时运行的代码
}
无效应用程序错误(对象发送方,事件参数e)
{ 
//发生未处理错误时运行的代码
}
无效会话\u启动(对象发送方,事件参数e)
{
//启动新会话时运行的代码
}
无效会话\u结束(对象发送方,事件参数e)
{
//会话结束时运行的代码。
//注意:只有在sessionstate模式下才会引发Session_End事件
//在Web.config文件中设置为InProc。如果会话模式设置为StateServer
//或SQLServer,则不会引发该事件。
}
Application Started()上受保护的覆盖无效
{
//base.on应用程序启动();
Container.Get().Info(“应用程序已启动”);
}
受保护的覆盖IKernel CreateKernel()
{
返回容器;
}
静态IKernel容器
{
得到
{
返回新的标准内核(新的SiteModule());
}
}
类SiteModule:NinjectModule
{
公共覆盖无效负载()
{
绑定().To().InSingletonScope();
}
}

部分正确。我知道扩展名ninject.web,但不知道如何在现有的asp.net web应用程序上实现它。@没有正文:涵盖得很好-有一个用于全局和页面的基类。有什么不包括的吗?如果有,问一下。但最后,海登使用了ninject.Framework.web。这与新的ninject.web?它是v1版本(特别是即将上市的版本)。在V2中,与v1相比,扩展性机制已经被清理/整理/概括了一些——新的命名就是其中的一部分。东西重新集成的工作方式并没有根本改变[而obv ASP.NET也没有太大改变]。(@Ian Davis将他的作品称为Nate作品的一部分,这反映了这一点。如果他从根本上改变了它,我相信他会在readme.markdown或代码中的注释中记录它)。你下载了它并查看了源代码吗?这是一个很好的代码,它将教给你的远不止是在论坛上来回走动:Pthanks,到目前为止还没有挖掘源代码,我希望看到一些示例代码/实际应用程序,但是,可以看到,似乎大多数人都喜欢asp.net mvc,而不是asp.net web。