March 31, 2020

SharePoint: Story of an orphan assemblyBinding and a misleading SPSite error message

Error

I have .NET console application doing all kinds of magic, but as a first step it creates instance of SPSite using the same piece of code I’ve used hundreds, if not thousands, of times over the last two decades:

using(var site = new SPSite(“http://xyz”))

But no, after few seconds it threw exception:

The Web application at http://xyz could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.

Thoughts

Having done all the suggested steps in here (including comments), and in all blog and forum posts related to this issue on the internet, I finally looked at ULS logs. Should’ve done it sooner. It had logged this error message when creating instance of SPSite:

Unexpected error while verifying backwards compatibility for [SPConfigurationDatabase] [e6f4eb31-959b-48da-a3a9-44d23f908f2e]: Microsoft.SharePoint.Upgrade.SPUpgradeException: Failed to call GetTypes on assembly Microsoft.Office.Server.Search, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c. Could not load file or assembly 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified. Could not load file or assembly 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified. Could not load file or assembly 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.   

What on earth, I don’t use Newtonsoft.Json in my project…?!

But I did! Only for a short while, though.

During some stage of the development I had added Newtonsoft.Json from NuGet, then removed it. It left orphan assemblyBinding reference pointing to Newtonsoft.Json in the app.config of the console application.

So basically my app.config in Visual Studio eventually the consoleapp.exe.config looked like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
     <startup>
         <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/>
     </startup>
   <runtime>
     <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
       <dependentAssembly>
         <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
         <bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0"/>
       </dependentAssembly>
     </assemblyBinding>
   </runtime>
</configuration>

This caused the slightly misleading Exception being thrown when doing the SPSite thing.

Solution

I removed the orphan <assemblyBinding></assemblyBinding> under <runtime> in my app.config, rebuilt the app, and sun started shining, and all was good:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
     <startup>
         <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/>
     </startup>
</configuration>

No comments:

Post a Comment