dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.55k stars 10.05k forks source link

.NET 6 Web API not working without .NET Core 2.1 Runtime in the server. #41837

Closed ponkumarpandian closed 2 years ago

ponkumarpandian commented 2 years ago

We are using below nuget packages for our WebAPI project. Since .NET Core 2.1 support has been end, as per our organization policy we are suppose to remove the .NET Core 2.1 runtime from the server as this is consider as a vulnerability. But we uninstall the .NET core 2.1 version , application not working and getting 500 internal server error .

Project 'XXX.YYY.ABC.FFF.Service' has the following package references [net6.0]: Top-level Package Requested Resolved

Microsoft.AspNetCore.Authentication.JwtBearer 6.0.0 6.0.0 Microsoft.AspNetCore.Cors 2.2.0 2.2.0 Microsoft.AspNetCore.Mvc.NewtonsoftJson 6.0.0 6.0.0 Microsoft.EntityFrameworkCore 6.0.0 6.0.0 Microsoft.EntityFrameworkCore.Design 6.0.0 6.0.0 Microsoft.EntityFrameworkCore.Relational 6.0.0 6.0.0 Microsoft.EntityFrameworkCore.SqlServer 6.0.0 6.0.0 Microsoft.IdentityModel.Tokens 6.14.1 6.14.1 ncrontab 3.3.1 3.3.1 Serilog.Extensions.Logging.File 2.0.0 2.0.0 System.DirectoryServices.Protocols 6.0.0 6.0.0 System.Linq 4.3.0 4.3.0 System.Linq.Expressions 4.3.0 4.3.0

dotnet-issue-labeler[bot] commented 2 years ago

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

teo-tsirpanis commented 2 years ago

The CORS package is an old version and does not seem to be required. Have you tried removing it and running the application again?

Same with the two packages at the bottom. If you only target .NET 6 you don't need them.

ponkumarpandian commented 2 years ago

yes, I am checking, how about the "Microsoft.IdentityModel.Tokens" and "System.DirectoryServices.Protocols" it has dependencies with .NET Standard 2.0?

Tratcher commented 2 years ago

Microsoft.AspNetCore.Cors 2.2.0 2.2.0

This is likely your issue, remove that reference. The Tokens and Protocols packages should be fine.

ponkumarpandian commented 2 years ago

I have tried that as well and it did not help much. Problem with Microsoft.Identity.Client 4.44.0 which is indirectly referenced in Microsoft.EntityFrameworkCore.SqlServer 6.0.0 library. Here is the reference hierarchy,

Microsoft.EntityFrameworkCore.SqlServer==>Microsoft.Data.SqlClient 4.1.0==> Microsoft.Identity.Client 4.44.0

So Microsoft.Identity.Client 4.44.0 has .NET Core 2.1 framework dependency. Please refer the nuget for more details.

Please let me do you have any road map to remove 2.1 framework dependency?

teo-tsirpanis commented 2 years ago

So Microsoft.Identity.Client 4.44.0 has .NET Core 2.1 framework dependency. Please refer the nuget for more details.

You shouldn't be worried about libraries targeting unsupported .NET Core versions. A library targeting .NET Core 2.1 means it can run .NET Core 2.1 or newer. It is your main executable app that needs to target a sufficiently new framework version. And this package is getting regular updates which means that it is supported.

ponkumarpandian commented 2 years ago

So Microsoft.Identity.Client 4.44.0 has .NET Core 2.1 framework dependency. Please refer the nuget for more details.

You shouldn't be worried about libraries targeting unsupported .NET Core versions. A library targeting .NET Core 2.1 means it can run .NET Core 2.1 or newer. It is your main executable app that needs to target a sufficiently new framework version. And this package is getting regular updates which means that it is supported.

Yes, But our application is not working if we don't install .net core 2.1 Runtime. May be due the above reference to 2.1 core .

teo-tsirpanis commented 2 years ago

Can you give us the error message you are getting?

ponkumarpandian commented 2 years ago

Getting 500 internal error and when I check the event logs, it says .netcore 2.1 not found error.

davidfowl commented 2 years ago

@ponkumarpandian Can you paste the exact error message, or screen shot it. It's a bit ambiguous right now.

