Search code examples
c#mysqlwindows-servicesentity-framework-core.net-assembly

assembly file load error when running application as windows service


I have an application configured to run on the console or as a service (depending on if I run the .exe manually or install it).

Running it from the command line works great... but when I install it as a service I get a file load error:

System.IO.FileLoadException: Could not load file or assembly 'Microsoft.EntityFrameworkCore.Relational, Version=2.2.0.0'  ... the located assembly's manifest definition does not match the assembly reference.

This package is needed because I am using EntityFrameworkCore and Pomelo libraries in order to query a MySQL database. I have made sure all required NuGet packages are included and up-to-date.

I've done a few things to remedy this error, and honestly can't see what I'm missing...

First, my program runs this line at the very beginning to ensure the service is running from the directory I expect (instead of system32):

System.IO.Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory);

Next, I tried reverting the specific package to v2.0.0.0 to match the error description... once I build it with the reverted package though, the other packages throw an error that they are looking for the updated version v2.2.6.0

I have also tried removing all references on the production machine to v2.0.0.0... so it wouldn't even be looking for that

My .csproj file only references the latest version:

<Reference Include="Microsoft.EntityFrameworkCore.Relational, Version=2.2.6.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
  <HintPath>packages\Microsoft.EntityFrameworkCore.Relational.2.2.6\lib\netstandard2.0\Microsoft.EntityFrameworkCore.Relational.dll</HintPath>
</Reference>

I even have a binding redirect in my app.config file that is seemingly being ignored entirely:

  <dependentAssembly>
    <assemblyIdentity name="Microsoft.EntityFrameworkCore.Relational" publicKeyToken="adb9793829ddae60" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-2.2.6.0" newVersion="2.2.6.0" />
  </dependentAssembly>

Now I am at a real loss as to what I could be missing... As noted above, I only get the file missing error when running as a service...

Why would my application not be finding the proper assembly version when run as a service? How do I resolve the issue?


Solution

  • I'm honestly still not entirely sure why this happens, but I found a solution...

    As far as I can tell the problem really has to do with NuGet packages not being descriptive enough.

    Basically, what was happening was the package I was using stated it could use any version of Microsoft.EntityFrameworkCore.Relational as long as it was 2.2.0.0 or greater. NuGet suggested updating to the latest version, so I did... which happened to be 2.2.6.0. This update broke it...

    So reverting to 2.2.0.0 AND removing the binding redirect fixed my problem. Both of these things had to be done since only reverting to the earlier version didn't resolve that the binding redirect was forcing it to look for a later version...