在 .Net Core API 中使用 ASMX 服务:System.Reflection.ReflectionTypeLoadException:无法加载一种或多种请求的类型

Consuming ASMX service in .Net Core API: System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types

尝试启动部署在 IIS 上的 .Net Core API 应用程序时,我在日志文件中遇到以下错误。根据许多帖子(IIS 中的配置更改等)尝试了许多解决方法,但仍然无法解决此问题。专门尝试添加日志以使用此link查找特定丢失的dll,与.Net Core相关

但它没有记录与此异常相关的任何特定日志消息。日志中只有上述错误。

这是一个 .Net Core API 应用程序(Net Core 2.0 with Visual Studio 2017)。在 Windows 10 和 Windows Server 2012 R2 中部署在 IIS 上。日志文件中存在同样的错误。

我在这里发现的主要问题是:该项目添加了对 SOAP 服务(asmx 服务)的引用。删除此服务引用后,不会出现此类错误。应用程序似乎试图从其他地方获取相关的 dll,而不是从已发布的文件夹中获取。可能来自 GAC。但是也将与 "system.servicemodel.*" 相关的 dll 复制到 GAC(这些是通过 Visual Studio 中的连接服务添加该服务引用时自动添加的 dll(来自 Nuget))。

这仅在部署后(在 IIS 上)发生。当从源代码运行这个应用程序时,这工作正常。没有这样的错误。我不确定这些新版本的.Net Core部署后是否不支持asmx服务。

感谢解决此问题的任何帮助,因为我们需要在此项目中引用 asmx 服务。 谢谢。

2018-10-01T10:10:42.6383743+01:00 0HLH7FMNJ4U50:00000002 [ERR] Connection id ""0HLH7FMNJ4U50"", Request id ""0HLH7FMNJ4U50:00000002"": An unhandled exception was thrown by the application. (560e7d32) System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information. at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module) at System.Reflection.RuntimeAssembly.get_DefinedTypes() at Microsoft.AspNetCore.Mvc.Controllers.ControllerFeatureProvider.PopulateFeature(IEnumerable1 parts, ControllerFeature feature) at Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartManager.PopulateFeature[TFeature](TFeature feature) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionDescriptorProvider.GetControllerTypes() at Microsoft.AspNetCore.Mvc.Internal.ControllerActionDescriptorProvider.BuildModel() at Microsoft.AspNetCore.Mvc.Internal.ControllerActionDescriptorProvider.GetDescriptors() at Microsoft.AspNetCore.Mvc.Internal.ControllerActionDescriptorProvider.OnProvidersExecuting(ActionDescriptorProviderContext context) at Microsoft.AspNetCore.Mvc.Internal.ActionDescriptorCollectionProvider.UpdateCollection() at Microsoft.AspNetCore.Mvc.Internal.ActionDescriptorCollectionProvider.get_ActionDescriptors() at Microsoft.AspNetCore.Mvc.Internal.AttributeRoute.GetTreeRouter() at Microsoft.AspNetCore.Mvc.Internal.AttributeRoute.RouteAsync(RouteContext context) at Microsoft.AspNetCore.Routing.RouteCollection.d__9.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Builder.RouterMiddleware.d__4.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Server.IISIntegration.IISMiddleware.d__11.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Hosting.Internal.RequestServicesContainerMiddleware.d__3.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Frame1.d__2.MoveNext() 2018-10-01T10:10:42.6385488+01:00 0HLH7FMNJ4U50:00000002 [INF] Request finished in 11.3774ms 500 (791a596a)

PS:我可以解决这个问题。我在下面的评论中添加了我找到的解决方案。

我可以通过将 .Net 标准项目包含到 .Net Core 2.0 项目中来解决此问题(添加了 .Net 标准项目对 .Net Core 项目的引用)。