Skip to content

'Could not load file or assembly' exception when referencing console app from web app #23477

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
iulianb opened this issue Jan 18, 2022 · 2 comments

Comments

@iulianb
Copy link

iulianb commented Jan 18, 2022

Hi.

I have a web application that needs to run a tool as a build step. The tool is developed as a console application that also needs to load a configuration file so there is a reference to Microsoft.Extensions.Hosting. This console application is referenced by the web application. When building the web application I get the following exception:

Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Extensions.Hosting.Abstractions, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
File name: 'Microsoft.Extensions.Hosting.Abstractions, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'

This was working on .NET 5 but does no longer work after migrating to .NET 6. I think this is similar to #22053 but haven't been able to make it work using the solution provided in the discussion.

I'm attaching a sample project that reproduces the issue: ExeReference.zip

@ghost ghost added Area-NetSDK untriaged Request triage from a team member labels Jan 18, 2022
@dsplaisted
Copy link
Member

What's going on here is that the Microsoft.Extensions.Hosting APIs are available both as a NuGet package and as part of the ASP.NET Core shared framework. The Console app references them as a NuGet package reference. The Web app gets them via the implicit ASP.NET Core FrameworkReference, and the conflict resolution process in the build removes the DLLs that came in transitively from the PackageReference, because the ones supplied by the shared framework are preferred.

However, the console app's runtimeconfig doesn't specify that it depends on the ASP.NET Core shared framework, so when it tries to run it doesn't have access to those DLLs from the shared framework, and it also can't find them in the current folder because they were trimmed out by conflict resolution when building the web app.

For situations like this, I would recommend having the Console app use the ASP.NET Core shared framework so that it matches the web app it's being copied into. You can replace the PackageReference with this:

<FrameworkReference Include="Microsoft.AspNetCore.App" />

@dsplaisted dsplaisted added this to the Discussion milestone Feb 9, 2022
@dsplaisted dsplaisted removed the untriaged Request triage from a team member label Feb 9, 2022
@dsplaisted dsplaisted removed their assignment Feb 9, 2022
@iulianb iulianb closed this as completed Feb 10, 2022
@iulianb
Copy link
Author

iulianb commented Feb 10, 2022

@dsplaisted thank you!

@dsplaisted dsplaisted added the consider-for-documentation Issues where some of the information in the discussion may be appropriate to add to documentation label Feb 21, 2022
@baronfel baronfel removed the consider-for-documentation Issues where some of the information in the discussion may be appropriate to add to documentation label Sep 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants