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

5

12

I had to change my project from .NET Core 2.1 to .NET 4.7.1 and I fixed almost all errors except the following that are still eluding me

  • 'IApplicationBuilder' does not contain a definition for 'UseHsts' and no extension method 'UseHsts' accepting a first argument of type 'IApplicationBuilder' could be found (are you missing a using directive or an assembly reference?)

  • 'IApplicationBuilder' does not contain a definition for 'UseAuthentication' and no extension method 'UseAuthentication' accepting a first argument of type 'IApplicationBuilder' could be found (are you missing a using directive or an assembly reference?)

  • 'IApplicationBuilder' does not contain a definition for 'UseCookiePolicy' and no extension method 'UseCookiePolicy' accepting a first argument of type 'IApplicationBuilder' could be found (are you missing a using directive or an assembly reference?)

  • 'IApplicationBuilder' does not contain a definition for 'UseHttpsRedirection' and no extension method 'UseHttpsRedirection' accepting a first argument of type 'IApplicationBuilder' could be found (are you missing a using directive or an assembly reference?)

  • 'IApplicationBuilder' does not contain a definition for 'UseSession' and no extension method 'UseSession' accepting a first argument of type 'IApplicationBuilder' could be found (are you missing a using directive or an assembly reference?)

  • 'IApplicationBuilder' does not contain a definition for 'UseStaticFiles' and no extension method 'UseStaticFiles' accepting a first argument of type 'IApplicationBuilder' could be found (are you missing a using directive or an assembly reference?)

  • 'IServiceCollection' does not contain a definition for 'AddAuthentication' and no extension method 'AddAuthentication' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?)

  • 'IServiceCollection' does not contain a definition for 'AddSession' and no extension method 'AddSession' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?)

I am using Microsoft.AspNetCore.Builder and Microsoft.Extensions.DependencyInjection. What else do I need to use/install/add to get IApplicationBuilder and IServiceCollection to work?

In my WebApi.csproj file I changed the target framework from netcoreapp2.1 to net471.

Old:

<PropertyGroup>
  <TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

New:

<PropertyGroup>
  <TargetFramework>net471</TargetFramework>
</PropertyGroup>
Dynamotor answered 27/7, 2018 at 7:34 Comment(4)
Just curious - I've got several projects build on net-core-2.1 (actually, they are net-standard-2.0 mostly and the entry point assemblies are net-core-2.1) and running on windows on net-4.7.1. Why do you have to port it "down"? Can't you just run them? "net core app" is just a bunch of dlls bundled as nugets, no more special than newtonsoft.json / etc, there should not be any problem running then on net471. If the size matters (netcoreapp is pretty large), you can skip the 'all' assembly and manually pick specific ones and cut the weight several times.. What was the problem in your case then?Verdugo
@Dynamotor the packages are .NET Standard 2.0 so they can be used in the full framework. You should probably move your code to .NET Standard libraries and leave only essential configuration to runtime-specific projects.Rondel
@Dynamotor btw ASP.NET Core itself isn't tied to .NET Core. You can use it for Full framework sites too by selecting the appropriate runtime when you create a Web App project. Perhaps you can just change netcoreapp2.0 to net471 in the csproj file as well. I've used that a lot with class libraries, console projects but not with ASP.NET Core projects yetRondel
I'm trying to from .NET Core 3.1 to .NET 4.7.2. how to replace HostBuilder.CreateDefaultBuilder in Program.cs file ?Sappanwood
D
25

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.

Dynamotor answered 30/7, 2018 at 6:21 Comment(1)
Do you have the versions?Blender
L
8

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

  • Microsoft.AspNetCore.Session
  • Microsoft.AspNetCore.Authentication
Lather answered 27/7, 2018 at 10:23 Comment(1)
Don't works in netframework 461Blender
R
3

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

Rondel answered 27/7, 2018 at 7:53 Comment(0)
H
1

Try to add "Microsoft.aspNetCore.App" to your *.csproj file ,this should solve the problem .

<ItemGroup>
    <FrameworkReference Include="Microsoft.aspNetCore.App" />
</ItemGroup>
Hughie answered 9/7, 2021 at 17:35 Comment(0)
B
0

Please install Microsoft.AspNetCore.ResponseCaching from nuget package. It can fix your problem. After that described functionality will be available under .net framework.

Bloem answered 4/2, 2021 at 8:45 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.