The below exception is very frustrating when we start working with SharePoint object model. Let us investigate how to solve below exception
Above Exception can encounter in two scenarios
1. Trying to use SharePoint 2010 object model in managed application like console or windows application.
2. Trying to use SharePoint 2010 object model in WCF Service or Web services.
Scenario # 1: The above Exception in console or windows application
Right click on Console/Windows Application project and select properties.
Click on Application tab and choose the target framework to .Net Framework 3.5
After changing Target framework to .NET Framework 3.5, click on Build tab
Change the Platform target to Any CPU.
Now when you run the application, you should not get the above exception
Scenario # 2: Above Exception when using SharePoint 2010 object model in WCF
If you are getting above exception while using SharePoint object model in WCF or any Web service. You need to perform below operations
Host WCF service in IIS. See the below link for step by step explanation on how to host WCF 4.0 Service in IIS 7.5
Only we need to make sure that,
Hosted WCF service is sharing the same application pool with SharePoint.
Select the Application pool to SharePoint-80
Try to apply both when you use SharePoint 2010object model. I hope this discussion will help you.