I got bitten today by having an EventReceiver in a separate assembly to my actual WSP Visual Studio Project. The EventReceiver was using the Unity and OpenXML framework and I had not referenced these in my WSP Visual Studio Project. When I deployed my WSP and ran a FeatureReceiver that was adding this EventReceiver programmatically to a List I was getting the below error:
Feature receiver assembly 'PerformanceAppraisalPackage, Version=184.108.40.206, Culture=neutral, PublicKeyToken=6f9f3c9091e369cc', class 'PerformanceAppraisalPackage.PerformanceAppraisalWebFeature', method 'FeatureActivated' for feature 'c5278558-662f-4fec-a5f9-61b109d8e1db' threw an exception: System.InvalidOperationException: Exception has been thrown by the target of an invocation.
at Microsoft.SharePoint.SPEventReceiverDefinition.UpdateInternal(Boolean isMigration)
at Microsoft.SharePoint.SPEventReceiverDefinitionCollection.Add(SPEventReceiverType receiverType, String assembly, String className)
at Readify.SharePoint.Provisioning.PerformanceAppraisalWebProvisioning.CreatePerformanceAppraisalList(SPWeb web, SPContentType pcCT, SPContentType scCT, SPContentType sdCT)
at Readify.SharePoint.Provisioning.PerformanceAppraisalWebProvisioning.ProvisionWeb(SPSite site, SPWeb web)
at Microsoft.SharePoint.SPFeature.DoActivationCallout(Boolean fActivate, Boolean fForce)
The error message wasn't all that clear and nor was the stack trace. On investigating the ULS Logs further I found another entry there:
Event manager error: Exception has been thrown by the target of an invocation.
This error message highlighted that is was definitely something to do with adding the EventReceiver. The problem was the code I was deploying's FeatureReceiver had lots going on so I started chasing all sorts of possibilities.
To resolve this issue, basically I added the dlls in the other project to my WSP Visual Studio Project and ensured that Copy Local was set to true. This ensured that these would be packaged with my WSP and deployed to my farm. Then when I activate the FeatureReceiver I no longer get this error. I figured I'd blog this as Google wasn't much help on the subject…so when someone else searches for this error they'll get this ;-) Hope it helps!