Search code examples
c#.netvisual-studio.net-assembly.net-7.0

.NET 7.0 Project Reference: "Could not load file or assembly" when loading external Class Library DLL during runtime


I am creating a Class Library DLL (.NET 7.0), and I'm trying to test referencing the DLL from a Console App (.NET 7.0). Visual Studio detects all of the references in the code and there are no build errors at all. When I run the Console Application, as soon as it tries to call the reference DLL, it throws an exception:

5/9/2023 12:12:33 PM|Error|WebSocket.messages|System.IO.FileNotFoundException: Could not load file or assembly 'ExampleSharp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
                       File name: 'ExampleSharp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
                          at ExampleSharpDemo.Connection.OnMessage(MessageEventArgs e)
                          at WebSocketSharp.Server.WebSocketBehavior.onMessage(Object sender, MessageEventArgs e)
                          at WebSocketSharp.Ext.Emit[TEventArgs](EventHandler`1 eventHandler, Object sender, TEventArgs e)
                          at WebSocketSharp.WebSocket.messages(MessageEventArgs e)

Here is my .csproj for ExampleSharpDemo:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="NETCore.Encrypt" Version="2.1.1" />
    <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
    <PackageReference Include="WebSocketSharp-netstandard" Version="1.0.1" />
  </ItemGroup>

  <ItemGroup>
    <Reference Include="ExampleWebSockets">
      <HintPath>..\..\ExampleWebSockets\ExampleWebSockets\bin\Release\net7.0\publish\win-x86\ExampleWebSockets.dll</HintPath>
    </Reference>
    <Reference Include="ExampleSharp">
      <HintPath>..\ExampleSharp\bin\Release\net7.0\publish\win-x86\ExampleSharp.dll</HintPath>
      <Private>True</Private>
    </Reference>
  </ItemGroup>

</Project>

Here is my .csproj for ExampleSharp:

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

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="NETCore.Encrypt" Version="2.1.1" />
    <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
  </ItemGroup>

  <ItemGroup>
    <Reference Include="ExampleWebSockets">
      <HintPath>..\..\ExampleWebSockets\ExampleWebSockets\bin\Release\net7.0\publish\win-x86\ExampleWebSockets.dll</HintPath>
    </Reference>
  </ItemGroup>


</Project>

I've already tried rebuilding, running it as x86, deleting the reference and adding it again. I've also loaded in the "ExampleWebSockets" and "NETCore.Encrypt" dependencies for "ExampleSharp". I have no idea why it fails to load the assembly. I opened it in multiple dependency walkers and they all seem to detect it fine.

EDIT: I've also tried using Procmon to see why the dependency could not be loaded, but it seems like it finds the file is found with the result "FILE LOCKED WITH ONLY READERS"? I'm pretty confused.

Additional Info: I plan to distribute this Class Library as an external DLL. The end users (developers) will not have access to the source for "ExampleSharp". So, I can't just include the project reference directly. Though, I will mention when I include the project reference directly, it works fine and I do not get this exception.

Is there something I'm missing? Is .NET 7.0 just not meant to do this?

Thank you


Solution

  • With help from @t3chb0t and @ewerspej, I learned that this is a pretty archaic and inefficient way of distributing a DLL these days.

    Instead, it made a lot more sense to create a NuGet package (which is essentially just a ZIP file) and distribute that to the end user (developer in this instance). NuGet seems to help with the headache of directly referencing the DLLs.

    To obfuscate my DLL in the nupkg, I had to use my obfuscation tool and then replace the DLL in place before calling the dotnet pack command.