首页 > 解决方案 > 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?

标签: c#mysqlwindows-servicesentity-framework-core.net-assembly

解决方案


Have you tried running the service/application in a "normal" account? Try entering the account where you can log in interactively and run the application onto the service's Logon tab:

enter image description here

(Run "whoami" at a command prompt to find your account name.)


推荐阅读