.NET Core 发布的应用程序因 FileNotFoundException 而失败
.NET Core published app fails with FileNotFoundException
我有一个目标为 netcoreapp2.1 的 .NET Core 控制台应用程序。它使用 Windows 兼容包调用现有的 WCF 服务。
当我通过 Visual Studio 运行 时,或者如果我从命令行使用 dotnet run
到 运行 时,它工作正常。
但是,我想将它发布到一个 .exe 文件中,这样我就可以通过双击它来运行它。
所以,我 运行 发布似乎成功了,但是当我尝试 运行 发布的 .exe 文件时,它失败了,出现以下异常:
System.IO.FileNotFoundException: Could not load file or assembly
'System.Private.ServiceModel, Version=4.1.2.1, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
此错误发生在我 运行 发布的机器上,并且如果我尝试将发布的文件夹复制到另一台机器。我相信 System.Private.ServiceModel
是 Microsoft.Windows.Compatibility
包的某种深度依赖,但为什么这不起作用?确定应该自动复制到输出文件夹?
是否需要额外的配置或设置才能让发布自动引入依赖项?
如果重要,csproj 文件中的引用如下所示:
<ItemGroup>
<PackageReference Include="Microsoft.Windows.Compatibility" Version="2.0.1" />
</ItemGroup>
我遇到了同样的问题,我通过安装 System.Private.ServiceModel
包从 nuget 中手动添加了丢失的文件。
虽然这是一个 hack,因为从包的描述中您可以看到它是供 .Net 内部使用的,但我没有找到任何其他选项。
加入vsstudio nuget包
Install-Package System.Private.ServiceModel
现在发布时会包含dll。
查看相关的 GitHub 问题*,人们似乎对建议的解决方法有不同的看法。
对我有用的是在构建后将 DLL 从运行时文件夹复制到 bin 文件夹,方法是将其添加到我的 Azure Function 应用程序 csproj:
<!-- https://github.com/dotnet/wcf/issues/2824 -->
<Target Name="FixForDotnetWcfIssueBuild" BeforeTargets="PostBuildEvent">
<Copy SourceFiles="$(OutputPath)bin\runtimes\win\lib\netstandard2.0\System.Private.ServiceModel.dll" DestinationFolder="$(OutputPath)bin" />
</Target>
<Target Name="FixForDotnetWcfIssuePublish" AfterTargets="AfterPublish">
<Copy SourceFiles="$(PublishDir)bin\runtimes\win\lib\netstandard2.0\System.Private.ServiceModel.dll" DestinationFolder="$(PublishDir)bin" />
</Target>
其他解决方法包括直接安装 System.Private.ServiceModel
包,或将 System.ServiceModel 的版本增加到 4.5.3,这两种方法都不适合我。
*
https://github.com/dotnet/wcf/issues/2824
https://github.com/Azure/azure-functions-host/issues/4304
https://github.com/Azure/azure-functions-host/issues/3568#issuecomment-433146109
我发现这是由我的一个包裹中的 PrivateAssets="All"
引起的:
<PackageReference Include="ULabs.VBulletinEntity" Version="0.7.17" />
背景:ULabs.VBulletinEntity
本身指的是一些共享库,应该自动运送而不安装第二个包。所以我尝试了this workaround,建议PrivateAssets="All"
集成共享库。
这有效,但是当部署到 Linux 时,包在 dotnet restore
之后不存在。这导致启动应用程序后出现 FileNotFound 异常。由于我没有收到任何错误,我尝试使用
手动安装它
RUN dotnet add package ULabs.VBulletinEntity --version -s my-baget-repo
这至少给了我一个错误:
error: Package 'ULabs.VBulletinEntity' is incompatible with 'all' frameworks in project '/app/MyApp/MyApp.csproj'.
错误消息没有任何有用的信息。大多数情况下,他们使用另一个版本(例如 3.0 项目中的 .NET Core 2.0 库)。在另一种情况下,似乎是某种有线错误。
删除 PrivateAssets="All"
后,包出现在发布输出中,我可以再次启动应用程序。这对我来说似乎很奇怪,但也许是同一个人的时间。我在这个问题上浪费了几个小时。使用 .NET Core 2.1 和 .NET Standard 2.0 作为库。
我有一个目标为 netcoreapp2.1 的 .NET Core 控制台应用程序。它使用 Windows 兼容包调用现有的 WCF 服务。
当我通过 Visual Studio 运行 时,或者如果我从命令行使用 dotnet run
到 运行 时,它工作正常。
但是,我想将它发布到一个 .exe 文件中,这样我就可以通过双击它来运行它。
所以,我 运行 发布似乎成功了,但是当我尝试 运行 发布的 .exe 文件时,它失败了,出现以下异常:
System.IO.FileNotFoundException: Could not load file or assembly
'System.Private.ServiceModel, Version=4.1.2.1, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
此错误发生在我 运行 发布的机器上,并且如果我尝试将发布的文件夹复制到另一台机器。我相信 System.Private.ServiceModel
是 Microsoft.Windows.Compatibility
包的某种深度依赖,但为什么这不起作用?确定应该自动复制到输出文件夹?
是否需要额外的配置或设置才能让发布自动引入依赖项?
如果重要,csproj 文件中的引用如下所示:
<ItemGroup>
<PackageReference Include="Microsoft.Windows.Compatibility" Version="2.0.1" />
</ItemGroup>
我遇到了同样的问题,我通过安装 System.Private.ServiceModel
包从 nuget 中手动添加了丢失的文件。
虽然这是一个 hack,因为从包的描述中您可以看到它是供 .Net 内部使用的,但我没有找到任何其他选项。
加入vsstudio nuget包
Install-Package System.Private.ServiceModel
现在发布时会包含dll。
查看相关的 GitHub 问题*,人们似乎对建议的解决方法有不同的看法。
对我有用的是在构建后将 DLL 从运行时文件夹复制到 bin 文件夹,方法是将其添加到我的 Azure Function 应用程序 csproj:
<!-- https://github.com/dotnet/wcf/issues/2824 -->
<Target Name="FixForDotnetWcfIssueBuild" BeforeTargets="PostBuildEvent">
<Copy SourceFiles="$(OutputPath)bin\runtimes\win\lib\netstandard2.0\System.Private.ServiceModel.dll" DestinationFolder="$(OutputPath)bin" />
</Target>
<Target Name="FixForDotnetWcfIssuePublish" AfterTargets="AfterPublish">
<Copy SourceFiles="$(PublishDir)bin\runtimes\win\lib\netstandard2.0\System.Private.ServiceModel.dll" DestinationFolder="$(PublishDir)bin" />
</Target>
其他解决方法包括直接安装 System.Private.ServiceModel
包,或将 System.ServiceModel 的版本增加到 4.5.3,这两种方法都不适合我。
*
https://github.com/dotnet/wcf/issues/2824
https://github.com/Azure/azure-functions-host/issues/4304
https://github.com/Azure/azure-functions-host/issues/3568#issuecomment-433146109
我发现这是由我的一个包裹中的 PrivateAssets="All"
引起的:
<PackageReference Include="ULabs.VBulletinEntity" Version="0.7.17" />
背景:ULabs.VBulletinEntity
本身指的是一些共享库,应该自动运送而不安装第二个包。所以我尝试了this workaround,建议PrivateAssets="All"
集成共享库。
这有效,但是当部署到 Linux 时,包在 dotnet restore
之后不存在。这导致启动应用程序后出现 FileNotFound 异常。由于我没有收到任何错误,我尝试使用
RUN dotnet add package ULabs.VBulletinEntity --version -s my-baget-repo
这至少给了我一个错误:
error: Package 'ULabs.VBulletinEntity' is incompatible with 'all' frameworks in project '/app/MyApp/MyApp.csproj'.
错误消息没有任何有用的信息。大多数情况下,他们使用另一个版本(例如 3.0 项目中的 .NET Core 2.0 库)。在另一种情况下,似乎是某种有线错误。
删除 PrivateAssets="All"
后,包出现在发布输出中,我可以再次启动应用程序。这对我来说似乎很奇怪,但也许是同一个人的时间。我在这个问题上浪费了几个小时。使用 .NET Core 2.1 和 .NET Standard 2.0 作为库。