Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

Modernize after upgrading to .NET from .NET Framework

Feedback

In this article

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 theGitHub Copilot app modernization assistant 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 theMicrosoft.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 theMicrosoft.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, seeUse 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 theMicrosoft.Web.WebView2 NuGet package as modern web browser control replacement.

App.config

.NET Framework uses theApp.config file to load settings for your app, such as connection strings and log provider configuration. Modern .NET uses theappsettings.json file for app settings.

Tip

If you don't want to use theappsettings.json file, you can add theSystem.Configuration.ConfigurationManager NuGet package to your app and your code will compile and use theApp.config file.

Even thoughappsettings.json is the modern way to store and retrieve settings and connection strings, your app still has code that uses theApp.config file. When your app was migrated, theSystem.Configuration.ConfigurationManager NuGet package was added to the project so that your code using theApp.config file continues to compile.

As libraries upgrade to .NET, they modernize by supportingappsettings.json instead ofApp.config. For example, logging providers in .NET Framework that have been upgraded for .NET 6+ no longer useApp.config for settings. It's good for you to follow their direction and also move away from usingApp.config.

Support forappsettings.json is provided by theMicrosoft.Extensions.Configuration NuGet package.

Perform the following steps to use theappsettings.json file as your configuration provider:

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

  2. Add theMicrosoft.Extensions.Configuration.Json NuGet package.

  3. Create a file namedappsettings.json.

    1. Right-click on the project file inSolution Explorer and selectAdd >New Item.
    2. In the search box, enterjson.
    3. Select theJavaScript JSON Configuration File template and set theName toappsettings.json.
    4. PressAdd to add the new file to the project.
  4. Set theappsettings.json file to copy to the output directory.

    InSolution Explorer, find theappsettings.json file and set the followingProperties:

    • 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 theApp.xaml.cs file for global setup and a Windows Forms app uses theProgram.Main method for startup. Regardless, you need to do two things at startup:

    • Create aninternal 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 namedConfig, assigns it an instance in theMain 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.ConfigurationModule 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 SubEnd Module
  6. Update the rest of your code to use the new configuration APIs.

  7. Delete theApp.config file from the project.

    Caution

    Make sure that your app runs correctly without theApp.config file. Back up theApp.config file through source control or by copying the file elsewhere. After you've thoroughly tested your app, delete theApp.config file.

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

  • Last updated on

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?