SharePoint 2010 SPSite constructor returns System.IO.FileNotFoundException

Today, I encountered an issue with a console application written in .NET that accesses the SharePoint object model. I immediately got a FileNotFoundException when instantiating a new SPSite object.

This is the code:

using (var siteCollection = new SPSite(siteCollectionUrl))
{
….
}

I double checked the following possible issues:

  • Are we running Visual Studio in Elevated Administrator mode?
  • Is the code compiled in 64bit mode?
  • Do we have any features deployed to the site that might cause this behavior?

However, everything seemed to be OK on the configuration side. Then, I started to validate the permissions on both the database level as well in SharePoint. But again, everything should have been OK.

In the ULS logs, I found the following error message appearing every time I called the SPSite constructor:

An exception occured while trying to acquire the local farm:

System.TypeInitializationException: The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception.
---> System.TypeInitializationException: The type initializer for 'System.Data.SqlClient.SqlConnectionFactory' threw an exception.
---> System.TypeInitializationException: The type initializer for 'System.Data.SqlClient.SqlPerformanceCounters' threw an exception.
---> System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize
---> System.Configuration.ConfigurationErrorsException: Unrecognized configuration section serviceBehaviors. (C:\Projects\GenerateSampleData\bin\Debug\GenerateSampleData.exe.Config
line 6)
at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal)
at System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors()
at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)    

--- End of inner exception stack trace ---

at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)
at System.Configuration.ClientConfigurationSystem.PrepareClientConfigSystem(String sectionName)
at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)
at System.Configuration.ConfigurationManager.GetSection(String sectionName)
at System.Configuration.PrivilegedConfigurationManager.GetSection(String sectionName)
at System.Diagnostics.DiagnosticsConfiguration.Initialize()
at System.Diagnostics.Switch.InitializeConfigSettings()
at System.Diagnostics.Switch.InitializeWithStatus()
at System.Diagnostics.Switch.get_SwitchSetting()
at System.Data.ProviderBase.DbConnectionPoolCounters..ctor(String categoryName, String categoryHelp)
at System.Data.SqlClient.SqlPerformanceCounters..ctor()
at System.Data.SqlClient.SqlPerformanceCounters..cctor()    

--- End of inner exception stack trace ---

at System.Data.SqlClient.SqlConnectionFactory..ctor()
at System.Data.SqlClient.SqlConnectionFactory..cctor()    

--- End of inner exception stack trace ---

at System.Data.SqlClient.SqlConnection..cctor()    

--- End of inner exception stack trace ---

at System.Data.SqlClient.SqlConnection..ctor(String connectionString)
at Microsoft.SharePoint.Utilities.SqlSession.OpenConnection()
at Microsoft.SharePoint.Utilities.SqlSession.ExecuteReader(SqlCommand command, CommandBehavior behavior, SqlQueryData monitoringData, Boolean retryForDeadLock)
at Microsoft.SharePoint.Utilities.SqlSession.ExecuteReader(SqlCommand command, Boolean retryForDeadLock)
at Microsoft.SharePoint.Utilities.SqlSession.ExecuteReader(SqlCommand command)
at Microsoft.SharePoint.Upgrade.SPDatabaseSequence.GetVersion(SPDatabase database, Guid id, Version defaultVersion, SqlSession session, SPDatabaseSequence sequence)
at Microsoft.SharePoint.Upgrade.SPDatabaseSequence.get_SchemaVersion() at Microsoft.SharePoint.Upgrade.SPSequence.get_IsBackwardsCompatible()
at Microsoft.SharePoint.Upgrade.SPUpgradeSession.IsBackwardsCompatible(Object o, Boolean bRecurse)
at Microsoft.SharePoint.Administration.SPPersistedUpgradableObject.get_IsBackwardsCompatible()
at Microsoft.SharePoint.Administration.SPPersistedUpgradableObject.ValidateBackwardsCompatibility()
at Microsoft.SharePoint.Administration.SPConfigurationDatabase.Initialize(SqlConnectionStringBuilder connectionString, Boolean enableCaching, Boolean checkCompatibility, Boolean bindRequestGuid)
at Microsoft.SharePoint.Administration.SPConfigurationDatabase.Initialize(SqlConnectionStringBuilder connectionString, Boolean enableCaching, Boolean checkCompatibility)
at Microsoft.SharePoint.Administration.SPConfigurationDatabase.get_Local()
at Microsoft.SharePoint.Administration.SPFarm.FindLocal(SPFarm& farm, Boolean& isJoined) 

 

The interesting part was the "Unrecognized configuration section" and "Configuration system failed to initialize" messages that appeared in the logs.

Then, I realized we had some app.config file toghether with our console application, which contained some settings:

The serviceBehavors element was causing this issue! As soon as we commented out this section in the app.config file, the problem disappeared.

Hope this can help you next time!