Skip to content

Dotnet test error during creating TestServer after updating to .NET 5 RC2 #26882

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

Closed
WojciechNagorski opened this issue Oct 14, 2020 · 8 comments
Assignees
Labels
area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug. Done This issue has been fixed
Milestone

Comments

@WojciechNagorski
Copy link

Describe the bug

After updating .NET Core 3.1 to .NET 5 RC2 (and all nugets) I got an exception during running tests from the console.

But the exception doesn't occur when I run:

  • run the same test from Resharper or Rider the test passed.
  • the same code from the Web application.

The problem occurs only when I run test from the console using the dotnet test command, so I think the problem is related to 'dotnet test' from .NET 5 or the APS package has some issue.

To Reproduce

Minimalistic repro can be found there https://github.com/WojciechNagorski/AspDotnet5Issue
The exception can be reproduced using the command:

dotnet test .\test.net5

I created also a test for .NET Core 3.1 that presents that the problem doesn't exist for the old version of .NET.

dotnet test .\test.netcore3.1

Description
I just create a new ASP project:

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();
    }

with almost default startup:

 public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetupValidation();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync("Hello World!");
                });
            });
        }
    }

where SetupValidation() method is an extension that registers all validators in all assemblies:

        public static IMvcBuilder SetupValidation(this IMvcBuilder builder)
        {
            static void RegisterValidators(FluentValidationMvcConfiguration conf)
            {
                foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies().Where(a => !a.IsDynamic && a.DefinedTypes.Any(t => typeof(IRequestValidator).GetTypeInfo().IsAssignableFrom(t) && !t.IsAbstract)))
                {
                    conf.RegisterValidatorsFromAssembly(assembly);
                }
            }

            return builder.AddFluentValidation(RegisterValidators);
        }

The last step is the test method where I just create TestServer.

        [Fact]
        public void Test1()
        {
            TestServer server = new TestServer(Program.CreateHostBuilder(new string[] { })
                .ConfigureTestServices(collection =>
                {
                    
                }));
            var client = server.CreateClient();
            
            Assert.NotNull(client);
        }

Exceptions

  Error Message:
   System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types.
