Transitioning from .NET Framework to .NET Core

Introduction

Today marks a significant phase in our software development journey as we embark on the transition from .NET Framework to .NET Core.

This decision was prompted by the need for greater flexibility, performance improvements, and cross-platform compatibility.

In this blog, we will observe the key changes and considerations as we migrate our project.

Project Structure

One of the initial steps is to update the project structure. .NET Core follows a simplified project file format (*.csproj) compared to the complex .csproj files in .NET Framework.

Here's an example:

.NET Framework (.csproj)

<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <!-- ... -->
</Project>

.NET Core (.csproj)

<Project Sdk="Microsoft.NET.Sdk">
    <!-- ... -->
</Project>

Dependencies and Packages

.NET Core uses the new PackageReference format for managing dependencies, providing a cleaner and more modular approach. Update your packages.config to the new format:

.NET Framework (packages.config)

<packages>
    <package id="Newtonsoft.Json" version="12.0.3" targetFramework="net461" />
    <!-- ... -->
</packages>

.NET Core (.csproj)

<ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
    <!-- ... -->
</ItemGroup>

Target Framework

Update the target framework in your project file to the appropriate version of .NET Core.

Let's have a look at example

.NET Framework (.csproj)

<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>

.NET Core (.csproj)

<TargetFramework>netcoreapp3.1</TargetFramework>

API Changes

Be prepared for API changes and deprecations. Some APIs in .NET Framework may have equivalent functionality but with different method names or signatures in .NET Core.

For example, the System.Web namespace is no longer available in .NET Core.

Configuration

Configuration handling has been revamped in .NET Core. Update your code to use the new ConfigurationBuilder

// .NET Framework
var value = ConfigurationManager.AppSettings["key"];

// .NET Core
var value = configuration["key"];

Logging

.NET Core introduces a more flexible and extensible logging system. Update your logging code accordingly

// .NET Framework
var logger = LogManager.GetCurrentClassLogger();

// .NET Core
var logger = LoggerFactory.Create(builder => builder.AddConsole()).CreateLogger<MyClass>();

Cross-platform Considerations

If your application needs to run on different platforms, ensure that your code is platform-agnostic. Use Path.Combine instead of hardcoding directory separators and be mindful of case sensitivity in file paths.

NuGet Package Compatibility

Verify that the NuGet packages you depend on are compatible with .NET Core. Some packages may have .NET Core versions available, while others may require finding alternative packages.

Conclusion

Transitioning from .NET Framework to .NET Core is a comprehensive process that requires careful planning and execution.

These outlined changes and considerations serve as a starting point for a successful migration.

Regular testing and collaboration within the development team are crucial to identifying and resolving any issues that may arise during this transformative journey.