Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeLoadException error (DependencyInjection) with latest EFCore #1390

Closed
leodip opened this issue Oct 12, 2022 · 27 comments · Fixed by #1395
Closed

TypeLoadException error (DependencyInjection) with latest EFCore #1390

leodip opened this issue Oct 12, 2022 · 27 comments · Fixed by #1395
Labels
waiting for customer Waiting for customer action with repro Issue with repro

Comments

@leodip
Copy link

leodip commented Oct 12, 2022

Hi,

I noticed that if I upgrade my EF Core libraries to latest (6.0.10), I get an error with coverlet:

Data collector 'XPlat code coverage' message: [coverlet]System.TypeLoadException: Could not load type 'Microsoft.Extensions.DependencyInjection.ServiceCollection' from assembly 'Microsoft.Extensions.DependencyInjection, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
   at Coverlet.Collector.DataCollection.CoverletCoverageCollector.GetDefaultServiceCollection(TestPlatformEqtTrace eqtTrace, TestPlatformLogger logger, String testModule)
   at Coverlet.Collector.DataCollection.CoverletCoverageCollector.OnSessionStart(Object sender, SessionStartEventArgs sessionStartEventArgs) in /_/src/coverlet.collector/DataCollection/CoverletCoverageCollector.cs:line 132.

I'm trying to upgrade Microsoft.EntityFrameworkCore from 6.0.9 to 6.0.10. My project is latest .NET 6.

Is this happening to anyone else? Is there a fix?

@cheesi
Copy link

cheesi commented Oct 12, 2022

Yes, we have the same issue, I can confirm that we also updated all Microsoft & EF Core packages today, however, I haven't check if it is related as our pipelines didn't break. But if @leodip has it already figured out, that EF Core is the issue, it is most likely same reason for us.

@Krashlog
Copy link

Actually, I think it's Microsoft.Extensions.DependencyInjection 6.0.1 that is problematic (as a dependency of both EF Core and coverlet). Downgrading to 6.0.0 solved the problem in our Azure pipelines.

@KlemensGenetec
Copy link

Got the same issue. When calling dotnet test with --collect:XPlat Code Coverage I get the error

Data collector 'XPlat code coverage' message: [coverlet]System.TypeLoadException: Could not load type 'Microsoft.Extensions.DependencyInjection.ServiceCollection' from assembly 'Microsoft.Extensions.DependencyInjection, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. at Coverlet.Collector.DataCollection.CoverletCoverageCollector.GetDefaultServiceCollection(TestPlatformEqtTrace eqtTrace, TestPlatformLogger logger, String testModule) at Coverlet.Collector.DataCollection.CoverletCoverageCollector.OnSessionStart(Object sender, SessionStartEventArgs sessionStartEventArgs) in /_/src/coverlet.collector/DataCollection/CoverletCoverageCollector.cs:line 132.

It must be related to DependencyInjection 6.0.0 not being present in our project anymore. Funnily enough when using CollectCoverage=true in dotnet test, which causes it to use msbuild it didnt have that issue.

@daveMueller
Copy link
Collaborator

Could somebody try to provide a simple repro for that? I just tried it myself with a simple console app referencing Microsoft.Extensions.DependencyInjection 6.0.1 but didn't face this issue.

@daveMueller daveMueller added the needs repro Needs repro to be investigated, cannot repro in local label Oct 17, 2022
@KlemensGenetec
Copy link

Also tried now but it doesn't seem to be that easy to reproduce it. I just included EntityFrameworkCore as I did in the other project and used WebApplicationFactory for testing as well but i couldn't see the issue either.
So there seems to be more to it than I thought.

@candritzky
Copy link

candritzky commented Oct 18, 2022

Same here and definitely related to .NET 6.0.10, but not directly to EF Core.

Seems like the ServiceCollection class was been moved from Microsoft.Extensions.DependencyInjection.dll into Microsoft.Extensions.DependencyInjection.Abstractions.dll.
But this happened a long time ago, long before .NET 6 was released.
See dotnet/runtime@ad7455c

Maybe it happens because coverlet.console still targets net5.0.
See

<TargetFramework>net5.0</TargetFramework>

@candritzky
Copy link

