Step by step guide for solving of the nopCommerce FluentValidation and .NET 4.5 problem

When you have .NET 4.5 installed it is impossible to run any nopCommerce version prior to nopCommerce 2.65. This happens because all the previous versions of nopCommerce use a version of the FluentValidation library that is not compatible with .NET 4.5 and throws exceptions at runtime. So if you have seen this error message "System.Security.VerificationException: Operation could destabilize the runtime" then you are probably already aware of this issue. If not, then you can read the release notes of nopCommerce 2.65, which uses a newer version of FluentValidation, where the .NET 4.5 problem is fixed.

In this blog post we will provide a step by step guide for resolving the problem, so you won't need to uprade your web sites to nopCommerce 2.65, even when your hosting provider upgrades the servers and installs .NET 4.5.

The idea is to use the new signed version of the FluentValidation assembly and have a binding redirect in the Web.config file. This way any references to the old signed FluentValidation assembly 3.2.0.0 will be redirected to the new stable signed version 3.4.6.0. All versions of nopCommerce prior to 2.65 reference a signed version of FluentValidation 3.2.0.0, so it is important to use the signed version of FluentValidation 3.4.6.0 as otherwise the binding redirect won't work. The signed version has a publickeytoken=a82054b837897c66, while the unsigned version has a publickeytoken=null.

There are basically two scenarios here - having a deployed web site (no source code) and working with the nopCommerce source code.

Scenario 1 - You have a running nopCommerce web site (no source code, so it is already compiled and referencing FluentValidation 3.2.0.0).

1. Download and copy the content of the zip file to the Bin folder of your web site, replacing all existing files. The folder contains the new FluentValidation 3.4.6.0 signed assemblies.

2. Modify the web site's Web.config file by adding the following text.
Note: There is alredy a record for the FluentValidation that you need to replace with the text below.

<dependentAssembly>
<assemblyIdentity name="FluentValidation" publicKeyToken="a82054b837897c66" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.3.1.0" newVersion="3.4.6.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="FluentValidation.MVC" publicKeyToken="a82054b837897c66" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.3.1.0" newVersion="3.4.6.0" />
</dependentAssembly>

That's all. Your nopCommerce web site will now work with .NET 4.5!

 

Scenario 2 - You are working with the nopCommerce source code. 

1. Update the references to FluentValidation to the latest signed version. There are several projects that reference FluentValidation assembly. When you open the nopCommerce solution in Visual Studion you can find the projects in the following solution folders:

Plugins:

Nop.Plugin.Payments.AuthorizeNet
Nop.Plugin.Payments.Manual
Nop.Plugin.Payments.PayPalDirect 

Presentation:

Nop.Admin
Nop.Web
Nop.Web.Framework

Tests:

Nop.Web.MVC.Tests

The easies way to update the references is to use the NuGet extension for Visual Studio. If you don't have NuGet installed, you need to do so, by going to the Extension Manager.

Visual Studio Extension Manager button

When you open the Extension Manager you need to find the Nuget Package Manager extension.
Simply search for "nuget" in the online gallery. Then download, install it and restart Visual Studio.

Nuget Package Manager

Once you get the NuGet installed and integrated, you can use it to update the references to FluentValidation in all the projects above.
To do so you need to right-click on the References and click on Manage Nuget Packages.

Adding reference using NuGet

When the NuGet package manager is opened, you need to search online for the FluentValidation-Signed and add it.

FluentValidation-Signed


The Nop.Web project references the FluentValidation.MVC assembly, so you also need to search and add FluentValidation.MVC3-Signed

FluentValidation MVC3 Signed

Please note that you need to update the references in all the projects listed above then continue with Step 2.

2. Modify the web site's Web.config file by adding the following text. 
Note: There is alredy a record for the FluentValidation that you need to replace with the text below. 

<dependentAssembly>
<assemblyIdentity name="FluentValidation" publicKeyToken="a82054b837897c66" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.3.1.0" newVersion="3.4.6.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="FluentValidation.MVC" publicKeyToken="a82054b837897c66" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.3.1.0" newVersion="3.4.6.0" />
</dependentAssembly>

That's all. Now you can rebuild your solution and run your web site inside Visual Studio and it will work with .NET 4.5!