网络核心加载程序异常

Posted

技术标签:

【中文标题】网络核心加载程序异常【英文标题】:netcore LoaderException 【发布时间】:2018-07-19 14:16:13 【问题描述】:

我正在构建针对 .NetCore20 的服务。我正在 CentOS 7.6 上使用 JetBrains Rider 构建这些服务。

我的一个解决方案有一个非常奇怪的问题,其中一个库在许多其他解决方案中使用都没有任何问题。

屏幕截图显示了抛出的完整错误:

因此,如果我查看我的 .csproj 文件,它会显示以下内容:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="AutoMapper" Version="7.0.1" />
    <PackageReference Include="EasyNetQ" Version="3.3.0" />
    <PackageReference Include="EasyNetQ.Serilog" Version="2.3.3" />
    <PackageReference Include="MongoDB.Driver" Version="2.6.1" />
    <PackageReference Include="Serilog.AspNetCore" Version="2.1.1" />
    <PackageReference Include="Serilog.Enrichers.Context" Version="2.4.0" />
    <PackageReference Include="ServiceStack" Version="5.0.2" />
    <PackageReference Include="ServiceStack.OrmLite" Version="5.0.2" />
    <PackageReference Include="ServiceStack.Redis" Version="5.0.2" />
    <PackageReference Include="ServiceStack.Text" Version="5.0.2" />
  </ItemGroup>
  <ItemGroup>
    <Reference Include="BediDto, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
      <HintPath>..\..\BediLibs\BediDto\bin\Release\netcoreapp2.0\BediDto.dll</HintPath>
    </Reference>
    <Reference Include="BediModels, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
      <HintPath>..\..\BediLibs\BediModels\bin\Release\netcoreapp2.0\BediModels.dll</HintPath>
    </Reference>
    <Reference Include="BediTools, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
      <HintPath>..\..\BediLibs\BediTools\bin\Release\netcoreapp2.0\BediTools.dll</HintPath>
    </Reference>
    <Reference Include="BizBus.CommonEnums, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
      <HintPath>..\..\BizBusLibs\BizBus.CommonEnums\bin\Release\netcoreapp2.0\BizBus.CommonEnums.dll</HintPath>
    </Reference>
    <Reference Include="BizBus.Commons, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
      <HintPath>..\..\BizBusLibs\BizBus.Commons\bin\Release\netcoreapp2.0\BizBus.Commons.dll</HintPath>
    </Reference>
    <Reference Include="OperationsManagerServer.ServiceModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
      <HintPath>..\..\BizBusOpsManagerServer\OperationsManagerServer.ServiceModel\bin\Release\netcoreapp2.0\OperationsManagerServer.ServiceModel.dll</HintPath>
    </Reference>
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\BizBusDataExchangeServer.ServiceModel\BizBusDataExchangeServer.ServiceModel.csproj" />
  </ItemGroup>
</Project>

未找到的程序集是 BediDtoBediModels,但当然两者都存在,并且也在项目的 bin 文件夹中:

[tbednarz@linuxdev-tbws2 netcoreapp2.0]$ pwd
/home/tbednarz/Projects/BizBusInvoiceServer/BizBusInvoiceServer/bin/Debug/netcoreapp2.0
[tbednarz@linuxdev-tbws2 netcoreapp2.0]$ ls -all | grep Bedi
-rw-rw-r-- 1 tbednarz tbednarz  16384 Jul 19 15:39 BediDto.dll
-rw-rw-r-- 1 tbednarz tbednarz   3528 Jul 19 15:39 BediDto.pdb
-rw-rw-r-- 1 tbednarz tbednarz  15872 Jul 19 15:39 BediModels.dll
-rw-rw-r-- 1 tbednarz tbednarz   4632 Jul 19 15:39 BediModels.pdb
-rw-rw-r-- 1 tbednarz tbednarz   6144 Jun  4 16:54 BediTools.dll
-rw-rw-r-- 1 tbednarz tbednarz   1096 Jun  4 16:54 BediTools.pdb

