Search code examples
c#asp.netasp.net-coreextension-methodsdowngrade

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


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>

Solution

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