'What could cause a FileNotFoundException when loading an Asp.Net Core project loads its dependency

I just created an Asp.Net Core project in .Net 6, which reference a class library project(well, several actually), in .Net 6.

When I launch the app, I get an error:

FileNotFoundException: 'Could not load file or assembly 'Insert.Name.Of.My.Class.Library.Here, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'. The file cannot be found'

And then the application stops. As of now, all the projects are quite empty, just did the naming and everything, created some base classes for my entities, Dto, added some automapper profile and MediatR request/handler.

But the dll is present in my debug folder(and only once).

  • I tried to remove everything( clean debug folder, also git clean -fdx) and rebuild
  • reboot, remove .Vs folder of my solution
  • tried to use copy local to true or false
  • tried to not use copy local and have ALL the DLL placed in the same debug folder(my current solution)
  • Removed and added again the referenced project
  • Referenced directly the loaded project(initially it was a dependency of another project.
  • triple checked that every one of my 4 project involved were having only reference to other things as project(no direct DLL referencing)

I'm a bit lost, I just took some proof of concept that I did outside our big solution, and once integrated in our big solution, I get those error while it looks exactly the same as my proof of concept.

Any lead on what could be causing this issue?



Solution 1:[1]

I finally found the reason, I'm not sure why though, if somebody could explain, it would be better.

Apparently, one of my colleague did put a Directory.Build.targets on the root of our solution. The goal was to uniformize the "CopyLocal" to false, since we are trying to build all our DLL in the same folder to avoid I/O and duplication during the compilation.

The content was:

<Project>   
    <Target Name="SetAllProjectReferenceAsPublic"
        AfterTargets="AssignProjectConfiguration" 
        BeforeTargets="ResolveProjectReferences">
        <ItemGroup>
            <ProjectReferenceWithConfiguration Update="@(ProjectReferenceWithConfiguration)" >
                <Private>false</Private>
            </ProjectReferenceWithConfiguration>
            <ProjectReference Update="@(ProjectReference)" >
                <Private>false</Private>
            </ProjectReference>
        </ItemGroup>
    </Target>
</Project>

I'm not sure why, but apparently my web projects doesn't seems to like it. I just added another file in my web directory with this and now it works:

<Project>
    <Target Name="SetAllProjectReferenceAsPublic"
        AfterTargets="AssignProjectConfiguration" 
        BeforeTargets="ResolveProjectReferences">
    </Target>
</Project>

I understand the purpose and cascading of this Directory.Build.targets file, but I don't understand why my project, which were already having the "CopyLocal" set to false, and were already being build all in the same folder, cannot execute with this information. If somebody could explain, I would gladly award him the points

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 J4N