繁体   English   中英

Asp.Net Core内存中TestServer中的ReflectionTypeLoadException异常

[英]ReflectionTypeLoadException exception in Asp.Net Core in-memory TestServer

我像这样创建一个IWebHostBuilder

public IWebHostBuilder GetWebHostBuilder()
{
     return new WebHostBuilder().UseContentRoot(_contentRoot)
                                       .ConfigureServices(InitializeServiceCollection)
                                       .UseEnvironment(_environment)
                                       .UseConfiguration(GetConfiguration())
                                       .UseStartup(typeof(TStartup));
}

在这里, InitializeServiceCollection的实现方式如下:

private void InitializeServiceCollection(IServiceCollection services)
{
     var manager = new ApplicationPartManager();

     manager.ApplicationParts.Add(new AssemblyPart(_assembly));
     manager.FeatureProviders.Add(new ControllerFeatureProvider());
     manager.FeatureProviders.Add(new ViewComponentFeatureProvider());

     services.AddSingleton(manager);
}

然后,我像这样创建TestServer

var myTestServer = new TestServer(GetWebHostBuilder());

在这里,我得到了异常(下面是完整的异常)。 它被抛出在services.AddAutoMapper(); 被测系统中的方法调用。 但是,当我自己运行要测试的系统并使用Postman对其进行手动测试时,它运行良好,并且使用Automapper进行对象映射也运行良好。 它只是在集成测试中引发了异常。

完全例外:

System.AggregateException : One or more errors occurred. (Unable to load one or more of the requested types.
Could not load type 'Microsoft.EntityFrameworkCore.Metadata.Internal.RelationalFullAnnotationNames' from assembly 'Microsoft.EntityFrameworkCore.Relational, Version=2.2.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.) (The following constructor parameters did not have matching fixture data: TestFixture fixture)
---- System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types.
Could not load type 'Microsoft.EntityFrameworkCore.Metadata.Internal.RelationalFullAnnotationNames' from assembly 'Microsoft.EntityFrameworkCore.Relational, Version=2.2.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
---- The following constructor parameters did not have matching fixture data: TestFixture fixture

----- Inner Stack Trace #1 (System.Reflection.ReflectionTypeLoadException) -----
   at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
   at System.Reflection.RuntimeAssembly.get_DefinedTypes()
   at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.ToArray()
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at AutoMapper.ServiceCollectionExtensions.AddAutoMapperClasses(IServiceCollection services, Action`2 configAction, IEnumerable`1 assembliesToScan) in xxx\automapper-extensions-microsoft-dependencyinjectio\src\AutoMapper.Extensions.Microsoft.DependencyInjection\ServiceCollectionExtensions.cs:line 72
   at xxx.Startup.ConfigureServices(IServiceCollection services) in xxx\Startup.cs:line 35
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.Initialize()
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
   at Microsoft.AspNetCore.TestHost.TestServer..ctor(IWebHostBuilder builder, IFeatureCollection featureCollection)
   at xxx.Tests.TestFixture.InitializeServer() in xxx.Tests\TestFixture.cs:line 67
   at xxx.Tests.TestFixture..ctor() in xxx.Tests\TestFixture.cs:line 31
----- Inner Stack Trace #2 (Xunit.Sdk.TestClassException) -----

因此,我设法通过将services.AddAutoMapper()替换为services.AddAutoMapper(Type[] types)重载来解决了该问题。 不确定原始方法有什么问题,但是为了避免对程序集进行反射调用,我使用了此重载,并且现在可以正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM