Developing With Sitefinity When A Different Version of OpenAccess is Installed

By in
No comments

One of the best features of Sitefinity is the inclusion of the fantastic ORM tool OpenAccess. However, one issue that can be a bit troublesome is the fact that Sitefinity has often shipped with a different version of OpenAccess that the latest.

As a result, if you have the most recent OpenAccess installed, and try to build a Sitefinity project, you might get an error like below:

Error 31 Could not load file or assembly ‘Telerik.OpenAccess.35.Extensions, Version=2012.2.607.1, Culture=neutral, PublicKeyToken=7ce17eeaf1d59342’ or one of its dependencies. General Exception (Exception from HRESULT: 0x80131500) ExceptionString: System.IO.FileLoadException: Could not load file or assembly ‘Telerik.OpenAccess.35.Extensions, Version=2012.2.607.1, Culture=neutral, PublicKeyToken=7ce17eeaf1d59342’ or one of its dependencies. General Exception (Exception from HRESULT: 0x80131500) File name: ‘Telerik.OpenAccess.35.Extensions, Version=2012.2.607.1, Culture=neutral, PublicKeyToken=7ce17eeaf1d59342’ —> System.Exception: The referenced assembly ‘c:\MyPath\Telerik.OpenAccess.35.Extensions.dll’ has the wrong version; expected Version=2012.2.607.1 but found Version=2011.3.1320.1. at Telerik.OpenAccess.SPI.AssemblyProvider.LoadFromReferences(String assemblyName) at Telerik.OpenAccess.SPI.AssemblyProvider.RefResolveEventHandler(Object sender, ResolveEventArgs args) at System.AppDomain.OnAssemblyResolveEvent(RuntimeAssembly assembly, String assemblyFullName) at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, Boolean loadTypeFromPartialName, ObjectHandleOnStack type) at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, Boolean loadTypeFromPartialName) at System.RuntimeType.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark) at System.Type.GetType(String typeName) at Telerik.OpenAccess.Sdk.Enhancer.Enhancer.CrossDomainRunImpl(AssemblyLoader assemblyLoader) at Telerik.OpenAccess.Sdk.Enhancer.EnhancerBase.CrossDomainRun()

By default, OpenAccess is going to look in the GAC for the latest version to enhance your Visual Studio projects. Since this version differs from the Sitefinity version, you get this assembly version mismatch error.

Fortunately, Telerik is planning to synchronize the releases of Sitefinity and OpenAccess so that the versions will always match. This is great news!

However, this unfortunately won’t help if you need to open an older Sitefinity project that hasn’t been upgraded and therefore is using an older OpenAccess release. Although upgrading to the latest Sitefinity version would resolve the problem, this isn’t always possible.

It is possible to work around this problem, with a little help from the OpenAccess product archive and an MSI extractor (special thanks to Josh Holt for his help in putting this together!).

Identify and Download the OpenAccess SDK

The first thing you need to do is determine which version of OpenAccess your Sitefinity project is using. The easiest way I’ve found to do this is to right-click the Telerik.OpenAccess.dll file in the bin folder and switch to the Details tab. Here I see I’m using the 2011.3.1320.1 release of OpenAccess.

Sitefinity-Open-Access-Version

Next go to your Telerik Account and find the downloads section for OpenAccess.

If you need the latest version you can simply grab it from there, but you’ll most likely be needed to go to the Older Versions section.

Sitefinity-Download-Open-Access

You’ll be shown a list of all the public releases of OpenAccess. As you can see below, the version we want is not in this list. This is because the Sitefinity project we are using was enhanced with an Internal Build of OpenAccess.

Fortunately the build numbers are listed along with each release, so just like the Price is Right, you want to find the one that is closest to your release (without going over).

Sitefinity-Download-Open-Access-Version

You’ll then be taken to the release page with the associated downloads, including all internal builds for that release. Find the one that matches the Sitefinity project and download it.

Be sure to download the SDK installer, and not just the regular OpenAccess installer, as this contains the additional files you’ll need later.

Sitefinity-Download-Open-Access-SDK

Extract the OpenAccess SDK MSI

Next we need to extract the OpenAccess files that Sitefinity will need to be enhanced. A more detailed walkthrough of the files needed and how you can do this for other Visual Studio projects is available on the OpenAccess Blogs: Different OpenAccess Versions Used in Development Process.

There are several tools to extract MSI files. Here are a few articles to get you started:

Once you’ve extracted the MSI contents, you’ll want to locate and copy the following files into your Sitefinity’s bin folder.

  • enhancer.exe
  • enhancer.exe.config
  • venhance.exe
  • venhance.exe.config
  • Telerik.OpenAccess.SDK
  • Mono.Cecil.dll
  • Mono.Cecil.pdb.dll
  • OpenAccess.targets

Update .targets and .csproj Files

Finally, you want to update both the OpenAccess.targets file and your Sitefinity .csproj file so that the .targets file so they explicitly point to your Sitefinity bin folder instead of the GAC or latest OpenAccess version.

First, modify the OpenAccess.targets file with the following line so that it points to the contents of the bin folder.

 <PropertyGroup>
    <UseOpenAccessTargets Condition="'$(UseOpenAccessTargets)'==''">true</UseOpenAccessTargets>
    <OpenAccessPath Condition="'$(OpenAccessPath)'==''">..\bin\</OpenAccessPath>
    <EnhancerAssembly>enhancer.exe</EnhancerAssembly>
  </PropertyGroup>

Next, edit your .csproj file by right-clicking it and selecting “Unload Project”, then right-click it again and select “Edit”.

Locate the the line that refers to the OpenAccess.targets file and update it to point to your local file in the bin folder.

Sitefinity-Update-Project-OpenAccess-Targets

Now reload your project and build; Your Sitefinity project is now permanently configured to use its specific OpenAccess version, regardless of whatever version you have installed.

I recommend doing this on every new Sitefinity, if possible. Even though Sitefinity and OpenAccess are planned to be in sync from now on, as I mentioned, this trick really comes in handy if you ever have to go back to an old project (which as developers, you know you will!).

Hope this is helpful!

The following two tabs change content below.

selaromdotnet

Senior Developer at iD Tech
Josh loves all things Microsoft and Windows, and develops solutions for Web, Desktop and Mobile using the .NET Framework, Azure, UWP and everything else in the Microsoft Stack. His other passion is music, and in his spare time Josh spins and produces electronic music under the name DJ SelArom. His other passion is music, and in his spare time Josh spins and produces electronic music under the name DJ SelArom.