Ninject with Sharepoint 2

In first acticle we talk about how we configure our Ninject with Sharepoint .Now there are two issue one of them is our ninject dlls permissions and the your assemblies that using ninject should allow partially trust callers.
http://blog.bugrapostaci.com/2010/02/23/ninject-with-sharepoint/

When you configure your ninject dll to sharepoint you probably encounter with permission denied errors . That is normal beacuse our custom Ninject Library is using Reflection and Sharepoint libraries and you have to define CAS for this libraries .

Setup your CAS

I’m using for deployment  WSPBuilder tool and i use it with  a customCAS file.There are two CAS permission as important

  • Microsoft.SharePoint.Security.SharePointPermission
  • System.Security.Permissions.ReflectionPermission

For deployment batch file contains that:

WSPBuilder.exe -WSPName Ninject.Deploy.wsp -CustomCAS CustomCAS.txt -SolutionID [Your Solution ID]

And you should copy CustomCAS.txt to same folder of WspBuilder.exe that contains

<IPermission class=”Microsoft.SharePoint.Security.SharePointPermission, Microsoft.SharePoint.Security, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” version=”1″ ObjectModel=”True” UnsafeSaveOnGet=”True” Unrestricted=”True”  /  >

<IPermission class=”System.Security.Permissions.ReflectionPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089″ version=”1″ Flags=”3″ Unrestricted=”True” /  >

For more information WSPBuilder with custom CAS there is a simple post here:
http://www.novolocus.com/2009/08/13/wspbuilder-and-cas-policies/
For more info Microsoft Windows SharePoint Services and Code Access Security is Msdn
http://msdn.microsoft.com/en-us/library/dd583158(office.11).aspx

Allow Partially Trusted Callers

There is another job about your  class or page assemblies that using ninject should be contains 
[assembly: AllowPartiallyTrustedCallers()]
attribute in your AssemblyInfo.cs file . (You should add refrence of “using System.Security;” namespace)

After build you can craete a setup with Sharepoint Solution Installer using your wsp file deploy without any further error.

Happy codding…

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.   

Prerequists.
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.http://blog.bugrapostaci.com/2009/11/12/step-by-step-configure-your-sharepoint-running-with-net-framework-3-5/   

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.http://blog.bugrapostaci.com/2010/02/22/solved-ninject-system-runtime-compilerservices-extensionattribute-is-defined-in-multiple-assemblies/   

For working with Ninject in Asp.net 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.
http://ninject.org/assets/dist/Ninject-1.0-src.zip   

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 :
http://rapidshare.com/files/354665567/NinjectSource.rar 

And i also add functionalities of UserControlBase and WebControlBase classes for injection about a good case.
Thats the page:http://stackoverflow.com/questions/1274026/ninject-asp-net-and-custom-controls

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.
        }
    }
</SCRIPT>

Keep going with Part 2:
http://blog.bugrapostaci.com/2010/02/27/ninject-with-sharepoint-2/

[Solved] Ninject ‘System.Runtime.CompilerServices.ExtensionAttribute’ is defined in multiple assemblies

Ninject Bug with .net framework 3.5

If you getting this error with your application that using ninject dependency injector.

System.Runtime.CompilerServices.ExtensionAttribute’ is defined in multiple assemblies in the global alias; using definition from  [….]\Ninject.Core.dll
Or
Missing compiler required member ‘System.Runtime.CompilerServices.ExtensionAttribute..ctor’

This is a know bug with ninject.NET 2.0 build would work for .NET 3.5 as well, but the extension method patch will screw things up.
http://bugs.ninject.org/jira/browse/NINJECT-4

Solution is:

 You can solve it by yourself

1)  Download source , extrat it some folder and open with VS IDE.
http://ninject.org/assets/dist/Ninject-1.0-src.zip

2) There are two project   “Core” and “Tests” that we change .

3) Open project properties windows by click solution explorer-> Core (Right Click ) -> Properties

4)Click “Application” Tab on the left side

5) Select  “.NET 3.5 framework ” in Target Framework combobox. 

6) Click  “Build” Tab on the left side

7)  type “NET_35” in Conditional Compilation Symbol box (Preprocessor flag)

8) Do it same with “Tests” Project

9) Rebuild solution.

Its worked for me 🙂

Ninject – Dependency Injector

Ninject is the ninja of dependency injectors.Ninject is a lightning-fast, ultra-lightweight dependency injector for .NET applications. It helps you split your application into a collection of loosely-coupled, highly-cohesive pieces, and then glue them back together in a flexible manner. By using Ninject to support your software’s architecture, your code will become easier to write, reuse, test, and modify.

Example of Code:

interface IWeapon
{
    void Hit(string target);
}
class Samurai
{
    private IWeapon _weapon;
 
    [Inject]
    public Samurai(IWeapon weapon)
    {
        _weapon = weapon;
    }
 
    public void Attack(string target)
    {
        _weapon.Hit(target);
    }
}
class WarriorModule : StandardModule {
  public override Load() {
    Bind<IWeapon>().To<Sword>();
    Bind<Samurai>().ToSelf();
  }
}
class Program
{
    public static void Main()
    {
        IKernel kernel = new StandardKernel(new WarriorModule());
        Samurai warrior = kernel.Get<Samurai>();
        warrior.Attack("the evildoers");
    }
}

..

My Rank is: 8.8

Project Home Page:
http://ninject.org/
Codeplex Home Page:
http://ninject.codeplex.com/
Download:for .NET Framework 2.0, 3.0, 3.5 version 1.0
http://ninject.org/assets/dist/Ninject-1.0-release-net-2.0.zip

You dont need to reinventing the wheel 🙂