Edit

Share via


Modernize after upgrading to .NET from .NET Framework

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.

Missing APIs

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.

Windows Compatibility Pack

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

Web browser control

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.

App.config

.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:

  1. Remove the System.Configuration.ConfigurationManager NuGet package or library if referenced by your upgraded app.

  2. Add the Microsoft.Extensions.Configuration.Json NuGet package.

  3. Create a file named appsettings.json.

    1. Right-click on the project file in Solution Explorer and select Add > New Item.
    2. In the search box, enter json.
    3. Select the JavaScript JSON Configuration File template and set the Name to appsettings.json.
    4. Press Add to add the new file to the project.
  4. Set the appsettings.json file to copy to the output directory.

    In Solution Explorer, find the appsettings.json file and set the following Properties:

    • Build Action: Content
    • Copy to Output Directory: Copy always
  5. 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:

    • Create an internal static (Friend Shared in Visual Basic) member that can be accessed from anywhere in your app.
    • During startup, assign an instance to that member.

    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
    
  6. Update the rest of your code to use the new configuration APIs.

  7. 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.