1
votes

I am creating one project in Asp.net MVC 4.7.1 latest version and want to reuse that solution as class library dll in other projects also. But other projects where we are using this dll not support latest version (4.6). When am trying to use the class library in other version solutions it throws below error.

(0): error CS1705: Assembly 'ProjectName(dll name), Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' uses 'System.Web.Mvc, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' which has a higher version than referenced assembly 'System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

I need to create a solution with multiple version support while using it is as a dll.

1

1 Answers

0
votes

You need to target multiple frameworks in your project;

  1. Right click on project name and click "Edit .cproj file"
  2. add ( s ) to target framework tag so it becomes
<TargetFrameworks>...</TargetFrameworks>
  1. specify your target frameworks (see docs for all versions):
<TargetFrameworks>net472;net48;netcoreapp2.0;netcoreapp2.1;netcoreapp2.2</TargetFrameworks>
  1. if your library will have any references, you need to specify manually all the references for each version:
<ItemGroup Condition=" '$(TargetFramework)' == 'net471' ">
  <Reference Include="System.Net" />
</ItemGroup>

or specify reference for multiple versions:

<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.0' || '$(TargetFramework)' == 'netcoreapp2.1' || '$(TargetFramework)' == 'netcoreapp2.2' ">
  <PackageReference Include="Microsoft.AspNetCore.Mvc.TagHelpers" Version="1.0.0" />
</ItemGroup>

or specify package reference for all target frameworks:

<ItemGroup>
  <PackageReference Include="Microsoft.AspNetCore.Localization" Version="1.0.0" />
</ItemGroup>

you can specify the min required version and the framework will install the latest compatible version. e.g. below we specify Localization package min version as 1.0.0 but when it will be installed on .Net Core 2.2 it will install the newest compatible version, for more details about versioning see the version ranges and wild chars

  1. last but not least; in you code you need to check for target framework specific cases where you need to use compatible code line with each version.

for example to get culture info you need to specify different code for different .Net Core versions:

#if NETCOREAPP1_0
    var culture = new CultureInfo("en-US");
#else
    var culture = CultureInfo.GetCultureInfo("en-US");
#endif

    _logger.LogInformation($"{culture.Name}");

if you are using visual studio, you will see a dropdown navigation for all target frameworks, you can use it to switch between target frameworks and check your code compatibility.