ILogger and SPItemEventReceiver

Jan 28, 2010 at 9:52 AM


I define my SPItemEventReceiver class like this (code removed for clarity)

namespace XXX.Oqa.SharePoint.ItemReceivers
    public class CreateNewSubWeb : SPItemEventReceiver
        ILogger _logger = null;

        public CreateNewSubWeb()
            _logger = ServiceLocator.Current.GetInstance<ILogger>(); //Registrered at farm level

I try to add the event receiver to a list like this

list.EventReceivers.Add(ItemAdding, "XXX.Oqa.SharePoint, Version=, Culture=neutral, PublicKeyToken=ca73e88caf79b823", "XXX.Oqa.SharePoint.ItemReceivers.CreateNewSubWeb");

I then receive this error

System.InvalidOperationException was unhandled by user code
  Message="Exception has been thrown by the target of an invocation."
       at Microsoft.SharePoint.SPEventReceiverDefinition.ValidAssembly()
       at Microsoft.SharePoint.SPEventReceiverDefinition.ValidReceiverFields()
       at Microsoft.SharePoint.SPEventReceiverDefinition.UpdateInternal(Boolean isMigration)
       at Microsoft.SharePoint.SPEventReceiverDefinition.Update()
       at Microsoft.SharePoint.SPEventReceiverDefinitionCollection.Add(SPEventReceiverType receiverType, String assembly, String className)
       at TetraPak.Oqa.Repository.Storage.AttachEventReceiver(SPEventReceiverType Type, String AssemblyName, String ClassName, String ListConfigKey)
       at TetraPak.Oqa.SharePoint.GlobalListsFeatureReceiver.FeatureActivated(SPFeatureReceiverProperties properties)
       at Microsoft.SharePoint.SPFeature.DoActivationCallout(Boolean fActivate, Boolean fForce)

When I remove code regarding ILogger the registration works fine.

All assemblies are located in the GAC

The error can also be validated by using this code

System.Security.Policy.Evidence x = new System.Security.Policy.Evidence();
System.Reflection.Assembly a = System.Reflection.Assembly.Load("XXX.Oqa.SharePoint, Version=, Culture=neutral, PublicKeyToken=ca73e88caf79b823", x);
object y = a.CreateInstance("XXX.Oqa.SharePoint.ItemReceivers.CreateNewSubWeb");

Can´t the ILogger interface be used in this scenario?


// Henrik

Jan 29, 2010 at 10:43 AM

Hi Henrik,

Can you try with SharePointServiceLocator.Current.GetInstance<ILogger>();




Jan 29, 2010 at 1:03 PM

Hi Appaji,

That seemed to do the trick.

Can you please explain this behaviour for me, it seems that I missed to understand something fundamental.


// Henrik

Jan 29, 2010 at 1:18 PM

Hi Henrik,

We discuss this in the documentation for the SharePoint Service Locator.  We load the configuration for the service locator out of the SPFarm property bag.  The ServiceLocator class is provided by the Common Service Locator project.  It is intended for environments where there is a "bootstrapping" event we can get when the application initially starts, such as events you get in global.asax with a typical web app.  We don't have any equivalent event to hook in SharePoint, so we rely upon calling into SharePointServiceLocator.Current as the event where we can do the initial bootstrapping.  We include the Common Service Locator project so our service locator can implement the interfaces provided by common service locator for compatibility purposes if it is replaced in the future (or if someone wants to implement their own).


Jan 29, 2010 at 1:28 PM


ok, I see. Thanks for the explanation ( I need to read the docs again, more carefully :) )


// Henrik