How to use IApplicationBuilder and IServiceCollection when downgrading from .NET Core 2.1 to .NET 4.7.1?

Following Rik's answer, I searched for more packages and found I had to add all of the following NuGet Packages:

  • Microsoft.AspNetCore.Authentication
  • Microsoft.AspNetCore.Session
  • Microsoft.AspNetCore.HttpsPolicy
  • Microsoft.AspNetCore.CookiePolicy
  • Microsoft.AspNetCore.StaticFiles

After I did that I got no more error messages.


The 'AddSession' and 'UseAuthentication' errors can be fixed by using the following nuget packages.

  • Microsoft.AspNetCore.Session
  • Microsoft.AspNetCore.Authentication

First of all, switching to 4.7.1 isn't downgrading, it's moving to a different platform. Something you probably don't need to do, unless you want to reuse the code for a Winforms or WPF application.

The Microsoft.Extensions.* packages target .NET Standard 2.0, not just Core, so you can use them in the Full framework as well.

The packages and classes aren't tied to ASP.NET either, except hosting. I'm using them in console applications.

It also means that if your class libraries target .NET Standard 2.0 they can be used by both platforms without changing the target. Perhaps you could move most of the code to .NET Standard 2.0 libraries and leave just the configuration to runtime-specific projects

You don't strictly need hosting to use all the other extensions, although it does provide a convenient API similar to the ASP.NET Core code. You can write your own Startup class with Configure etc methods and call them explicitly. In the end, what you need is access to the IServiceCollection so you can get configured services and run them.

You can add a generic .NET host by using the Microsoft.Extensions.Hosting package. Apart from the common API it adds the ability to host long-running services to non-ASP.NET Core projects.

This blog post shows how you can use the Hosting package to create a console application that starts a long-running service, similar to a Windows service or daemon, eg :

public static async Task Main(string[] args)
{
    var hostBuilder = new HostBuilder()
        .ConfigureServices((hostContext, services) =>
        {
            services.AddSingleton<IBusControl>(serviceProvider =>
            {
                return MassTransit.Bus.Factory.CreateUsingRabbitMq(cfg =>
                {
                    var host = cfg.Host(new Uri("rabbitmq://localhost"), h =>
                    {
                        h.Username("guest");
                        h.Password("guest");
                    });
                });
            });
            services.AddScoped<IHostedService, MassTransitHostedService>();
        });

    await hostBuilder.RunConsoleAsync();
}

The .NET Generic Host goes into more detail and shows more examples of logging, DI, configuration etc.

UPDATE

ASP.NET Core is not tied to .NET Core. You can use it in Full Framework projects as well, simply by changing the target runtime in the Project creationg dialog