Events
17 Mar, 11 pm - 21 Mar, 11 pm
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
In this article, you'll learn about different ways you can modernize your app after it's been upgraded from .NET Framework to .NET. Use the .NET Upgrade Assistant tool to upgrade your app to .NET.
When upgrading a .NET Framework app, you'll most likely have some incompatibilities. This is because .NET Framework is a Windows-only technology and .NET is a cross-platform technology. Some libraries aren't. For example, .NET doesn't provide out-of-the-box APIs to access the Windows Registry like .NET Framework did. Support for the Windows Registry is provided by the Microsoft.Win32.Registry
NuGet package. Many .NET Framework-specific libraries have been ported to .NET or .NET Standard, and are hosted on NuGet. If you find a missing reference in your project, search NuGet.
If after migration you have some dependencies on .NET Framework APIs that are not supported on your new version of .NET, you might find them in the Microsoft.Windows.Compatibility
NuGet package. It adds around 20,000 APIs to your .NET project, significantly increasing the API set available to your project. These APIs include Windows-only APIs such as those related to Windows Management Instrumentation (WMI) and the Windows EventLog. For more information, see Use the Windows Compatibility Pack to port code to .NET.
Projects that target a Windows desktop technology, such as Windows Presentation Foundation or Windows Forms, may include a web browser control. The web browser control provided was most likely designed prior to HTML5 and other modern web technologies and is considered obsolete. Microsoft publishes the Microsoft.Web.WebView2
NuGet package as modern web browser control replacement.
.NET Framework uses the App.config file to load settings for your app, such as connection strings and log provider configuration. Modern .NET uses the appsettings.json file for app settings. The CLI version of the Upgrade Assistant handles converting App.config files to appsettings.json, but the Visual Studio extension doesn't.
Tip
If you don't want to use the appsettings.json file, you can add the System.Configuration.ConfigurationManager
NuGet package to your app and your code will compile and use the App.config file.
Even though appsettings.json is the modern way to store and retrieve settings and connection strings, your app still has code that uses the App.config file. When your app was migrated, the System.Configuration.ConfigurationManager
NuGet package was added to the project so that your code using the App.config file continues to compile.
As libraries upgrade to .NET, they modernize by supporting appsettings.json instead of App.config. For example, logging providers in .NET Framework that have been upgraded for .NET 6+ no longer use App.config for settings. It's good for you to follow their direction and also move away from using App.config.
Support for appsettings.json is provided by the Microsoft.Extensions.Configuration
NuGet package.
Perform the following steps to use the appsettings.json file as your configuration provider:
Remove the System.Configuration.ConfigurationManager
NuGet package or library if referenced by your upgraded app.
Add the Microsoft.Extensions.Configuration.Json
NuGet package.
Create a file named appsettings.json.
json
.Set the appsettings.json file to copy to the output directory.
In Solution Explorer, find the appsettings.json file and set the following Properties:
In the startup code of your app, you need to load the settings file.
The startup code for your app varies based on your project type. For example, a WPF app uses the App.xaml.cs
file for global setup and a Windows Forms app uses the Program.Main
method for startup. Regardless, you need to do two things at startup:
internal static
(Friend Shared
in Visual Basic) member that can be accessed from anywhere in your app.The following example creates a member named Config
, assigns it an instance in the Main
method, and loads a connection string:
using Microsoft.Extensions.Configuration;
internal class Program
{
internal static IConfiguration Config { get; private set; }
private static void Main(string[] args)
{
Config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
// Use the config file to get a connection string.
string? myConnectionString = Config.GetConnectionString("database");
// Run the rest of your app.
}
}
Imports Microsoft.Extensions.Configuration
Module Program
Private _config As IConfiguration
' Shared not required since Program is a Module
Friend Property Config As IConfiguration
Get
Return _config
End Get
Private Set(value As IConfiguration)
_config = value
End Set
End Property
Sub Main(args As String())
Config = New ConfigurationBuilder() _
.AddJsonFile("appsettings.json") _
.Build()
' Use the config file to get a connection string
Dim myConnectionString As String = Config.GetConnectionString("database")
' Run the rest of your app
End Sub
End Module
Update the rest of your code to use the new configuration APIs.
Delete the App.config file from the project.
Caution
Make sure that your app runs correctly without the App.config file. Back up the App.config file through source control or by copying the file elsewhere. After you've thoroughly tested your app, delete the App.config file.
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Events
17 Mar, 11 pm - 21 Mar, 11 pm
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Module
Modernize ASP.NET Framework to ASP.NET Core with the .NET Upgrade Assistant - Training
In this module, you'll learn to when, why, and how to modernize an ASP.NET Framework app to ASP.NET Core using the Upgrade Assistant.
Certification
Microsoft Certified: Azure Developer Associate - Certifications
Build end-to-end solutions in Microsoft Azure to create Azure Functions, implement and manage web apps, develop solutions utilizing Azure storage, and more.
Documentation
Port from .NET Framework to .NET 7 - .NET Core
Understand the porting process and discover tools you might find helpful when porting a .NET Framework project to .NET 7.
Use the Windows Compatibility Pack to port code - .NET Core
Learn about the Windows Compatibility Pack and how can you use it to port existing .NET Framework code to .NET.
Upgrade a .NET Framework WinForms app to .NET - Windows Forms .NET
Learn how to upgrade a .NET Framework (or previous .NET) Windows Forms application to .NET with the .NET Upgrade Assistant and Visual Studio.