Home > Net >  Errors loading loosely linked user assemblies
Errors loading loosely linked user assemblies

Time:12-15

Good morning.

I am developing a Web API using Net 6.

The preliminary structure of the API is shown in the figure.

enter image description here

ContractGpdApi - this is the direct assembly of the API. It has a reference to the ContractGpdApi.ServiceInterfaces assembly. ContractGpdApi.ServiceInterfaces - this assembly containing the DTO classes (business models) and service interfaces. ContractGpdApi.ServiceImplementations - this assembly contains classes that implement the ContractGpdApi.ServiceInterfaces assembly interfaces. The assembly adapts the methods and classes of the ContractGpdApi.DbServiceInterfaces assembly into the methods and classes of the ContractGpdApi.ServiceInterfaces assembly. ContractGpdApi.DbServiceInterfaces - this assembly contains classes and database access layer interfaces. ContractGpdApi.DbServiceImplementations - this assembly contains classes that implement the ContractGpdApi.DbServiceInterfaces assembly interfaces. The assembly contains a reference to the ContractGpdApi.DbServiceInterfaces assembly, as well as references to the microsoft.entityframeworkcore, microsoft.entityframeworkcore.sqlserver assemblies

I ended up implementing the Ladder pattern and dividing the CUI into loosely coupled layers.

The DB layer is represented by two assemblies: ContractGpdApi.DbServiceImplementations and ContractGpdApi.DbServiceInterfaces.

The business work layer is represented by two assemblies: ContractGpdApi.ServiceImplementations and ContractGpdApi.ServiceInterfaces.

Accordingly, assembly ContractGpdApi will be a kind of root where you load and embed dependencies, loosely linked assemblies.

The following errors occur when loading assemblies:

  1. Could not load file or assembly 'ContractGpdApi.DbServiceInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The specified file cannot be found.
  2. Could not load file or assembly 'Microsoft.EntityFrameworkCore, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The specified file cannot be found.

enter image description here

All other assemblies are loaded normally. Contents of the ContractGpdApi.csproj file:

<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>

Contents of the CopyingAssemblies.bat file:

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"

Tell me, what am I doing wrong?

CodePudding user response:

Made a change to the method code:

 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;
        }
    }

This edit fixed only the first error:

Could not load file or assembly 'ContractGpdApi.DbServiceInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The specified file cannot be found.

  • Related