Wednesday, March 20, 2019

How to handle 'Which has a higher version than referenced assembly' error (Error CS1705)

Where there are multiple projects within the solution and one project is dependent on others, and versions introduce breaking changes, we sometimes encounter package version conflict and Visual Studio resulting in build error in the solution. It may return the following error:

Error    CS1705    Assembly 'WebAPI' with identity 'WebAPI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' uses 'Microsoft.AspNetCore.Mvc.Core, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' which has a higher version than referenced assembly 'Microsoft.AspNetCore.Mvc.Core' with identity 'Microsoft.AspNetCore.Mvc.Core, Version=2.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'    WebApiTest    C:\... 

The above error can be produced on a solution that uses two projects, namely WebApi and WebApiTest. 

The error above shows that the test project and API project are using a different version of Microsoft.AspNetCore.Mvc.Core. For example, the test project is using version 2.1.0.0 while the API project is using 2.1.1.0.

We can resolve this issue by updating the package reference in either of the projects to match. For example, we can update the WebApiTest project to use the higher version of the assembly (i.e., at 2.1.1.0) or downgrade the assembly version in the WebApi project to 2.1.0.0 depending on the need. 

We can issue a command like below in the package manager console targetting the right project. For example, the code below will update the assembly reference for Microsoft.AspNetCore.Mvc.Core on WebApiTest test to use version 2.1.1.0. This will then match the version of the same assembly in the WebApi project.  The following code 
Install-Package Microsoft.AspNetCore.Mvc.Core -Version 2.1.1.0 -ProjectName WebApiTest 

The argument '- ProjectName WebApiTest' name can be omitted if the correct project is selected in the dropdown that is available in the Package Manager Console window in the Visual Studio as shown in the figure below: 


Likewise, to update the WebApi project instead, the following command will do the trick:

Install-Package Microsoft.AspNetCore.Mvc.Core -Version 2.1.1.0 -ProjectName WebApiTest 
 
 

No comments:

Post a Comment