Search code examples
c#.net-corenuget-package.net-standard

Runtime exception referencing type from a NuGet package: 'Could not load file or assembly 'Foo'. The system cannot find the file specified.'


The Background:

  1. Consuming application is a website built using .NET Core 3.1.
  2. Consuming application references a NuGet package built using .NET Standard 2.0.
  3. The consuming application has a reference to the latest version of the NuGet package and builds without any errors or warnings.
  4. The DLL from the NuGet package is visible in the bin\debug\netcoreapp31\ and bin\release\netcoreapp31\ folders.
  5. In the IDE, full IntelliSense for the types in the NuGet package is available, including the XML Documentation comments for types, methods, and parameters.
  6. At runtime, at the first reference to any type in the NuGet package, a FileNotFound exception is thrown, reporting that the DLL from the NuGet package file cannot be found.

What I have tried:

  1. Verifying that the PackageReference entries in the .csproj file is correct. Since it's a .NET Core application, there are no hint paths or assembly binding redirects that are interfering with assembly binding.
  2. Clearing the NuGet cache.
  3. Removing and re-adding the NuGet package.
  4. Forcing NuGet to reinstall the package through the Package Manager Console.
  5. Cleaning and rebuilding the solution.
  6. Enabling Fusion Log Viewer and combing through the logs. This is where things get interesting. There are no entries in the logs indicating any attempts to resolve or load the assembly, or any entries indicating that the assembly bind failed.

Exception Message

Could not load file or assembly 'OneCall.FeatureFlags, Version=1.0.0.0, Culture=en-US, PublicKeyToken=null'. The system cannot find the file specified.
   at PTWebAPI.Startup.ConfigureServices(IServiceCollection services) in C:\Dev\PTWebAPI\PTWebAPI\Startup.cs:line 65
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.InvokeCore(Object instance, IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass9_0.<Invoke>g__Startup|0(IServiceCollection serviceCollection)
   at Microsoft.AspNetCore.Hosting.StartupLoader.ConfigureServicesDelegateBuilder`1.<>c__DisplayClass15_0.<BuildStartupServicesFilterPipeline>g__RunPipeline|0(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.Invoke(Object instance, IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass8_0.<Build>b__0(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.StartupLoader.ConfigureServicesDelegateBuilder`1.<>c__DisplayClass14_0.<ConfigureServices>g__ConfigureServicesWithContainerConfiguration|0(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.WebHost.EnsureApplicationServices()
   at Microsoft.AspNetCore.Hosting.WebHost.Initialize()
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
   at PTWebAPI.Program.Main(String[] args) in C:\Dev\PTWebAPI\PTWebAPI\Program.cs:line 15

Point of Failure

public static void Main(string[] args)
{
   CreateWebHostBuilder(args).Build().Run(); // <-- Right here
}

The Question

What causes this particular exception when loading an assembly in a .NET Core application, and how do you resolve it?


Solution

  • The issue turned out to be the culture on the NuGet package itself.

    The culture on the NuGet package was set to "en-US", while the culture on the consuming assembly was set to neutral (""). This prevented the consuming assembly from being able to load the NuGet package's assembly at runtime.

    The solution was to set the culture on the NuGet package to neutral (""), republish the package, and update the package reference in the consuming assembly.

    (Thanks to Hamlet Hakobyan for pointing me in the right direction.)