Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Core published app fails with FileNotFoundException

I have a .NET Core console app with target netcoreapp2.1. It calls into an existing WCF service using the Windows Compatibility Pack.

When I run it through Visual Studio, or if I use dotnet run to run it from the command line, it works fine.

However, I want to publish it to a .exe file so that I can run it by double-clicking it.

So, I ran the Publish which appears to succeed, but when I try to run the published .exe file it fails with the following exception:

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.

This error happens on the machine that I ran the publish on, and also if I try copying the published folder to another machine. I believe that System.Private.ServiceModel is some sort of deep dependency of the Microsoft.Windows.Compatibility package, but why is this not working? Surely it should have been copied to the output folder automatically?

Is there additional configuration or setup required to get the publish to pull in dependencies automatically?

If it matters, the reference in the csproj file looks like this:

  <ItemGroup>
    <PackageReference Include="Microsoft.Windows.Compatibility" Version="2.0.1" />
  </ItemGroup>
like image 323
gallivantor Avatar asked Nov 22 '18 00:11

gallivantor


2 Answers

I had the same problem and I've manually added from nuget the missing file by installing the System.Private.ServiceModel package.

Link on nuget.org.

This is a hack though, because from the description of the package you can see that it's meant for .Net internal usage, but I didn't find any other options.

like image 54
Mihail Stancescu Avatar answered Sep 29 '22 02:09

Mihail Stancescu


Looking at the related GitHub issues*, people seem to have varying mileage with suggested workarounds.

What worked for me was copying the DLL from the runtime folder, to the bin folder, after build, by adding this to my Azure Function app 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>

Other workarounds include installing the System.Private.ServiceModel package directly, or increasing version of System.ServiceModel to 4.5.3, neither of which worked for me.

*

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

https://github.com/dotnet/wcf/issues/2349

like image 21
Saeb Amini Avatar answered Sep 29 '22 03:09

Saeb Amini