我不喜欢项目文件中的相对路径(....\BediLibs.....)这在运行时是否仍然有效,我不知道....

无论是在调试器中启动还是作为docker容器启动,代码都会失败...

知道有什么问题吗?是否有我可能需要手动删除的旧缓存文件? 任何帮助将不胜感激。

更新

由于@mythz 正在回复,我有更多关于抛出异常的地方的详细信息。这是 ServiceStack 中的最后一段代码:

ServiceController.cs 有一个方法public ServiceController Init(),然后是一个方法private List&lt;Type&gt; GetAssemblyTypes(Assembly[] assembliesWithServices)。这个方法里面有一个循环

foreach (Assembly assembliesWithService in assembliesWithServices)

    str1 = assembliesWithService.FullName;
    foreach (Type type in assembliesWithService.GetTypes())
    
        if (!this.appHost.ExcludeAutoRegisteringServiceTypes.Contains(type))
        
            str2 = type.GetOperationName();
            typeList.Add(type);
        
    

我捕获的异常是在这个语句中抛出的:

str1 = assembliesWithService.FullName;

详见截图 2: 如果我进入该语句,我将在 .NET 代码中结束,一个名为 Assembly.cs 的文件,并在以下方法中:

public virtual Type[] GetTypes()

  Module[] modules = this.GetModules(false);
  int length1 = 0;
  Type[][] typeArray1 = new Type[modules.Length][];
  for (int index = 0; index < typeArray1.Length; ++index)
  
    typeArray1[index] = modules[index].GetTypes();
    length1 += typeArray1[index].Length;
  
  int destinationIndex = 0;
  Type[] typeArray2 = new Type[length1];
  for (int index = 0; index < typeArray1.Length; ++index)
  
    int length2 = typeArray1[index].Length;
    Array.Copy((Array) typeArray1[index], 0, (Array) typeArray2, destinationIndex, length2);
    destinationIndex += length2;
  
  return typeArray2;

语句typeArray1[index] = modules[index].GetTypes(); 是导致崩溃的语句。在这里,我还可以在我的库中看到 FileNotFound 异常:

所以问题是为什么会发生这种情况。我在许多其他项目中使用它没有问题,所以这很奇怪,但也许代码让其他开发人员对 .Net 有更深入的了解,我可以进一步调查......

更新 2

我将我的软件作为 docker 镜像分发。为了构建它们,我目前使用了一些 bash 脚本。这是我在“问题”解决方案中使用的生成本地图像的方法:

#!/usr/bin/env bash
publishdir=publish
localimagename=bbinvoiceserver
if [ -d $publishdir ]; then
  printf "Target directory (%s) already exists, deleting it ...\n" $publishdir
  rm -rf $publishdir
fi
dotnet publish -c "Debug" -o $publishdir
# create docker image
docker build -t $localimagename -f dockerfile.debug .
if docker image ls | grep -q '<none>'; then
    printf "Removing temporary images (<none>   <none> ones)....\n"
    docker rmi $(docker images -f dangling=true -q)
fi
docker image ls
rm -rf $publishdir

'dotnet publish' 的输出是:

Microsoft (R) Build Engine version 15.6.82.30579 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Restoring packages for /home/tbednarz/Projects/BizBusInvoiceServer/BizBusInvoiceServer.ServiceInterface/BizBusInvoiceServer.ServiceInterface.csproj...
  Restoring packages for /home/tbednarz/Projects/BizBusInvoiceServer/BizBusInvoiceServer.ServiceModel/BizBusInvoiceServer.ServiceModel.csproj...
  Restoring packages for /home/tbednarz/Projects/BizBusInvoiceServer/BizBusInvoiceServer/BizBusInvoiceServer.csproj...
  Generating MSBuild file /home/tbednarz/Projects/BizBusInvoiceServer/BizBusInvoiceServer.ServiceModel/obj/BizBusInvoiceServer.ServiceModel.csproj.nuget.g.props.
  Restore completed in 441.69 ms for /home/tbednarz/Projects/BizBusInvoiceServer/BizBusInvoiceServer.ServiceModel/BizBusInvoiceServer.ServiceModel.csproj.
  Generating MSBuild file /home/tbednarz/Projects/BizBusInvoiceServer/BizBusInvoiceServer.ServiceInterface/obj/BizBusInvoiceServer.ServiceInterface.csproj.nuget.g.props.
  Generating MSBuild file /home/tbednarz/Projects/BizBusInvoiceServer/BizBusInvoiceServer/obj/BizBusInvoiceServer.csproj.nuget.g.props.
  Restore completed in 949.69 ms for /home/tbednarz/Projects/BizBusInvoiceServer/BizBusInvoiceServer.ServiceInterface/BizBusInvoiceServer.ServiceInterface.csproj.
  Restore completed in 978.09 ms for /home/tbednarz/Projects/BizBusInvoiceServer/BizBusInvoiceServer/BizBusInvoiceServer.csproj.
  BizBusInvoiceServer.ServiceModel -> /home/tbednarz/Projects/BizBusInvoiceServer/BizBusInvoiceServer.ServiceModel/bin/Debug/netcoreapp2.0/BizBusInvoiceServer.ServiceModel.dll
  BizBusInvoiceServer.ServiceModel -> /home/tbednarz/Projects/BizBusInvoiceServer/BizBusInvoiceServer.ServiceModel/publish/
  BizBusInvoiceServer.ServiceInterface -> /home/tbednarz/Projects/BizBusInvoiceServer/BizBusInvoiceServer.ServiceInterface/bin/Debug/netcoreapp2.0/BizBusInvoiceServer.ServiceInterface.dll
  BizBusInvoiceServer.ServiceInterface -> /home/tbednarz/Projects/BizBusInvoiceServer/BizBusInvoiceServer.ServiceInterface/publish/
  BizBusInvoiceServer -> /home/tbednarz/Projects/BizBusInvoiceServer/BizBusInvoiceServer/bin/Debug/netcoreapp2.0/BizBusInvoiceServer.dll
  BizBusInvoiceServer -> /home/tbednarz/Projects/BizBusInvoiceServer/BizBusInvoiceServer/publish/

如果我列出 publish 文件夹的内容,它会包含它抱怨无法在运行时找到的所有文件:

[tbednarz@linuxdev-tbws2 publish]$ ls -l
total 10224
-rwxrw-rw-  1 tbednarz tbednarz  273408 Jun 18 13:00 AutoMapper.dll
-rw-rw-r--  1 tbednarz tbednarz   16384 Jul 19 15:39 BediDto.dll
-rw-rw-r--  1 tbednarz tbednarz    3528 Jul 19 15:39 BediDto.pdb
-rw-rw-r--  1 tbednarz tbednarz   15872 Jul 19 15:39 BediModels.dll
-rw-rw-r--  1 tbednarz tbednarz    4632 Jul 19 15:39 BediModels.pdb
-rw-rw-r--  1 tbednarz tbednarz    6144 Jun  4 16:54 BediTools.dll
-rw-rw-r--  1 tbednarz tbednarz    1096 Jun  4 16:54 BediTools.pdb
-rw-rw-r--  1 tbednarz tbednarz   10752 Jul 18 14:40 BizBus.CommonEnums.dll
-rw-rw-r--  1 tbednarz tbednarz     240 Jul 18 14:40 BizBus.CommonEnums.pdb
-rw-rw-r--  1 tbednarz tbednarz   44032 Jul 18 14:40 BizBus.Commons.dll
-rw-rw-r--  1 tbednarz tbednarz   15812 Jul 18 14:40 BizBus.Commons.pdb
-rwxr-xr-x  1 tbednarz tbednarz   59392 May 29 09:15 BizBusDataExchangeServer.ServiceModel.dll
-rwxr-xr-x  1 tbednarz tbednarz   16712 May 29 09:15 BizBusDataExchangeServer.ServiceModel.pdb
-rw-rw-r--  1 tbednarz tbednarz  130372 Jul 20 08:45 BizBusInvoiceServer.deps.json
-rw-rw-r--  1 tbednarz tbednarz   27648 Jul 20 08:45 BizBusInvoiceServer.dll
-rw-rw-r--  1 tbednarz tbednarz    5800 Jul 20 08:45 BizBusInvoiceServer.pdb
-rw-rw-r--  1 tbednarz tbednarz     146 Jul 20 08:45 BizBusInvoiceServer.runtimeconfig.json
-rw-rw-r--  1 tbednarz tbednarz  273408 Jul 20 08:45 BizBusInvoiceServer.ServiceInterface.dll
-rw-rw-r--  1 tbednarz tbednarz   66832 Jul 20 08:45 BizBusInvoiceServer.ServiceInterface.pdb
-rw-rw-r--  1 tbednarz tbednarz   94720 Jul 20 08:45 BizBusInvoiceServer.ServiceModel.dll
-rw-rw-r--  1 tbednarz tbednarz   24488 Jul 20 08:45 BizBusInvoiceServer.ServiceModel.pdb
-rwxrw-rw-  1 tbednarz tbednarz  186368 Jan 11  2018 CommandLine.dll
-rwxrw-rw-  1 tbednarz tbednarz   92672 May  1  2017 DnsClient.dll
-rwxrw-rw-  1 tbednarz tbednarz   22008 Apr 24 00:44 Microsoft.AspNetCore.Hosting.Abstractions.dll
-rwxrw-rw-  1 tbednarz tbednarz  124912 Apr 24 00:44 Microsoft.AspNetCore.Hosting.dll
-rwxrw-rw-  1 tbednarz tbednarz   15352 Apr 24 00:44 Microsoft.AspNetCore.Hosting.Server.Abstractions.dll
-rwxrw-rw-  1 tbednarz tbednarz   73712 Apr 24 00:44 Microsoft.AspNetCore.Http.Abstractions.dll
-rwxrw-rw-  1 tbednarz tbednarz   78840 Apr 24 00:44 Microsoft.AspNetCore.Http.dll
-rwxrw-rw-  1 tbednarz tbednarz   38384 Apr 24 00:44 Microsoft.AspNetCore.Http.Extensions.dll
-rwxrw-rw-  1 tbednarz tbednarz   31728 Apr 24 00:44 Microsoft.AspNetCore.Http.Features.dll
-rwxrw-rw-  1 tbednarz tbednarz  276464 Apr 24 00:44 Microsoft.AspNetCore.Server.Kestrel.Core.dll
-rwxrw-rw-  1 tbednarz tbednarz   15864 Apr 24 00:44 Microsoft.AspNetCore.Server.Kestrel.dll
-rwxrw-rw-  1 tbednarz tbednarz   93688 Apr 24 00:44 Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.dll
-rwxrw-rw-  1 tbednarz tbednarz   83440 Apr 24 00:44 Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.dll
-rwxrw-rw-  1 tbednarz tbednarz   66544 Apr 24 00:44 Microsoft.AspNetCore.WebUtilities.dll
-rwxrw-rw-  1 tbednarz tbednarz   19960 Apr 24 00:44 Microsoft.Extensions.Configuration.Abstractions.dll
-rwxrw-rw-  1 tbednarz tbednarz   24056 Apr 24 00:44 Microsoft.Extensions.Configuration.Binder.dll
-rwxrw-rw-  1 tbednarz tbednarz   20984 Apr 24 00:44 Microsoft.Extensions.Configuration.CommandLine.dll
-rwxrw-rw-  1 tbednarz tbednarz   24560 Apr 24 00:44 Microsoft.Extensions.Configuration.dll
-rwxrw-rw-  1 tbednarz tbednarz   19960 Apr 24 00:44 Microsoft.Extensions.Configuration.EnvironmentVariables.dll
-rwxrw-rw-  1 tbednarz tbednarz   22000 Apr 24 00:44 Microsoft.Extensions.Configuration.FileExtensions.dll
-rwxrw-rw-  1 tbednarz tbednarz   23544 Apr 24 00:44 Microsoft.Extensions.Configuration.Json.dll
-rw-r--r--  1 tbednarz tbednarz   36360 Apr  6 16:09 Microsoft.Extensions.DependencyInjection.Abstractions.dll
-rw-r--r--  1 tbednarz tbednarz   44552 Apr  6 16:09 Microsoft.Extensions.DependencyInjection.dll
-rw-r--r--  1 tbednarz tbednarz   17904 Apr  6 16:09 Microsoft.Extensions.FileProviders.Abstractions.dll
-rw-r--r--  1 tbednarz tbednarz   31216 Apr  6 16:09 Microsoft.Extensions.FileProviders.Physical.dll
-rw-r--r--  1 tbednarz tbednarz   39408 Apr  6 16:09 Microsoft.Extensions.FileSystemGlobbing.dll
-rwxrw-rw-  1 tbednarz tbednarz   14320 Apr 24 00:44 Microsoft.Extensions.Hosting.Abstractions.dll
-rwxrw-rw-  1 tbednarz tbednarz   46584 Apr 24 00:44 Microsoft.Extensions.Logging.Abstractions.dll
-rwxrw-rw-  1 tbednarz tbednarz   30704 Apr 24 00:44 Microsoft.Extensions.Logging.dll
-rw-r--r--  1 tbednarz tbednarz   17928 Apr  6 16:09 Microsoft.Extensions.ObjectPool.dll
-rwxrw-rw-  1 tbednarz tbednarz   26104 Apr 24 00:44 Microsoft.Extensions.Options.dll
-rw-r--r--  1 tbednarz tbednarz   33288 Apr  6 16:09 Microsoft.Extensions.Primitives.dll
-rwxrw-rw-  1 tbednarz tbednarz   71152 Apr 24 00:44 Microsoft.Net.Http.Headers.dll
-rwxrw-rw-  1 tbednarz tbednarz  451072 May 16 22:31 MongoDB.Bson.dll
-rwxrw-rw-  1 tbednarz tbednarz  632320 May 16 22:31 MongoDB.Driver.Core.dll
-rwxrw-rw-  1 tbednarz tbednarz  639488 May 16 22:31 MongoDB.Driver.dll
-rwxrw-rw-  1 tbednarz tbednarz  639488 Jun 18  2017 Newtonsoft.Json.dll
-rw-rw-r--  1 tbednarz tbednarz   87552 Jul 16 14:59 OperationsManagerServer.ServiceModel.dll
-rw-rw-r--  1 tbednarz tbednarz   23292 Jul 16 14:59 OperationsManagerServer.ServiceModel.pdb
drwxrwxr-x 15 tbednarz tbednarz    4096 Jul 20 08:45 runtimes
-rwxrw-rw-  1 tbednarz tbednarz    8192 Mar  7 06:12 Serilog.AspNetCore.dll
-rwxrw-rw-  1 tbednarz tbednarz  116736 Dec  3  2017 Serilog.dll
-rwxrw-rw-  1 tbednarz tbednarz    6656 Jan 14  2018 Serilog.Enrichers.Context.dll
-rwxrw-rw-  1 tbednarz tbednarz    5120 Nov 13  2016 Serilog.Enrichers.Thread.dll
-rwxrw-rw-  1 tbednarz tbednarz   27136 May  1 08:16 Serilog.Exceptions.dll
-rwxrw-rw-  1 tbednarz tbednarz   11264 Aug 18  2017 Serilog.Extensions.Logging.dll
-rwxrw-rw-  1 tbednarz tbednarz    8192 Jul  5  2016 Serilog.Formatting.Compact.dll
-rwxrw-rw-  1 tbednarz tbednarz   32768 Oct 22  2017 Serilog.Sinks.Console.dll
-rwxrw-rw-  1 tbednarz tbednarz   26624 Oct 29  2017 Serilog.Sinks.File.dll
-rwxrw-rw-  1 tbednarz tbednarz 1088512 Jan  2  2018 ServiceStack.Api.Swagger.dll
-rwxrw-rw-  1 tbednarz tbednarz  189440 Jan  2  2018 ServiceStack.Client.dll
-rwxrw-rw-  1 tbednarz tbednarz  411136 Jan  2  2018 ServiceStack.Common.dll
-rwxrw-rw-  1 tbednarz tbednarz 1517568 Jan  2  2018 ServiceStack.dll
-rwxrw-rw-  1 tbednarz tbednarz  138240 Jan  2  2018 ServiceStack.Interfaces.dll
-rwxrw-rw-  1 tbednarz tbednarz  575488 Jan  2  2018 ServiceStack.OrmLite.dll
-rwxrw-rw-  1 tbednarz tbednarz  286208 Jan  2  2018 ServiceStack.Redis.dll
-rwxrw-rw-  1 tbednarz tbednarz  398848 Jan  2  2018 ServiceStack.Text.dll
-rwxrw-rw-  1 tbednarz tbednarz   32504 Jul 19  2017 System.Net.Http.WinHttpHandler.dll
-rw-r--r--  1 tbednarz tbednarz   21944 Apr  6 16:09 System.Runtime.CompilerServices.Unsafe.dll
-rwxrw-rw-  1 tbednarz tbednarz   28568 Jul 25  2017 System.ServiceModel.Primitives.dll
-rw-r--r--  1 tbednarz tbednarz   60808 Apr  6 16:08 System.Text.Encodings.Web.dll