Happens because of the updated Microsoft.Extensions.DependencyInjection NuGet package (now version 6.0.1). This results in a newer Microsoft.Extensions.DependencyInjection.dll being copied to the bin folder (still assembly version 6.0.0.0).

If you either delete the DLL from the bin folder before running the tests, or if you manually copy Microsoft.Extensions.DependencyInjection.Abstractions.dll (from NuGet package 6.0.0 which is still latest) to the bin folder, then code coverage is working again.

Really strange...

@KlemensGenetec
Copy link

Got a sample working where i got the error. Hopefully its not just on my machine. For some reason it is the combination of App insights and EF.

Then did the following command: dotnet test "--collect:XPlat Code Coverage" --configuration Release --logger trx --results-directory result

console-app-master.zip

@cheesi
Copy link

cheesi commented Oct 18, 2022

Hey!
I downloaded the sample from @KlemensGenetec and can confirm, I got the same error. So it seems to be a machine independent working reproduction.

❯   dotnet test "--collect:XPlat Code Coverage" --configuration Release --logger trx --results-directory result
  Determining projects to restore...
  Restored C:\Users\Christian\Downloads\console-app-master\src\TemplateApp.ConsoleApp\TemplateApp.ConsoleApp.csproj (in 495 ms).
  Restored C:\Users\Christian\Downloads\console-app-master\tests\TemplateApp.Application.Tests\TemplateApp.Application.Tests.csproj (in 1,56 sec).
  TemplateApp.ConsoleApp -> C:\Users\Christian\Downloads\console-app-master\src\TemplateApp.ConsoleApp\bin\Release\net6.0\TemplateApp.ConsoleApp.dll
  TemplateApp.Application.Tests -> C:\Users\Christian\Downloads\console-app-master\tests\TemplateApp.Application.Tests\bin\Release\net6.0\TemplateApp.Application.Tests.dll
Test run for C:\Users\Christian\Downloads\console-app-master\tests\TemplateApp.Application.Tests\bin\Release\net6.0\TemplateApp.Application.Tests.dll (.NETCoreApp,Version=v6.0)
Microsoft (R) Test Execution Command Line Tool Version 17.3.1 (x64)
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
Data collector 'XPlat code coverage' message: [coverlet]System.TypeLoadException: Could not load type 'Microsoft.Extensions.DependencyInjection.ServiceCollection' from assembly 'Microsoft.Extensions.DependencyInjection, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
   at Coverlet.Collector.DataCollection.CoverletCoverageCollector.GetDefaultServiceCollection(TestPlatformEqtTrace eqtTrace, TestPlatformLogger logger, String testModule)
   at Coverlet.Collector.DataCollection.CoverletCoverageCollector.OnSessionStart(Object sender, SessionStartEventArgs sessionStartEventArgs) in /_/src/coverlet.collector/DataCollection/CoverletCoverageCollector.cs:line 132.
test

Results File: C:\Users\Christian\Downloads\console-app-master\result\Christian_WORKSTATION_2022-10-18_19_02_04.trx

Passed!  - Failed:     0, Passed:     1, Skipped:     0, Total:     1, Duration: 207 ms - TemplateApp.Application.Tests.dll (net6.0)

@Krashlog
Copy link

I can repro too, using @KlemensGenetec sample.

Simply removing the AppInsight nuget package is enough for the tests to work again.

@rodchenkov
Copy link

I have the same issue, can someone suggest a solution for pipeline?

@KlemensGenetec
Copy link

You can use something similar to dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover --configuration Release --logger trx --results-directory result

Then you need to make sure that <IsTest>true</IsTest> is in the test project and probably add <PackageReference Include="coverlet.msbuild" Version="3.1.2"> to the test project too.

That will work if you have msbuild installed.

@rodchenkov
Copy link

@KlemensGenetec thanks for suggestion, but I'm using linux builds and this command:
dotnet test --configuration Release --no-restore --collect:"XPlat Code Coverage" --settings .runsettings -- RunConfiguration.DisableAppDomain=true
Looks like we need update of coverlet package to search 'Microsoft.Extensions.DependencyInjection.ServiceCollection' in Microsoft.Extensions.DependencyInjection.Abstractions.dll to a new path.

@rodchenkov
Copy link

