System.IO.FileNotFoundException: '无法加载文件或程序集。

4

我有一个在 .net core 上的应用程序。这个应用程序一直运行得很好,但是在我安装后开始出现错误。

dotnet add package Microsoft.AspNetCore.Authentication.Negotiate --version 6.0.14

一旦我安装了这个包并将这行代码放在startup.cs文件中:

  services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
           .AddNegotiate();
        services.AddAuthorization(options =>
        {
            options.FallbackPolicy = options.DefaultPolicy;
        });

我开始遇到这个错误:
System.IO.FileNotFoundException: 'Could not load file or assembly 'System.DirectoryServices.Protocols, Version=6.0.0.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.'

当我看到上述错误时,我安装了这个软件包:

dotnet add package System.DirectoryServices --version 7.0.0

我仍然遇到这个错误。该错误出现在program.cs文件中。以下是错误的屏幕截图:

enter image description here

很遗憾,在Nuget中我没有看到System.directoryServices.protocols的6.0.0.1版本。不确定为什么应用程序在我已经安装了7.0.0版本的情况下还要寻找6.0.0.1版本。以下是我应用程序上安装的包:

enter image description here

我尝试将这个放入我的web.config文件中:

<configuration>
   <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.DirectoryServices.Protocols" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.1" newVersion="7.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
</configuration>

1
尝试更新 System.DirectoryServices.Protocols 包的版本,使其与应用程序中的其他包兼容。Update-Package System.DirectoryServices.Protocols - Pranav Bilurkar
错误提示需要使用较低版本的System.DirectoryServices.Protocols。如果我升级,它会再次抛出错误吗? - Anjali
或者,您是否尝试向项目的 app.configweb.config 文件中添加绑定重定向,以将对版本 6.0.0.1 的请求重定向到应用程序中可用的版本。 - Pranav Bilurkar
我把绑定重定向放在我的 web.config 文件中,但仍然出现错误。 - Anjali
我注意到包7.0.0的System.DirectoryServices.Protocols的程序集版本是4.0.0.0。因此,绑定重定向可能需要指向4.0.0.0才能使其正常工作。我在dotnet 6中遇到了同样的问题,其中没有绑定重定向,我不知道该如何解决。 - rominator007
哦,毕竟还是遇到了同样的问题。看这里。你的绑定重定向在 .NET 6 中将不再起作用 https://dev59.com/IFYO5IYBdhLWcg3wP_Ub#46120907 - rominator007
2个回答

3
更新: 这是System.DirectoryServices.Protocols 7.0.0和Microsoft.AspNetCore.Authentication.Negotiate中的一个错误,已经得到修复,并将随着包的下一个版本一起发布。 在此处查看对问题的响应https://github.com/dotnet/runtime/issues/85296#issuecomment-1522092739 同时,请使用我提供的旧版本包的解决方法。

之前的分析(更新之前)

我没有解决这个问题,但是我会分享我的见解。也许有人可以根据我的发现找到问题的根本原因?

当运行发布的dotnet 6 exe时,我遇到了相同的错误。 当将Microsoft.AspNetCore.Authentication.Negotiate升级到6.0.16和System.DirectoryServices.Protocols从6.0.1升级到7.0.0时,出现了问题。 在Visual Studio中使用F5运行exe可以正常工作,但我无法解释这一点。我们在发布配置文件中使用<SelfContained>false</SelfContained>

与您的解决方案的不同之处可能在于,我们需要直接引用System.DirectoryServices.Protocols到另一个项目(而不是直接引用exe),因为我们在同一个exe中以编程方式访问LDAP服务器。

我注意到7.0.0版本的System.DirectoryServices.Protocols包的程序集版本是4.0.0.0,而6.0.1版本的程序集版本是6.0.0.1(请参见下面的截屏)。所以使用新版本的软件包会得到较低的程序集版本。这很奇怪,我认为这不是最佳实践。我认为,这可能会在发布步骤中使dotnet的依赖管理出现问题,但我没有找到任何信息或证据。我因程序集版本的原因为System.DirectoryServices提出了一个问题:https://github.com/dotnet/runtime/issues/85296

附注:程序集版本在Windows资源管理器的属性对话框中不会显示。获取程序集版本的最安全方式是通过https://learn.microsoft.com/en-us/dotnet/framework/tools/ildasm-exe-il-disassembler

enter image description here

enter image description here

所以你的绑定重定向必须指向4.0.0.0才能使其工作,但遗憾的是,在dotnet 6中不再有绑定重定向了(https://dev59.com/IFYO5IYBdhLWcg3wP_Ub#46120907)。

根据我在网上找到的进一步信息(https://www.reddit.com/r/csharp/comments/112h640/could_not_load_file_or_assembly/ https://learn.microsoft.com/en-us/answers/questions/1185993/system-io-filenotfoundexception-could-not-load-fil),目前唯一可行的解决方案似乎是直接使用System.DirectoryServices.Protocols 6.0.1并在应用程序中直接引用它。至少这对我们有效。

看起来,在System.DirectoryServices.Protocols的7.0.0版本中,某些东西出现了问题,导致发生了冲突,或者至少Microsoft.AspNetCore.Authentication.Negotiate 6.0.x与System.DirectoryServices.Protocols 7.0.0不兼容。


0
我在使用.NET Core 6的Blazor Server应用程序中遇到了类似的问题,而默认使用的是Negotiate 6.014,但是我的库也是使用.NET Core 6,使用的是System.DirectoryServices*版本7。在Program.cs的app.Run();处出现了错误。
通过更新System.DirectoryServices.Protocols的新项目版本以匹配我的库版本,问题得到了解决。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接