简体   繁体   English

启动复杂的 ASP.NET Core 应用程序时无法加载文件或程序集

[英]Could not load file or assembly when starting complex ASP.NET Core application

I have complex ASP.NET Core solution, which consists of:我有复杂的 ASP.NET Core 解决方案,其中包括:

  • Web part, which is ASP.NET Core Web Api project Web 部件,即 ASP.NET Core Web Api 项目
  • other parts, which are .NET Core class libraries其他部分,即 .NET Core 类库

I want to add extension method to this class libraries, so they could register themselves, eg:我想向此类库添加扩展方法,以便他们可以自行注册,例如:

public static IServiceCollection AddThisLibrary(this IServiceCollection services)
{
    //a lot of addTransient...
    return services;
}

which I use in Startup class in ConfigureServices method, like this:我在ConfigureServices方法的Startup类中使用它,如下所示:

services.AddThisLibrary();

The problem is, that when I'm starting my application, it throws the exception:问题是,当我启动我的应用程序时,它抛出异常:

An error occurred while starting the application.启动应用程序时发生错误。 FileNotFoundException: Could not load file or assembly 'MyProject.API.Service, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. FileNotFoundException:无法加载文件或程序集“MyProject.API.Service,版本=1.0.0.0,文化=中性,PublicKeyToken=null”。 The system cannot find the file specified.系统找不到指定的文件。 MyProject.API.Web.Startup.ConfigureServices(IServiceCollection services) MyProject.API.Web.Startup.ConfigureServices(IServiceCollection 服务)

for each my library.对于我的每个图书馆。

I've checked /bin/Debug folder, where I have all my class libraries built in DLL , so I have no idea, why they can't be found.我检查了/bin/Debug文件夹,在那里我的所有类库都内置在DLL 中,所以我不知道为什么找不到它们。

How can I properly write such extension methods and remove that exception?如何正确编写此类扩展方法并删除该异常?

you should check that your dll is the same version as the one it is trying to load it, what i suggest is go to your reference, remove the existing one, go to your assembly remove it as well, then clean, then add it to your references again, build.您应该检查您的 dll 是否与它尝试加载的版本相同,我建议去您的参考资料,删除现有的,去您的程序集删除它,然后清理,然后将其添加到再次参考,构建。 Make sure you have one reference only.确保你只有一个参考。

My case may be specific a lot, but I faced this error when changed target from .net core 3.1 to .net 5. Some files in deploy folder remained from prevous deploys.我的情况可能很具体,但是当将目标从 .net core 3.1 更改为 .net 5 时,我遇到了这个错误。 deploy 文件夹中的一些文件仍然来自以前的部署。 I clean the folder an published once again.我再次清理发布的文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 '无法加载文件或程序集',DLL参考ASP.NET Core 2应用程序 - 'Could not load file or assembly', dll reference asp.net core 2 application ASP.net无法加载文件或程序集 - ASP.net Could not load file or assembly 无法加载文件或程序集asp.net - Could not load file or assembly asp.net Asp.net无法加载文件或程序集错误 - Asp.net Could not load file or assembly error 无法加载文件或程序集临时 ASP.NET 文件 - Could not load file or assembly Temporary ASP.NET Files 当我尝试以天蓝色发布我的ASP.NET CORE Web应用程序时出现问题-无法加载文件或程序集'Microsoft.Extensions.Identity.Core - Problem when i try to publish my ASP.NET CORE web app in azure - Could not load file or assembly 'Microsoft.Extensions.Identity.Core 无法加载文件或程序集-ASP.NET MVC应用 - Could not load file or assembly - asp.net mvc app ASP.NET运行时错误:无法加载文件或程序集'DotNetOpenAuth, - ASP.NET runtime error: Could not load file or assembly 'DotNetOpenAuth, 获取无法加载文件或程序集'System.Core,Version = 4.0.0.0 ..'在使用moq在asp.net核心中进行单元测试时 - Getting Could not load file or assembly 'System.Core, Version=4.0.0.0..' While doing unit testing in asp.net core with moq Asp.net核心负载组件 - Asp.net Core Load Assembly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM