Ninject with Sharepoint

Ninject is one of the best tool for dependency injection that i  use. I tell about how to use with sharepoint in this article.   

Ninject Source Code,
Microsoft Sharepoint Library.   

First i want to say that i m using Sharepoint with Framework 3.5 and you can find how to configure sharepoint with working Sharepoint in this article of mine.   

Thats why one of the reason  i recompile with Ninject Library with Framework 3.5  for this but if you use Framework 2.0 its not necessery for you. if  you mind you learn about mantioned in my another article.   

For working with Ninject in you should inherit Global class with Ninject.Framework.Web.NinjectHttpApplication but in sharepoint standart Global class is derived from Microsoft.Sharepoint.ApplicationRuntime.SPHttpApplication. the second reason that i recompile ninject source.   

Example of Standart Sharepoint Global.asax:
[Drive]:\inetpub\wwwroot\wss\VirtualDirectories\[Your app port]\Global.asax   

<%@ Assembly Name=”Microsoft.SharePoint”%>
<%@ Application Language=”C#” Inherits=”Microsoft.SharePoint.ApplicationRuntime.SPHttpApplication” %>   

You can download Ninject Source Code from project site of Ninject.   

Than i created a new project in Framework folder. And add references of Microsoft.Sharepoint library.
My project’s namespace is Ninject.Framework.Web.Sharepoint.I create a class named “NinjectSPHttpApplication”
that just inherits SPHttpApplication inspite of  HttpApplication. All members code as same as Ninject.Framework.Web.NinjectHttpApplication class.   

namespace Ninject.Framework.Web.Sharepoint   


    /// <summary>   

    /// A <see cref=”HttpApplication”/> that creates a <see cref=”IKernel”/> for use throughout   

    /// the application.   

    /// </summary>   

    public abstract class NinjectSPHttpApplication : SPHttpApplication   


          ….Same as NinjectHttpApplication   




And build the all solution. 

You can download source code of my project : 

And i also add functionalities of UserControlBase and WebControlBase classes for injection about a good case.
Thats the page:

After your ninject dlls ready you should add this dlls to your 80/BIN folder and register with GAC and add to your sharepoint application web.config file. I don’t tell how to register custom assemblies in sharepoint asume that you know already. 

After you complete this you can change that your Global.asax file in your Sharepoint wss root folder.
[Drive:]\inetpub\wwwroot\wss\VirtualDirectories\[Your app port]\Global.asax Like: 

<%@ Import Namespace=”Ninject.Framework.Web.Sharepoint” %>
<%@ Import Namespace=”Ninject.Core” %>
<%@ Application Language=”C#” inherits=”Ninject.Framework.Web.Sharepoint.NinjectSPHttpApplication” %> 

<SCRIPT language=”C#” runat=”server”>    
    protected override IKernel CreateKernel()
        IKernel kernel = new StandardKernel(new SharePointUnitTestModule() );
        return kernel;
    public class SharePointUnitTestModule : StandardModule
        public override void Load()
          ...Your codes here.

Keep going with Part 2:


Session_End is not fired

Remarkable Question :
1. Remember Session_End event is supported only in InProc mode.  
2. Session_End won’t be fired if you close your browser. HTTP is a stateless protocol, and the server has no way to know if your browser has closed or not. 
3. Session_End will be fired only (i) after n minutes of inactivity (n = timeout value), or (ii) if someone calls Session.Abandon(). 
4. For case (i) (pt. 3), Session_End will be run by a background thread, which implies:

    a. Your code in Session_End is running using the worker process account. You may have permission problem if you’re accessing resource such as database.
    b. If an error happens in Session_End, it will fail silently.
5. For case (ii), please note that in order for Session_End to be fired, your session state has to exist first.  That means you have to store some data in the session state and has completed at least one request.  
6. Again for case (ii), Session_End will be called only if the abandoned session is actually found. As a result, if you create and abandon a session inside the same request, because the session hasn’t been saved and thus can’t be found, Session_End won’t be called.  This is a bug in v1 and upcoming v1.1.