In the previous post, I covered the steps required to migrate your project to the new format. In this post, I am going to move to the next stage and cover how you adapt the solution to target multiple frameworks.
The first step is to modify the project and specify which frameworks you want to target. This is a simple change, just modify the <TargetFramework> element to be <TargetFrameworks> and then specify the frameworks you wish to target.
After the project as been modified you will also need to to update the package references ensuring they target the correct framework. This is straightforward, simply add a condition to the parent <ItemGroup>.
<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'"> <PackageReference Include="EPiServer.CMS.UI.Core" Version="[12.0.3,13)" /> <PackageReference Include="EPiServer.Framework.AspNetCore" Version="[12.0.3,13)" /> <PackageReference Include="EPiServer.Framework" Version="[12.0.3,13)" /> <PackageReference Include="Microsoft.AspNetCore.Http" Version="2.0" /> <PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.0" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0" /> </ItemGroup>
There may also be orther sections of your project file that will also require you to use these condition clauses.
After changing the project to target multiple frameworks you will get compilation errors, you will need to fix these by creating different implementations of you code and wrapping each implementation with a preprocessor statement to indicate which framework the code is targeting.
#if NET461 // Code specific for .net framework 4.6.1 #elif NET5_0 // Code specific for .net 5.0 #elif NET5_0_OR_GREATER // Code specific for .net 5.0 (or greater) #else // Code for anything else #endif
You may just need to alter a couple of lines within a class, or in some cases you will need to deliver a completely different approach. A good example would be Middleware replacing a .Net Framework HTTPModule.
Wrapping it all up
Everyones journey whist converting their module will differ. The type of module, whether it has a UI etc will detemine the complexity.
Whilst you are modifying the code base I would strongly recommend :
- Keep the the ‘DRY’ principle and refactor you code when necessary so that you are not repeating sections of code.
- If you have an interface that uses WebForms then it is probably better to replace this with an interface that works for all the different frameworks rather than trying to maintain two different interfaces.
I hope this post helps you migrate your project.