【问题标题】:Errors loading loosely linked user assemblies加载松散链接的用户程序集时出错
【发布时间】:2022-01-16 05:06:57
【问题描述】:

早上好。

我正在使用 Net 6 开发 Web API。

API的初步结构如图所示。

ContractGpdApi - 这是 API 的直接组装。它引用了 ContractGpdApi.ServiceInterfaces 程序集。 ContractGpdApi.ServiceInterfaces - 此程序集包含 DTO 类(业务模型)和服务接口。 ContractGpdApi.ServiceImplementations - 此程序集包含实现 ContractGpdApi.ServiceInterfaces 程序集接口的类。该程序集将 ContractGpdApi.DbServiceInterfaces 程序集的方法和类改编为 ContractGpdApi.ServiceInterfaces 程序集的方法和类。 ContractGpdApi.DbServiceInterfaces - 此程序集包含类和数据库访问层接口。 ContractGpdApi.DbServiceImplementations - 此程序集包含实现 ContractGpdApi.DbServiceInterfaces 程序集接口的类。该程序集包含对 ContractGpdApi.DbServiceInterfaces 程序集的引用,以及对 microsoft.entityframeworkcore、microsoft.entityframeworkcore.sqlserver 程序集的引用

我最终实现了梯形图模式并将 CUI 划分为松散耦合的层。

DB 层由两个程序集表示:ContractGpdApi.DbServiceImplementations 和 ContractGpdApi.DbServiceInterfaces。

业务工作层由两个程序集表示:ContractGpdApi.ServiceImplementations 和 ContractGpdApi.ServiceInterfaces。

因此,程序集 ContractGpdApi 将是一种根,您可以在其中加载和嵌入依赖项,松散链接的程序集。

加载程序集时出现以下错误:

  1. 无法加载文件或程序集“ContractGpdApi.DbServiceInterfaces,版本=1.0.0.0,文化=中性,PublicKeyToken=null”。找不到指定的文件。
  2. 无法加载文件或程序集“Microsoft.EntityFrameworkCore, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60”。找不到指定的文件。

所有其他程序集都正常加载。 ContractGpdApi.csproj 文件内容:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\ContractGpdApi.ServiceInterfaces\ContractGpdApi.ServiceInterfaces.csproj" />
  </ItemGroup>

  <Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <Exec Command="call F:\CopyingAssemblies.bat" />
  </Target>

</Project>

CopyingAssemblies.bat 文件的内容:

xcopy /y "C:\Git\ContractGpdApi\ContractGpdApi.DbServiceImplementations\bin\Debug\net6.0\*.dll" "C:\Git\ContractGpdApi\ContractGpdApi\bin\Debug\net6.0"
xcopy /y "C:\Git\ContractGpdApi\ContractGpdApi.ServiceImplementations\bin\Debug\net6.0\*.dll" "C:\Git\ContractGpdApi\ContractGpdApi\bin\Debug\net6.0"

告诉我,我做错了什么?

【问题讨论】:

  • 您可能希望使用.net-assembly 标签而不是assembly。请阅读assembly标签的描述。

标签: c# .net api .net-assembly


【解决方案1】:

修改了方法代码:

 private static Assembly LoadAssembly(string assemblyName, bool skipOnError)
    {
        try
        {
            return Assembly.LoadFrom(assemblyName); //Old code: Assembly.LoadFile(assemblyName);
        }
        catch (Exception e)
        {
            if (!(skipOnError && (e is FileNotFoundException || e is FileLoadException || e is BadImageFormatException)))
            {
                throw;
            }

            return null;
        }
    }

此编辑仅修复了第一个错误:

无法加载文件或程序集“ContractGpdApi.DbServiceInterfaces,版本=1.0.0.0,文化=中性,PublicKeyToken=null”。找不到指定的文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-20
    • 2016-08-17
    • 1970-01-01
    • 1970-01-01
    • 2012-01-03
    • 1970-01-01
    相关资源
    最近更新 更多