如您所见,文件BediModels.dllBediDto.dll 位于顶部。

【问题讨论】:

Rider 中的 NuGet 恢复有什么问题吗? 我不知道。 “问题”是什么意思?这不是 NuGet 问题,该库是我自己的库,我在 NuGet 包中没有它们。 在 sdk 项目中,nuget restore 使 packages.assets.json 具有所有项目依赖项,而不仅仅是 NuGet。你能从控制台构建你的项目吗? 我在一个 bash 脚本中使用 dotnet publish 来创建和推送我的 docker 镜像。这工作得很好,没有任何问题。我将在一分钟内更新原始消息并提供更多详细信息..... 使用 UPDATE 2 更新 【参考方案1】:

如果这些是项目引用,则应改为引用项目:

<ProjectReference Include="..\..\BediLibs\BediDto\BediDto.csproj" />

【讨论】:

嗯,这是一个不同解决方案中的项目。该解决方案有 12 个项目,其中包含很多我自己的库。我不知道如何从另一个解决方案添加项目引用,至少在 Rider 中我看不到这样做的方法.. 我已经更新了原始消息,提供了有关崩溃位置的更多详细信息。 @ThommyB 无法告诉您问题出在哪里,只能告诉您找不到 BediDto 程序集的错误消息。您可以在另一个解决方案中引用项目,您只需要还包括对他们引用的所有项目的引用。但是,如果您要直接引用 .dll,我建议您将它们放在本地 /lib 文件夹中,并使用脚本来复制它们,而不是尝试引用项目输出文件夹。 是的,听起来我很快就需要一个 CI 工具,否则构建所有这些东西会越来越不舒服.....【参考方案2】:

我可以通过简单地将两个 DLL 添加到“根”项目中以“快速而肮脏”的方式解决这个问题(即使这些库不是直接需要的),现在它可以工作了。所以遵循@mythz 推荐

但是,如果您要直接引用 .dll,我建议您将它们放在本地 /lib 文件夹中并使用脚本来复制它们,而不是尝试引用项目输出文件夹。

似乎是要走的路。但我相信这是dotnet 构建工具中的一个错误,它似乎无法正确解决所有依赖项(至少在某些未知情况下......)这类问题通常很难找到,因为它们几乎从来没有可正确重现...

【讨论】:

以上是关于网络核心加载程序异常的主要内容,如果未能解决你的问题,请参考以下文章

如何使用重定向 url 加载网络图像?

微信小程序。获取参数异常

C#程序访问底层网络

python动态加载小图呈现大图

从核心数据加载 UICollectionView

异常,网络编程