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…