I agree with @Krashlog about "Microsoft.ApplicationInsights.AspNetCore". If I remove it from reference than all start working. Also I see Microsoft.Extensions.DependencyInjection.dll after removing "Microsoft.ApplicationInsights.AspNetCore"

@rodchenkov
Copy link

Shall we go to "Microsoft.ApplicationInsights.AspNetCore" and create issue there?

@daveMueller
Copy link
Collaborator

daveMueller commented Oct 19, 2022

@KlemensGenetec thanks for the repro. I'm not 100% sure what exactly the problem is yet, but it is definitely related to the change @candritzky mentioned here:

Seems like the ServiceCollection class was been moved from Microsoft.Extensions.DependencyInjection.dll into Microsoft.Extensions.DependencyInjection.Abstractions.dll. But this happened a long time ago, long before .NET 6 was released. See dotnet/runtime@ad7455c

Coverlet currently references both Microsoft.Extensions.DependencyInjection and Microsoft.Extensions.DependencyInjection.Abstractions which somehow leads to incompatibilities with the ServiceCollection in the new version.

I compiled a new version of coverlet that at least solved this issue for the repro. I would be happy if someone could give it a try. 😄

coverlet.3.1.3-preview.16.zip

@daveMueller daveMueller added with repro Issue with repro waiting for customer Waiting for customer action and removed needs repro Needs repro to be investigated, cannot repro in local labels Oct 19, 2022
@cheesi
Copy link

cheesi commented Oct 19, 2022

Hey @daveMueller !
I tested it with one of our services and it seems to work!

Microsoft (R) Test Execution Command Line Tool Version 17.3.1 (x64)
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...
A total of 1 test files matched the specified pattern.

Attachments:
  C:\Users\Christian\source\repos\Service\tests\Service.A.Tests\TestResults\e33618a8-c236-4998-937b-7d7ba0f2e6c3\coverage.cobertura.xml
Passed!  - Failed:     0, Passed:    47, Skipped:     0, Total:    47, Duration: 323 ms - Service.A.Tests.dll (net6.0)

Attachments:
  C:\Users\Christian\source\repos\Service\tests\Service.B.Tests\TestResults\8f892410-9325-47e0-8023-be81f6b051b6\coverage.cobertura.xml
Passed!  - Failed:     0, Passed:     6, Skipped:     0, Total:     6, Duration: 370 ms - Service.B.Tests.dll (net6.0)

Attachments:
  C:\Users\Christian\source\repos\Service\tests\Service.C.Tests\TestResults\af7d88ac-ec8f-4cdd-9d6f-93f50f918b12\coverage.cobertura.xml
Passed!  - Failed:     0, Passed:    41, Skipped:     0, Total:    41, Duration: 2 s - Service.C.Tests.dll (net6.0)

And I get a coverage.opencover.xml file:
image

@MarcoK80
Copy link

How fast can this be released? We have only 1 week to get this in or we need to downgrade Microsoft NuGets again. Thx

@daveMueller
Copy link
Collaborator

@cheesi cool thanks. I'll create a PR for this and try to discuss it with the other maintainers. @MarcoK80 a release in the next week is really unlikely. But maybe we can get a solution merged which then can be consumed by referencing our nightly build version.

@bhugot
Copy link

bhugot commented Oct 25, 2022

Hello hope we can have the fix soon but I find it really strange that you have any dependency risk with the instrumented code and library .

@petli
Copy link
Collaborator

petli commented Oct 27, 2022

Merged so will get into the nightly builds now:
https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/ConsumeNightlyBuild.md

@rodchenkov
Copy link

Sorry, do you have any plans when it will be released?

@petli
Copy link
Collaborator

petli commented Oct 27, 2022

Ping @MarcoRossignoli

@MarcoRossignoli
Copy link
Collaborator

MarcoRossignoli commented Oct 27, 2022

By the end of the week I should be able to release a minor release

@MarcoK80
Copy link

By the end of the week I should be able to release a patch release

Which is very good and helps us

@MarcoRossignoli
Copy link
Collaborator

MarcoRossignoli commented Oct 29, 2022

3.2.0 released

@KrzysztofPajak
Copy link

@MarcoRossignoli Thanks, everythings works properly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting for customer Waiting for customer action with repro Issue with repro
Projects
None yet