ponkumarpandian commented 2 years ago

@davidfowl Here is the details error message when run the api on the server with localhost url. I have attached the portion of my application dependency json content (highlighted in bold) which has a .NETCore2.1 framework dependency. There is no application log also generated since application couldn't start due to this error.

IIS Version : 8.1

Error: HTTP Error 500.21 - Internal Server Error Handler "aspNetCore" has a bad module "AspNetCoreModule" in its module list

Error-500

{
  "runtimeTarget": {
    "name": ".NETCoreApp,Version=v6.0",
    "signature": ""
  },

  "compilationOptions": {},
  "targets": {
    ".NETCoreApp,Version=v6.0": {
      "AAA.XXX.AAA.AAAA.Service/2.0.7": {
        "dependencies": {
          "DocumentFormat.OpenXml": "2.16.0",
          "Ical.Net": "4.2.0",
          "Microsoft.AspNetCore.Authentication.JwtBearer": "6.0.0",
          "Microsoft.AspNetCore.Mvc.NewtonsoftJson": "6.0.0",
          "Microsoft.EntityFrameworkCore": "6.0.0",
          "Microsoft.EntityFrameworkCore.Design": "6.0.0",
          "Microsoft.EntityFrameworkCore.Relational": "6.0.0",
          **"Microsoft.EntityFrameworkCore.SqlServer": "6.0.0",**
          "Microsoft.IdentityModel.Tokens": "6.14.1",
          "Serilog.Extensions.Logging.File": "2.0.0",
          "System.DirectoryServices.Protocols": "6.0.0",
          "NCrontab": "3.3.1"
        },
        "runtime": {
          "AAA.XXX.AAA.AAAA.Service.dll": {}
        }
      },
      "Microsoft.Identity.Client/4.21.1": {
        "runtime": {
          **"lib/netcoreapp2.1/Microsoft.Identity.Client.dll": {**
            "assemblyVersion": "4.21.1.0",
            "fileVersion": "4.21.1.0"
          }
        }
      },
      "Microsoft.IdentityModel.JsonWebTokens/6.10.0": {
        "dependencies": {
          "Microsoft.IdentityModel.Tokens": "6.14.1"
        },
        "runtime": {
          "lib/netstandard2.0/Microsoft.IdentityModel.JsonWebTokens.dll": {
            "assemblyVersion": "6.10.0.0",
            "fileVersion": "6.10.0.20330"
          }
        }
      }
}
ponkumarpandian commented 2 years ago

@davidfowl Here is my web.config and detailed error message. As I mentioned earlier Microsoft.Identity.Client still have .netcoreappp 2.1 reference due to that it got failed.

Please let me if you need any further information.

Hosting environment: UAT
Content root path: E:\SME Review\Prod\Services\Audit
Now listening on: http://127.0.0.1:12232
Application started. Press Ctrl+C to shut down.
warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3]
      Failed to determine the https port for redirect.
fail: XXX.ABC.QAD.AAA.Service.Controllers.AuditsController[0]
      The type initializer for 'Microsoft.Data.SqlClient.SqlAuthenticationProviderManager' threw an exception.
      System.TypeInitializationException: The type initializer for 'Microsoft.Data.SqlClient.SqlAuthenticationProviderManager' threw an exception.
       ---> System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Identity.Client, Version=4.21.1.0, Culture=neutral, PublicKeyToken=0a613f4dd989e8ae'. The system cannot find the file specified.
      File name: 'Microsoft.Identity.Client, Version=4.21.1.0, Culture=neutral, PublicKeyToken=0a613f4dd989e8ae'
         at Microsoft.Data.SqlClient.ActiveDirectoryAuthenticationProvider..ctor(String applicationClientId)
         at Microsoft.Data.SqlClient.SqlAuthenticationProviderManager..cctor()
         --- End of inner exception stack trace ---
         at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
         at Microsoft.Data.ProviderBase.DbConnectionPool.WaitForPendingOpen()
      --- End of stack trace from previous location ---
         at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternalAsync(Boolean errorsExpected, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternalAsync(Boolean errorsExpected, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternalAsync(Boolean errorsExpected, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenAsync(CancellationToken cancellationToken, Boolean errorsExpected)
         at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenAsync(CancellationToken cancellationToken, Boolean errorsExpected)
         at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(AsyncEnumerator enumerator, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(AsyncEnumerator enumerator, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
         at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
         at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
         at XXX.ABC.QAD.AAA.Service.Services.AuditService.GetMasterData(String masterName) in D:\a\1\s\src\XXX.ABC.QAD.AAA.Service\Services\AuditService.cs:line 671
         at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <remove name="X-Powered-By" />
      </customHeaders>
    </httpProtocol>
    <handlers>
      <remove name="aspNetCore" />
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
    </handlers>
    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false" requestTimeout="00:10:00">
      <environmentVariables>
        <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
      </environmentVariables>
    </aspNetCore>
  </system.webServer>
</configuration>
ponkumarpandian commented 2 years ago

Just wanted to give update on what I understood on this issue, I have manually upgraded the following packages to latest version and it is started working, Microsoft.EntityFrameworkCore.SqlServer - 6.0.0 --> 6.0.5 Microsoft.Data.SqlClient - 2.1.4 --> 4.1.0 Microsoft.Identity.Client - 4.21.1 --> 4.44.0

But the question is, when we upgrade Microsoft.EntityFrameworkCore.SqlServer to latest version, it's dependencies also should upgrade to the latest version. Please let me know what is the expected behaviour on this?

ponkumarpandian commented 2 years ago

Sorry I was wrong, I am still getting same error even though I upgraded to the latest one. I just cross verified my server had .net core 2.1.3 - Windows Server Hosting and because of that it worked . If I uninstall .net core 2.1.3 - Windows Server Hosting from my server, I am getting same error as mentioned before.

Can someone please let me know the work around? I am running out time , need to resolve as soon as possible otherwise my application need to be go down. It will be great if someone respond this issue quickly.

Here are the .NET Runtime installed on my server. Framework_installed

@davidfowl @teo-tsirpanis please help me to resolve this issue.

adityamandaleeka commented 2 years ago

If I uninstall .net core 2.1.3 - Windows Server Hosting from my server, I am getting same error as mentioned before.

There are two errors shown above, the one about the bad module and the one about SqlAuthenticationProviderManager. Which one are you seeing?

ponkumarpandian commented 2 years ago

@adityamandaleeka Bad module error resolved once I update the web.config with AspNetCoreModuleV2 model. After upgrading the package I am still facing the SqlAuthenticationProviderManager error due to .netcoreapp 2.1 dependency and my IIS server doesn't have .netcoreapp 2.1 version installed.

davidfowl commented 2 years ago

@ponkumarpandian Can you clarify what you mean by the netcoreapp2.1 dependency? What are you referring to and why you think it's relevant to the error?

ponkumarpandian commented 2 years ago

@davidfowl Please refer to the below screenshot, Microsoft.Identity.Client package reference in Microsoft.Data.SqlClient which is referenced in Microsoft.EntityFrameworkCore.SqlServer package. It is clearly shows Microsoft.Identity.Client has .netcoreapp 2.1 framework dependency. Due to this package has .netcoreapp2.1 framework dependency, our application not even starting since the application running on IIS onprime server where we don't have .netcore 2.1 framework installed. This server have only .NET 6 run time.

Please let me know if you need further information and help me to resolve this issue as soon as possible.

NETCore2 1_Dependancy

adityamandaleeka commented 2 years ago

It looks like the Microsoft.Identity.Client library only supports netcoreapp2.1 and net5.0-windows10.0.17763 (https://www.nuget.org/packages/Microsoft.Identity.Client/4.44.0). Unfortunately, there's nothing we (ASP.NET Core) can do about that.

Can you file this issue over at https://github.com/AzureAD/microsoft-authentication-library-for-dotnet so that they can add net6 support? .NET 5 is now out of support.

ponkumarpandian commented 2 years ago

@adityamandaleeka You mean if I target my application to .Net 5.0 and deploy the application to server without .netcore 2.1 framework, will it work?

ghost commented 2 years ago

This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.

See our Issue Management Policies for more information.