Could not load type 'Microsoft.AspNetCore.Components.Forms.ReadRequest' from assembly 'Microsoft.AspNetCore.Components.Web, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' because it contains an object field at offset 4 that is incorrectly aligned or overlapped by a non-object field.
  Stack Trace:
     at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
   at test.MvcBuilderExtensions.<>c.<SetupValidation>b__0_1(Assembly a) in C:\work\test\test\MvcBuilderExtensions.cs:line 17
   at System.Linq.Enumerable.WhereArrayIterator`1.MoveNext()
   at test.MvcBuilderExtensions.<SetupValidation>g__RegisterValidators|0_0(FluentValidationMvcConfiguration conf) in C:\work\test\test\MvcBuilderExtensions.cs:line 17
   at FluentValidation.AspNetCore.FluentValidationMvcExtensions.AddFluentValidation(IMvcBuilder mvcBuilder, Action`1 configurationExpression) in C:\Projects\FluentValidation\src\FluentValidation.AspNetCore\FluentValidationMvcExtensions.cs:line 77
   at test.MvcBuilderExtensions.SetupValidation(IMvcBuilder builder) in C:\work\test\test\MvcBuilderExtensions.cs:line 23
   at test.Startup.ConfigureServices(IServiceCollection services) in C:\work\test\test\Startup.cs:line 18
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.InvokeCore(Object instance, IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass9_0.<Invoke>g__Startup|0(IServiceCollection serviceCollection)
   at Microsoft.AspNetCore.Hosting.StartupLoader.ConfigureServicesDelegateBuilder`1.<>c__DisplayClass15_1.<BuildStartupServicesFilterPipeline>g__InvokeStartup|1(IServiceCollection serviceCollection)
   at Microsoft.AspNetCore.TestHost.WebHostBuilderExtensions.ConfigureTestServicesStartupConfigureServicesFilter.<>c__DisplayClass2_0.<ConfigureServices>b__0(IServiceCollection serviceCollection)
   at Microsoft.AspNetCore.Hosting.StartupLoader.ConfigureServicesDelegateBuilder`1.<>c__DisplayClass15_0.<BuildStartupServicesFilterPipeline>g__RunPipeline|0(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.Invoke(Object instance, IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass8_0.<Build>b__0(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.StartupLoader.ConfigureServicesDelegateBuilder`1.<>c__DisplayClass14_0.<ConfigureServices>g__ConfigureServicesWithContainerConfiguration|0(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.WebHost.EnsureApplicationServices()
   at Microsoft.AspNetCore.Hosting.WebHost.Initialize()
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
   at Microsoft.AspNetCore.TestHost.TestServer..ctor(IWebHostBuilder builder, IFeatureCollection featureCollection)
   at Microsoft.AspNetCore.TestHost.TestServer..ctor(IWebHostBuilder builder)
   at test.integration.Example.Test1() in C:\work\test\test.integration\Example.cs:line 12
--- End of stack trace from previous location ---

Further technical details

  • ASP.NET Core version
    from sdk 5.0.100-rc.2.20479.15
  • Include the output of dotnet --info
❯ dotnet --info
.NET SDK (reflecting any global.json):
 Version:   5.0.100-rc.2.20479.15
 Commit:    da7dfa8840

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.18362
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\5.0.100-rc.2.20479.15\

Host (useful for support):
  Version: 5.0.0-rc.2.20475.5
  Commit:  c5a3f49c88

.NET SDKs installed:
  2.1.804 [C:\Program Files\dotnet\sdk]
  2.2.207 [C:\Program Files\dotnet\sdk]
  3.1.101 [C:\Program Files\dotnet\sdk]
  3.1.102 [C:\Program Files\dotnet\sdk]
  5.0.100-rc.2.20479.15 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.21 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.21 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.0-rc.2.20475.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.21 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.0-rc.2.20475.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.0-rc.2.20475.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  • The IDE (VS / VS Code/ VS4Mac) you're running on, and it's version
    Dotnet CLI - 5.0.100-rc.2.20479.15
    Resharper 2020.2.4 (doesn't required to reproduce exception)
    VisualStudio 2019 Preview 16.8.0 Preview 4.0 (doesn't required to reproduce exception)
@Tratcher Tratcher added area-blazor Includes: Blazor, Razor Components and removed area-servers labels Oct 14, 2020
@Tratcher
Copy link
Member

Could not load type 'Microsoft.AspNetCore.Components.Forms.ReadRequest' from assembly 'Microsoft.AspNetCore.Components.Web, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' because it contains an object field at offset 4 that is incorrectly aligned or overlapped by a non-object field.

CC: Blazor

@mkArtakMSFT
Copy link
Member

@SteveSandersonMS do you happen what may cause this?

@SteveSandersonMS
Copy link
Member

Yeah we'll probably have to fix this in 5.0.

There's a type in the .Web project that is only intended for use on WebAssembly, and it has a fixed struct layout that assumes 32-bit only. This isn't normally a problem because at runtime the type is never used if you're not on WebAssembly. However if you use reflection to walk over all the loaded types, you can attempt to load it on server code and hence trigger a type load exception (assuming server is 64-bit).

Fixing it is pretty trivial - we just change the struct to be safe for 64-bit runtimes too by aligning fields to 8-byte boundaries, like our other fixed-layout structs. I'll look into doing this.

@WojciechNagorski
Copy link
Author

@SteveSandersonMS thanks for investigation. I think that use reflection to walk over all the loaded types is common use case, especially when we use dependency injection.

@WojciechNagorski
Copy link
Author

WojciechNagorski commented Oct 15, 2020

@SteveSandersonMS I thought about this problem and one thing is not clear to me. Why the problem occurs only when I run this code from the test but when I run the application from the console it works correctly. In this case, I use reflection to go through all types and it doesn't cause an error.

The command:

dotnet run --project .\test.net5\test\test.csproj

Output:

Building...
Register
Hosting environment: Development
Content root path: C:\work\AspDotnet5Issue\test.net5\test
Now listening on: https://localhost:5001
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

@SteveSandersonMS
Copy link
Member

Fixed in #26931

@ghost ghost added Done This issue has been fixed and removed Working labels Oct 19, 2020
dotnet-maestro bot added a commit that referenced this issue Oct 27, 2020
[master] Update dependencies from dotnet/runtime dotnet/efcore


 - Update TFM net5.0 -> net6.0

 - Introduce $(TargetTFM)

 - TargetTfm -> DefaultNetCoreTargetFramework

 - Updated: NETCoreAppMaximumVersion

 - Apply suggestions from code review

Co-authored-by: Doug Bunting <[email protected]>

 - Fix version numbers used in Blazor WASM SDK

 - Address feedback from peer review

 - Fix check for platform version

 - Fix build config for dotnet-watch project

 - Update TFM net5.0 -> net6.0

 - Introduce $(TargetTFM)

 - TargetTfm -> DefaultNetCoreTargetFramework

 - Apply suggestions from code review

Co-authored-by: Doug Bunting <[email protected]>

 - Fix version numbers used in Blazor WASM SDK

 - Address feedback from peer review

 - Remove workaround in dotnet-watch tests

 - Testrunner to 5.0 and adding workarounds

 - Try props.in

 - net50

 - Fixup KnownFrameworkReference for integration tests

 - Resolve a few comments
- don't use repo-specific properties in shipping file
- pass `$(DefaultNetCoreTargetFramework)` into template files used in tests

 - Update src/Components/WebAssembly/Sdk/src/targets/Microsoft.NET.Sdk.BlazorWebAssembly.Current.targets

 - Update KnownFrameworkReference for template tests

 - Use DefaultNetcoreTFM

 - Undo change to props.in

 - Update src/ProjectTemplates/test/ProjectTemplates.Tests.csproj

 - Update GenerateTestProps.targets

- this is where `$(KnownAppHostPackOrFrameworkReferenceTfm)` is needed

 - Update src/ProjectTemplates/test/ProjectTemplates.Tests.csproj

- additional template input not needed here

 - Other file changes

 - Rename the one net5.0 directory in the repo

 - Fixup rebase remnants

 - Use runtime and ref/ assemblies matching repo in Helix testing
- add Directory.Build.*.in files based on project template test infrastructure
- use files as import boundary where the project doesn't create its own Directory.Build.* files
- ensure `dotnet-watch` tests also use the latest runtime and ref/ assemblies

 - Switch Directory.Build.*.in files in Helix content
- ensure item manipulation is done after base items exist

nit: Fix *.in exclusion

 - Update TFM workaround to reference 5.0 instead of 3.1 (#26991)

 - Make some Directory.Build.*.in settings override-able
- that is, move some properties to Directory.Build.props.in

also
- fix `Condition`s that resulted in empty Directory.Build.targets
- separate Directory.Build.empty.in
- ensure RunTests project is _not_ affected by root Directory.Build.* files

 - Update SDK

 - Set `$(DefaultNetCoreTargetFramework)` in Helix root Directory.Build.props
= `dotnet-watch` tests otherwise fail with "The TargetFramework value '' was not recognized"

 - Extend Helix Directory.Build.* workarounds
- generate Directory.Build.* files when restoring any projects
- include generated files in Helix runs needing the latest runtime
- copy generated files when testing `dotnet-watch` locally
- include generated content in Microsoft.NET.Sdk.BlazorWebAssembly.IntegrationTests test assets
  - remove duplicate settings from existing Directory.Build.* files
- ensure shared framework and targeting packs are laid out under .dotnet/ before test assets restore

 - !fixup! Remove extra end tags

 - !fixup! Don't build GenerateFiles.csproj in desktop `msbuild`

 - !fixup! Arcade uses different test targets

 - Disable `crossgen` when building for Helix runs
- make `$(CrossgenOutput)` property override-able
- use override in CI jobs that submit to other platforms
  - for now, leave the ARM64 Helix jobs alone (build on Ubuntu, run in Debian)

nits:
- correct condition for `$(GenerateCrossgenProfilingSymbols)`
- set `$(ASPNETCORE_TEST_LOG_DIR)` in every step of the Helix build jobs

 - Ensure ReadRequest type can be loaded on server. Fixes #26882 (#26931)

 - Enable debugging when using embedded PDBs (#27107)

* Fix debug using embedded PDBs.

* Check for either debugBuild or referenced PDBs

Co-authored-by: Thays <[email protected]>

 - Ensure Blazor JS files are up-to-date

 - Merge branch 'master' into darc-master-76e24f4d-c90c-42ac-a1e5-411ae61ca37b
@bhrugen
Copy link

bhrugen commented Oct 30, 2020

this is roadblocking in creating migrations in a real project for me, is there any workaround?

@SteveSandersonMS
Copy link
Member

I’d suggest considering updating to RTM packages from the .NET nightly build feed.

@ghost ghost locked as resolved and limited conversation to collaborators Nov 29, 2020
dougbu pushed a commit to dougbu/razor-compiler that referenced this issue Nov 17, 2021
…t/aspnetcore#26788)

[master] Update dependencies from dotnet/runtime dotnet/efcore


 - Update TFM net5.0 -> net6.0

 - Introduce $(TargetTFM)

 - TargetTfm -> DefaultNetCoreTargetFramework

 - Updated: NETCoreAppMaximumVersion

 - Apply suggestions from code review

Co-authored-by: Doug Bunting <[email protected]>

 - Fix version numbers used in Blazor WASM SDK

 - Address feedback from peer review

 - Fix check for platform version

 - Fix build config for dotnet-watch project

 - Update TFM net5.0 -> net6.0

 - Introduce $(TargetTFM)

 - TargetTfm -> DefaultNetCoreTargetFramework

 - Apply suggestions from code review

Co-authored-by: Doug Bunting <[email protected]>

 - Fix version numbers used in Blazor WASM SDK

 - Address feedback from peer review

 - Remove workaround in dotnet-watch tests

 - Testrunner to 5.0 and adding workarounds

 - Try props.in

 - net50

 - Fixup KnownFrameworkReference for integration tests

 - Resolve a few comments
- don't use repo-specific properties in shipping file
- pass `$(DefaultNetCoreTargetFramework)` into template files used in tests

 - Update src/Components/WebAssembly/Sdk/src/targets/Microsoft.NET.Sdk.BlazorWebAssembly.Current.targets

 - Update KnownFrameworkReference for template tests

 - Use DefaultNetcoreTFM

 - Undo change to props.in

 - Update src/ProjectTemplates/test/ProjectTemplates.Tests.csproj

 - Update GenerateTestProps.targets

- this is where `$(KnownAppHostPackOrFrameworkReferenceTfm)` is needed

 - Update src/ProjectTemplates/test/ProjectTemplates.Tests.csproj

- additional template input not needed here

 - Other file changes

 - Rename the one net5.0 directory in the repo

 - Fixup rebase remnants

 - Use runtime and ref/ assemblies matching repo in Helix testing
- add Directory.Build.*.in files based on project template test infrastructure
- use files as import boundary where the project doesn't create its own Directory.Build.* files
- ensure `dotnet-watch` tests also use the latest runtime and ref/ assemblies

 - Switch Directory.Build.*.in files in Helix content
- ensure item manipulation is done after base items exist

nit: Fix *.in exclusion

 - Update TFM workaround to reference 5.0 instead of 3.1 (dotnet/aspnetcore#26991)

 - Make some Directory.Build.*.in settings override-able
- that is, move some properties to Directory.Build.props.in

also
- fix `Condition`s that resulted in empty Directory.Build.targets
- separate Directory.Build.empty.in
- ensure RunTests project is _not_ affected by root Directory.Build.* files

 - Update SDK

 - Set `$(DefaultNetCoreTargetFramework)` in Helix root Directory.Build.props
= `dotnet-watch` tests otherwise fail with "The TargetFramework value '' was not recognized"

 - Extend Helix Directory.Build.* workarounds
- generate Directory.Build.* files when restoring any projects
- include generated files in Helix runs needing the latest runtime
- copy generated files when testing `dotnet-watch` locally
- include generated content in Microsoft.NET.Sdk.BlazorWebAssembly.IntegrationTests test assets
  - remove duplicate settings from existing Directory.Build.* files
- ensure shared framework and targeting packs are laid out under .dotnet/ before test assets restore

 - !fixup! Remove extra end tags

 - !fixup! Don't build GenerateFiles.csproj in desktop `msbuild`

 - !fixup! Arcade uses different test targets

 - Disable `crossgen` when building for Helix runs
- make `$(CrossgenOutput)` property override-able
- use override in CI jobs that submit to other platforms
  - for now, leave the ARM64 Helix jobs alone (build on Ubuntu, run in Debian)

nits:
- correct condition for `$(GenerateCrossgenProfilingSymbols)`
- set `$(ASPNETCORE_TEST_LOG_DIR)` in every step of the Helix build jobs

 - Ensure ReadRequest type can be loaded on server. Fixes dotnet/aspnetcore#26882 (dotnet/aspnetcore#26931)

 - Enable debugging when using embedded PDBs (dotnet/aspnetcore#27107)

* Fix debug using embedded PDBs.

* Check for either debugBuild or referenced PDBs

Co-authored-by: Thays <[email protected]>

 - Ensure Blazor JS files are up-to-date

 - Merge branch 'master' into darc-master-76e24f4d-c90c-42ac-a1e5-411ae61ca37b

Commit migrated from dotnet/aspnetcore@219ecd688012
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug. Done This issue has been fixed
Projects
None yet
Development

No branches or pull requests

6 participants