.NET 7.0控制台引用外部类库DLL运行时加载失败求助
.NET 7.0控制台应用引用外部类库DLL运行时加载失败问题
运行控制台应用调用引用的DLL时,抛出以下异常:
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)
ExampleSharpDemo项目文件(.csproj)
<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>
ExampleSharp项目文件(.csproj)
<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>
已尝试的解决步骤
- 重新生成解决方案及所有项目
- 切换为x86架构运行控制台应用
- 删除并重新添加DLL引用
- 确保ExampleSharp已加载其依赖项(ExampleWebSockets、NETCore.Encrypt)
- 使用多个依赖检测工具验证依赖关系,结果均正常
- 通过Procmon排查,发现文件被找到但显示"FILE LOCKED WITH ONLY READERS"
额外说明
- 计划将ExampleSharp作为外部DLL分发,终端开发者无法获取其源码,因此不能使用项目引用
- 直接添加项目引用时,控制台应用运行正常,无异常
请问我遗漏了什么?.NET 7.0是否不支持这种直接引用已发布DLL的用法?
内容的提问来源于stack exchange,提问作者Thomas Yamakaitis
相关产品推荐
相关产品推荐

